给出一个搜索多个字段的数据结构,我该如何检索匹配的字段?
示例数据:
doc = {el.id: el.to_dict() for el in doc_ref}
查询类似于:
person {
"id": 123,
"name": Bill,
"name": William,
"surname": Smith
}
有没有办法让ES返回
GET _search
{
"query": {
"multi_match" : {
"query": "Will",
"fields": [ "name", "surname" ]
}
}
}
我知道突出显示了,也许有一种方法可以使用hits[
type: person,
id: 123,
matched_name: "William"
]
返回匹配的字段而不突出显示。
答案 0 :(得分:0)
据我所知,最接近的解决方案是使用named queries和一个bool
查询:
GET mynames/_search
{
"query": {
"bool": {
"should": [
{
"multi_match" : {
"query": "Bill",
"fields": ["name"],
"_name" : "name"
}
},
{
"multi_match" : {
"query": "Bill",
"fields": ["surname"],
"_name" : "surname"
}
}
]
}
}
}
这将产生以下结果:
{
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "mynames",
"_type": "_doc",
"_id": "123",
"_score": 0.2876821,
"_source": {
"id": 123,
"name": [
"Bill",
"William"
],
"surname": "Smith"
},
"matched_queries": [
"name" // <== the "name" part matched
]
}
]
}
}
这并不是multi_match
查询的直接替代品,因为它在后台做了一些魔术,但是应该可以通过结合bool
和其他查询来获得相同的行为,例如multi_match
,match
,dis_max
,function_score
等。
希望有帮助!
答案 1 :(得分:0)
这可以通过highlight
功能来完成。可以搜索具有多个条目的字段,然后仅返回匹配的字符串。
GET mynames/_search
{
"fields": ["_id", "surname"],
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "Will",
"type": "phrase_prefix",
"fields": [
"name",
"surname"
]
}
}
]
}
},
"highlight": {
"order": "score",
"pre_tags": [""],
"post_tags": [""],
"fields": {
"names": {"fragment_size": 150, "number_of_fragments": 3}
}
}
}
返回
"hits": [
{
"_id": "123",
"fields": {
"surname": [
"Smith"
]
},
"highlight": {
"names": [
"William"
]
}
},
...
匹配字段在突出显示项下列出的位置。请注意标签的删除,这里不需要。