gpt4 book ai didi

elasticsearch - 嵌套类型的 Elasticsearch bool 查询中的模型 'or TRUE'?

转载 作者:行者123 更新时间:2023-12-03 02:34:08 25 4
gpt4 key购买 nike

我正在尝试在 Elasticsearch 中构建以下查询:

(query1) AND (query2 OR query2 OR TRUE)

使用elasticsearch 是否可以实现“OR true”部分,或者可能有另一种构造查询的方法以提供相同的结果?

我有一组文档,比如说 10 个,所有匹配的 tag1,这 10 个文档中的一些也将匹配 tag2 和 tag3,如果是这样,我使用命名查询来告诉我哪些文档匹配 tag2 和 tag3(文档匹配tag2 和 tag3 是匹配 tag1 的文档的子集)。

但是,即使没有匹配 tag2 或 tag3,我仍然应该从匹配 tag1 的初始查询中获得结果。
GET /test/_search
{
"query": {
"nested": {
"path": "TAGS",
"query": {
"bool": {
"must": [
{
"match": {
"TAGS.ID": {
"query": "tag1",
"_name": "tag1-query"
}
}
},
{
"bool": {
"should": [
{
"match": {
"TAGS.ID": {
"query": "tag2",
"_name": "tag2-query"
}
}
},
{
"match": {
"TAGS.ID": {
"query": "tag3",
"_name": "tag3-query"
}
}
},
// OR true here?
]
}
}
]
}
},
"inner_hits": {}
}
}
}

更新:基于@Val 的评论。这是我的完整测试:
PUT /test

PUT /test/_mapping/_doc
{
"properties": {
"name": {
"type": "text"
},
"TAGS": {
"type": "nested"
}
}

}

POST /test/_doc
{
"name" : "doc1",
"TAGS" : [
{
"ID" : "tag1",
"TYPE" : "BASIC"
},
{
"ID" : "tag2",
"TYPE" : "BASIC"
}
]
}


# (tag1) and (tag2 or tag3 or true)
GET /test/_search
{
"query": {
"nested": {
"path": "TAGS",
"query": {
"bool": {
"must": [
{
"match": {
"TAGS.ID": {
"query": "tag1",
"_name": "tag1-query"
}
}
}
],
"should": [
{
"match": {
"TAGS.ID": {
"query": "tag2",
"_name": "tag2-query"
}
}
},
{
"match": {
"TAGS.ID": {
"query": "tag3",
"_name": "tag3-query"
}
}
}
]
}
},
"inner_hits": {}
}
}
}

运行上面的查询只会给出以下结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.6931472,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "SaOs8G4BbvPS27u-IouS",
"_score" : 0.6931472,
"_source" : {
"name" : "doc1",
"TAGS" : [
{
"ID" : "tag1",
"TYPE" : "BASIC"
},
{
"ID" : "tag2",
"TYPE" : "BASIC"
}
]
},
"inner_hits" : {
"TAGS" : {
"hits" : {
"total" : 1,
"max_score" : 0.6931472,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "SaOs8G4BbvPS27u-IouS",
"_nested" : {
"field" : "TAGS",
"offset" : 0
},
"_score" : 0.6931472,
"_source" : {
"ID" : "tag1",
"TYPE" : "BASIC"
},
"matched_queries" : [
"tag1-query"
]
}
]
}
}
}
}
]
}
}

IE。 matched_queries数组仅报告 tag1-query 的匹配项,当我期望它包含 tag1-query 时和 tag2-query ?

最佳答案

您需要两个嵌套查询,因为您正在检查对两个不同嵌套元素(它们是两个不同的嵌套文档)的约束。试试这个:

{
"query": {
"bool": {
"must": {
"nested": {
"path": "TAGS",
"query": {
"match": {
"TAGS.ID": {
"query": "tag1",
"_name": "tag1-query"
}
}
}
}
},
"should": [
{
"nested": {
"path": "TAGS",
"query": {
"match": {
"TAGS.ID": {
"query": "tag2",
"_name": "tag2-query"
}
}
}
}
},
{
"nested": {
"path": "TAGS",
"query": {
"match": {
"TAGS.ID": {
"query": "tag3",
"_name": "tag3-query"
}
}
}
}
}
]
}
}
}

关于elasticsearch - 嵌套类型的 Elasticsearch bool 查询中的模型 'or TRUE'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59271521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com