我有以下数据要在ElasticSearch上编制索引.
我想实现自动完成功能,并突出显示特定文档与查询匹配的原因.
这是我的索引的设置:
{
"settings": {
"number_of_shards": 1,
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 15
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"autocomplete_filter"
]
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
指数分析
因此倒置指数看起来像:
这就是我为名称字段定义映射的方式:
{
"index_type": {
"properties": {
"name": {
"type": "string",
"index_analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我查询时:
GET http://localhost:9200/index/type/_search
{
"query": {
"match": {
"name": "soft"
}
},
"highlight": {
"fields" : {
"name" : {}
}
}
}
Run Code Online (Sandbox Code Playgroud)
搜索:软
应用标准标记符,"软"是用于在倒排索引上查找的术语.此搜索匹配文档:1,3,4,5,6,7这是正确的,但突出显示的部分我希望是"软"而不是整个单词:
{
"hits": [
{
"_source": {
"name": "SoftwareRocks everytime"
},
"highlight": {
"name": [
"<em>SoftwareRocks</em> everytime"
]
}
},
{
"_source": {
"name": "Software AG"
},
"highlight": {
"name": [
"<em>Software</em> AG"
]
}
},
{
"_source": {
"name": "Software AG2"
},
"highlight": {
"name": [
"<em>Software</em> AG2"
]
}
},
{
"_source": {
"name": "Op Software AG good software better"
},
"highlight": {
"name": [
"Op <em>Software</em> AG good <em>software</em> better"
]
}
},
{
"_source": {
"name": "Op Software AG"
},
"highlight": {
"name": [
"Op <em>Software</em> AG"
]
}
},
{
"_source": {
"name": "is soft ware ok"
},
"highlight": {
"name": [
"is <em>soft</em> ware ok"
]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
搜索:软件ag
应用标准标记符,将"软件ag"转换为"软件"和"ag",以找到倒排索引.这个搜索匹配文档:1,3,4,5,6,这是正确的,但突出显示的部分我希望是"软件"和"ag",而不是围绕"软件"和"ag"的整个词:
{
"hits": [
{
"_source": {
"name": "Software AG"
},
"highlight": {
"name": [
"<em>Software</em> <em>AG</em>"
]
}
},
{
"_source": {
"name": "Software AG2"
},
"highlight": {
"name": [
"<em>Software</em> <em>AG2</em>"
]
}
},
{
"_source": {
"name": "Op Software AG"
},
"highlight": {
"name": [
"Op <em>Software</em> <em>AG</em>"
]
}
},
{
"_source": {
"name": "Op Software AG good software better"
},
"highlight": {
"name": [
"Op <em>Software</em> <em>AG</em> good <em>software</em> better"
]
}
},
{
"_source": {
"name": "SoftwareRocks everytime"
},
"highlight": {
"name": [
"<em>SoftwareRocks</em> everytime"
]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
我阅读了有关elasticsearch的高亮文档,但我无法理解突出显示是如何执行的.对于上面的两个例子,我希望只有突出显示倒排索引上的匹配标记,而不是整个单词.任何人都可以帮助如何突出显示传递的值?
更新
因此,似乎在ElasticSearch网站上,服务器端的自动完成与我的实现类似.但是,它们似乎突出显示了客户端上匹配的查询.如果他们这样做,我开始认为在ElasticSearch方面没有合适的解决方案,所以我在服务器端实现了突出显示功能,而不是在客户端(就像他们似乎那样).
我在服务器端的实现(使用PHP)是:
public function search($term)
{
$params = [
'index' => $this->getIndexName(),
'type' => $this->getIndexType(),
'body' => [
'query' => [
'match' => [
'name' => $term
]
]
]
];
$results = $this->client->search($params);
$hits = $results['hits']['hits'];
$data = [];
$wrapBefore = '<strong>';
$wrapAfter = '</strong>';
foreach ($hits as $hit) {
$data[] = [
$hit['_source']['id'],
$hit['_source']['name'],
preg_replace("/($term)/i", "$wrapBefore$1$wrapAfter", strip_tags($hit['_source']['name']))
];
}
return $data;
}
Run Code Online (Sandbox Code Playgroud)
输出我对此问题的目标:
我添加了一笔赏金,看看ElasticSearch级别是否有解决方案来实现我上面描述的内容.
截至目前,对于最新版本的弹性,这是不可能的,因为高亮文档不引用任何设置或查询。我在 xhr requests 选项卡下的浏览器控制台中检查了弹性自动完成示例,并发现关键字的“att”自动完成响应的响应如下。
\n\nurl - https://search.elastic.co/suggest?q=att\n {\n "current_page": 1,\n "last_page": 4,\n "total_hits": 49,\n "hits": [\n {\n "tags": [],\n "url": "/elasticon/tour/2016/jp/not-attending",\n "section": "Elasticon",\n "title": "Not <em>Attending</em> - JP"\n },\n {\n "section": "Elasticon",\n "title": "<em>Attending</em> from Training - JP",\n "tags": [],\n "url": "/elasticon/tour/2016/jp/attending-training"\n },\n {\n "tags": [],\n "url": "/elasticon/tour/2016/jp/attending-keynote",\n "title": "<em>Attending</em> from Keynote - JP",\n "section": "Elasticon"\n },\n {\n "tags": [],\n "url": "/elasticon/tour/2016/not-attending",\n "section": "Elasticon",\n "title": "Thank You - Not <em>Attending</em>"\n },\n {\n "tags": [],\n "url": "/elasticon/tour/2016/attending",\n "section": "Elasticon",\n "title": "Thank You - <em>Attending</em>"\n },\n {\n "section": "Blog",\n "title": "What It\'s Like to <em>Attend</em> Elastic Training",\n "tags": [],\n "url": "/blog/what-its-like-to-attend-elastic-training"\n },\n {\n "tags": "Elasticsearch",\n "url": "/guide/en/elasticsearch/plugins/5.0/mapper-attachments-highlighting.html",\n "section": "Docs/",\n "title": "Highlighting <em>attachments</em>"\n },\n {\n "title": "<em>attachments</em> \xc2\xbb email",\n "section": "Docs/",\n "tags": "Logstash",\n "url": "/guide/en/logstash/5.0/plugins-outputs-email.html#plugins-outputs-email-attachments"\n },\n {\n "section": "Docs/",\n "title": "Configuring Email <em>Attachments</em> \xc2\xbb Actions",\n "tags": "Watcher",\n "url": "/guide/en/watcher/2.4/actions.html#configuring-email-attachments"\n },\n {\n "url": "/guide/en/watcher/2.4/actions.html#hipchat-action-attributes",\n "tags": "Watcher",\n "title": "HipChat Action <em>Attributes</em> \xc2\xbb Actions",\n "section": "Docs/"\n },\n {\n "title": "Slack Action <em>Attributes</em> \xc2\xbb Actions",\n "section": "Docs/",\n "tags": "Watcher",\n "url": "/guide/en/watcher/2.4/actions.html#slack-action-attributes"\n }\n ],\n "aggs": {\n "sections": [\n {\n "Elasticon": 5\n },\n {\n "Blog": 1\n },\n {\n "Docs/": 43\n }\n ],\n "top_tags": [\n {\n "XPack": 14\n },\n {\n "Elasticsearch": 12\n },\n {\n "Watcher": 9\n },\n {\n "Logstash": 4\n },\n {\n "Clients": 3\n },\n {\n "Shield": 1\n }\n ]\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\n但在前端,他们仅在自动建议结果中突出显示“att”。因此,他们正在浏览器层上处理突出显示的内容。
\n| 归档时间: |
|
| 查看次数: |
1195 次 |
| 最近记录: |