当前位置: 首页 > 知识库问答 >
问题:

嵌套类型的Elasticsearch布尔查询中的Model“还是True”?

樊奇思
2023-03-14

我试图在ElasticSearch中构建以下查询:

(query1) AND (query2 OR query2 OR TRUE)

是否可以使用elasticsearch使用“或真”部分,或者有其他方法构造查询以给出相同的结果?

我有一组文档,比如说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"
                  ]
                }
              ]
            }
          }
        }
      }
    ]
  }
}

即。matched_queries数组只报告了tag1-query的匹配,而我希望它包含tag1-querytag2-query

共有1个答案

周博达
2023-03-14

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

{
  "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"
                }
              }
            }
          }
        }
      ]
    }
  }
}
 类似资料:
  • 问题内容: 我正在使用Nest Elastic并使用Head插件为布尔搜索构建查询,我正在合并多个查询 有关数据库结构和弹性映射的注释 数据库中的每个文档都链接到特定的profileId,后者又具有多个属性 每个文档都有与其关联的多个属性值 在此查询中,我要获取具有特定配置文件和属性值> 30的所有文档,同时要记住,此属性应仅具有ID 2。 SQL查询: 从文档d内部联接attributeValu

  • 问题内容: 我在获取与Elasticsearch一起使用的嵌套查询时遇到问题(如果我删除了查询字符串之一,则可以使用)。我要解决的问题是我有一个包含关闭列表的文档(关闭)。我想在条件满足另一个值的闭包列表中搜索一个值。那只是从argan = 1的闭包中获得价值 我正在得到这个错误响应; 我的映射如下所示。 有人知道我在做什么错吗? 问题答案: 您的查询不是有效的查询。您需要使用适当的复合查询将其他

  • 我在这里遵循指南https://dzone.com/articles/23-usplient-ellasticsearch-example-queries,下面的bool查询让我感到困惑: 根据教程,查询的解释是: 搜索标题中有“ellasticsearch”或“solr”字样的书,由“Clinton Gormley”撰写,但不由“Radu Gheorge”撰写 我的问题是,bool查询中有3个条

  • 我使用bool查询的必须和must_not功能。它像预期的那样工作,直到我得到一个3级深的属性。 工作正常并返回结果,但如果我在bool查询中执行相同的操作,如。。 不返回任何结果。这是为什么?????记住其他bool查询,例如。。 干得很好!它只适用于任何3级深的字段,因此搜索任何1级深的术语。建议3。建议3

  • 问题内容: 我想使用ES进行图书搜索。因此,我决定将作者姓名和标题(作为嵌套文档)放入索引,如下所示: 我不明白的是:如何构造搜索查询,以便在搜索“一二”时仅找到第二本书,而在搜索“二三”时什么也找不到,而在搜索“一”时所有图书呢? 问题答案: 也许是这样的? 该查询基本上说一个文件必须有and 。您可以轻松地重新配置该查询。例如,如果您只想搜索作者,请删除嵌套部分。如果您想要另一本书,请更改嵌套

  • 我试图创建一个嵌套查询,它将过滤掉一些带有特定术语的文档。在本例中,我试图过滤掉在user.first中有匹配术语的文档。数据示例: 我的查询没有得到所需的结果,因为它返回给我所有未筛选的记录。我尝试使用: 我希望这里得到与过滤器不匹配的文档。在这种情况下,它应该只返回第二个文档。做这件事的正确方法是什么?