我正在使用elasticsearch-6.4.3
我使用以下映射创建了索引test_index:
PUT test_index
{
"mappings": {
"_doc": {
"properties": {
"name": {
"type": "keyword"
},
"segments": {
"type": "nested",
"properties": {
"f1": {
"type": "keyword"
},
"f2": {
"type": "integer"
}
}
}
}
}
}
}
我使用以下命令在索引中插入了一个文档:
POST /test_index/_doc
{
"name": "ABC",
"segments": [{
"f1": "abc",
"f2": 123
},
{
"f1": "def",
"f2": 124
}]
}
我有一组值说
arr = ["def", "xyz"]
。我想将所有f1 field in segments field
中至少一个匹配的文档与给定数组arr
中的任何值进行匹配。您可以将其称为array_intersect之类的名称,以便于理解。
我正在尝试这样的事情:
GET /test_index/_search
{
"query": {
"nested": {
"path": "segments",
"query": {
"bool": {
"must": [
{
"terms": {
"f1": [
"def",
"xyz"
]
}
}
]
}
}
}
}
}
我没有得到任何结果作为输出。
Expected output
:文档应该匹配,因为文档中f1的值存在“def”。
最佳答案
您需要在术语查询中将完整路径指定为segments.f1
而不是nested query的f1
。
修改查询,如下所示:
POST test_index/_search
{
"query":{
"nested":{
"path":"segments",
"query":{
"bool":{
"must":[
{
"terms": {
"segments.f1": ["def","xyz"] <---- Mention full path here
}
}
]
}
}
}
}
}
希望这可以帮助!
关于elasticsearch - 在Elasticsearch中搜索对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58321975/