【问题标题】:In elasticsearch, is it possible to use regular queries for nested objects?在elasticsearch中,是否可以对嵌套对象使用常规查询?
【发布时间】:2017-04-21 15:11:55
【问题描述】:

在我的 elasticsearch 索引中,我的文档具有这样的嵌套对象属性:

{
  "my_index": {
    "mappings": {
      "my_type": {
        "properties": {
          "nested_prop": {
            "type": "nested",
            "properties": {
              "subprop1": {
                "type": "boolean"
              },
              "subprop2": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
  }
}

我现在可以使用嵌套查询搜索对象,如下所示:

{
    "query": {
        "nested": {
            "path": "nested_prop",
            "query": {
                "bool": {
                    "must": [{
                        "term": {
                            "nested_prop.subprop1": "true"
                        }
                    }, {
                        "term": {
                            "nested_prop.subprop2": "SOME_KEY"
                        }
                    }]
                }
            }
        }
    }
}

到目前为止一切顺利。我正在使用来自查询字符串的非常通用的机制构建我的弹性搜索查询。所以,我希望仍然能够使用“常规”(非嵌套)查询来查询文档,如下所示:

{
    "query": {
        "term": {
            "nested_prop.subprop1": "true"
        }
    }
}

但是,除非我将其包装到 nested 查询中,否则我只会得到空的结果。

有没有办法使用嵌套对象的简单查询?

【问题讨论】:

  • 这个答案可能会有所帮助:stackoverflow.com/questions/35353952/…
  • 谢谢,@Val,虽然我不打算使用 Lucene 查询语法,但我从您指出的答案中找到了一个 answer 链接:“需要嵌套字段具有嵌套查询/过滤器的查询,因为可以匹配多个文档,并且您需要能够指定如何将这些多个分数降低到一个分数。"

标签: elasticsearch


【解决方案1】:

目前 (ES 5.3.1) 使用 query_string 是不可能的:https://github.com/elastic/elasticsearch/issues/16551

对于其他查询,无论您对嵌套字段做什么,都需要有一个nested 查询才能使用它。

【讨论】:

  • 那么使用 query_string 和嵌套对象(链接的问题是关于什么)对于其他查询是必要的,例如普通的 term 查询(我在问什么)?
  • 我很困惑。您提到了query_string... 无论您对嵌套字段做什么,都需要有一个nested 查询才能使用它。
  • 我提到了query_string?您发布的链接是关于query_string :) 无论如何,谢谢。我对这个问题的评论中有更多信息。
  • 我以为你做到了 :-): I'm building my elasticsearch queries with a pretty general mechanism from a querystring。确实,不是query_string,我可能理解错了。
  • 抱歉,这非常不精确且具有误导性。我的意思是人们发送给我的服务的字符串,我从中构造一个弹性搜索查询。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-31
  • 2015-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
相关资源
最近更新 更多