Elasticsearch 返回搜索到的词 [英] Elasticsearch return searched word
问题描述
我正在使用 fuzzy
并且希望 elasticsearch 返回搜索到的单词而不仅仅是命中.当我搜索词 dogo
并且我的模糊搜索找到词 dog
时,我想知道是 dogo
找到了它.
I am using fuzzy
and want elasticsearch to return the searched word not just the hit.
When i am searching for the word dogo
and my fuzzy search finds the word dog
i want to know that it was dogo
who found it.
数据:
{ "index": { "_id":1 }}
{ "title": "The quick brown fox", "price":5 }
{ "index": { "_id":2 }}
{ "title": "The quick blue dog", "price":7 }
{ "index": { "_id":3 }}
{ "title": "The slow brown dog", "price":5 }
查询:
{
"query": {
"bool": {
"should": [
{
"fuzzy": {
"title": "dogo"
}
},
{
"fuzzy": {
"title": "fox"
}
}
]
}
},
"highlight" : {
"fields" : {
"title":{
"pre_tags": [
"===>"
],
"post_tags": [
"<==="
],
"fragment_size": 200,
"number_of_fragments": 100
}
}
}
}
此查询将返回 ===>dog<===
但不知道 dogo
是否找到它.
This query will return ===>dog<===
but don't know if dogo
found it.
有谁知道如何做到这一点或想法?我希望我的输出类似于 dog : dogo
.
Does anyone know how to do this or an idea?
I want my output to be something like dog : dogo
.
推荐答案
你可以使用 命名查询,为此,为您的每个查询命名.在结果中,每个命中都将包含一个 matched_queries
数组,其中包含匹配的查询的名称(例如,下面的 dogo
和 fox
).>
You could use named queries for this, by giving a name to each of your queries. In the results, each hit will feature a matched_queries
array containing the names of the queries that matched (e.g. dogo
and fox
below).
{
"query": {
"bool": {
"should": [
{
"fuzzy": {
"name": {
"value": "dogo",
"_name": "dogo"
}
}
},
{
"fuzzy": {
"name": {
"value": "fox",
"_name": "fox"
}
}
}
]
}
},
"highlight": {
"fields": {
"title": {
"pre_tags": [
"===>"
],
"post_tags": [
"<==="
],
"fragment_size": 200,
"number_of_fragments": 100
}
}
}
}
这篇关于Elasticsearch 返回搜索到的词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!