【发布时间】:2018-02-09 21:53:58
【问题描述】:
我在 elasticsearch 中有一个索引,其中正文包含一个字段数组和一个值数组。例如:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "families",
"_type": "family",
"_id": "o8qxd2EB9CizMt-k15mv",
"_score": 1,
"_source": {
"names": [
"Jefferson Erickson",
"Bailee Miller",
"Ahmed Bray"
]
}
},
{
"_index": "families",
"_type": "family",
"_id": "osqxd2EB9CizMt-kfZlJ",
"_score": 1,
"_source": {
"names": [
"Nia Walsh",
"Jefferson Erickson",
"Darryl Stark"
]
}
},
{
"_index": "families",
"_type": "family",
"_id": "pMrEd2EB9CizMt-kq5m-",
"_score": 1,
"_source": {
"names": [
"lia shelton",
"joanna shaffer",
"mathias little"
]
}
}
]
}
}
现在我需要一个搜索查询,我可以在其中从一组值中搜索文档,如下所示:
GET /families/_search
{
"query" : {
"bool" : {
"filter" : {
"bool" : {
"should" : [
{"match_phrase" : {"names" : ["ahmed bray", "nia walsh"]}}
]
}
}
}
}
}
它应该返回包含这些名称的 2 个文档,如下所示:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": [
{
"_index": "families",
"_type": "family",
"_id": "o8qxd2EB9CizMt-k15mv",
"_score": 0,
"_source": {
"names": [
"Jefferson Erickson",
"Bailee Miller",
"Ahmed Bray"
]
}
},
{
"_index": "families",
"_type": "family",
"_id": "osqxd2EB9CizMt-kfZlJ",
"_score": 0,
"_source": {
"names": [
"Nia Walsh",
"Jefferson Erickson",
"Darryl Stark"
]
}
}
]
}
}
如何进行这样的查询?我尝试使用“terms”关键字,但“terms”只允许我从数组中搜索单个单词,如下所示: {“条款”:{“名称”:[“布雷”,“尼亚”]}}
但我需要使用这样的全名: {"names" : ["ahmed bray", "nia walsh"]}}
【问题讨论】:
标签: elasticsearch elasticsearch-5