Elasticsearch搜索多个文档的属性匹配查询

jtjikinw  于 2023-01-12  发布在  ElasticSearch
关注(0)|答案(1)|浏览(122)

我的数据模型中,具有不同属性的多个文档通过chainID逻辑连接,因为文档之间的索引时间未定义,即在后台执行之后。所有文档都在同一索引上索引。示例文档:

Doc 1:
   {
     "att1": "a",
     "att2": "b",
     "chainID": "123"
   }
Doc 2:
   {
     "att3": "c",
     "att4": "d",
     "chainID": "123"
   }
Doc 3:
   {
     "att1": "x",
     "att2": "y",
     "chainID": "678"
   }
Doc 4:
   {
     "att3": "z",
     "att4": "u",
     "chainID": "678"
   }

Map:

{
    "properties": {
        "att1": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                }
            }
        },
        "att2": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                }
            }
        },
        "att3": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                }
            }
        },
        "att4": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                }
            }
        },
        "chainID": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                }
            }
        }
    }
}

我想按chainID对文档进行分组,并搜索聚合结果,这样使用att1=a AND att3=c的查询就会得到chainID=123作为结果。
我尝试了以下查询,但没有找到匹配的文档

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "att1.keyword": "a"
                    }
                },
                {
                    "term": {
                        "att3.keyword": "c"
                    }
                }
            ]
        }
    },
    "aggs": {
        "chainIDs": {
            "terms": {
                "field": "chainID.keyword"
            },
            "aggs": {
                "docs": {
                    "top_hits": {
                        "_source": [
                            "chainID"
                        ]
                    }
                }
            }
        }
    }
}

看起来聚合似乎发生在查询处理之后。我想做的是根据文档的chainID聚合文档,并对聚合的文档运行查询。ElasticSearch是否可以实现这一点,或者我是否需要调整Map/数据模型?

6rqinv9w

6rqinv9w1#

尝试将“must”替换为should(逻辑OR)。“Must”要求同一文档中att1=1和att3=c(逻辑AND)。

{
"query": {
    "bool": {
        "should": [
            {
                "term": {
                    "att1.keyword": "a"
                }
            },
            {
                "term": {
                    "att3.keyword": "c"
                }
            }
        ]
    }
},
"aggs": {
    "chainIDs": {
        "terms": {
            "field": "chainID.keyword"
        },
        "aggs": {
            "docs": {
                "top_hits": {
                    "_source": [
                        "chainID"
                    ]
                }
            }
        }
    }
}

}

相关问题