【问题标题】:Elasticsearch nested object query_stringElasticsearch 嵌套对象 query_string
【发布时间】:2016-12-05 16:11:25
【问题描述】:

我对 ElasticSearch 中的 query_string 查询有疑问。我想对索引中的所有类型和字段创建全文搜索。 query_string 字符串是否针对嵌套对象执行?例如我有这个映射

 {
  "my_index": {
    "mappings": {
      "my_type": {
        "properties": {
          "group": {
            "type": "string"
          },
          "user": {
            "type": "nested",
            "properties": {
              "first": {
                "type": "string"
              },
              "last": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  }
}

还有查询

GET /my_index/_search
{
  "query": {
      "query_string" : {
          "query" : "paul"
      }
    }

} 

所以当我调用查询时,ES 是否会搜索所有字段,包括嵌套或仅在 my_type 对象中,而对于嵌套搜索,我将不得不使用嵌套查询?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您不能从根的 query_string 引用嵌套字段。即这行不通:

    {
        "query": {
             "query_string": {
              "query": "myNestedObj.myTextField:food"
             }
       }
    }
    

    要在特定的嵌套字段中搜索,必须使用嵌套子句:

    {
        "query": {
          "nested": {
            "path": "myNestedObj",
            "query": {
             "query_string": {
              "query": "myNestedObj.myTextField:food"
             }
            }
          }
        }
      }
    }
    

    但是,我发现伪字段“_all”确实包含嵌套字段,因此此查询将在 myNestedObj.myTextField(以及其他任何地方)中找到包含“食物”的文档

    {
        "query": {
             "query_string": {
              "query": "_all:food"
             }
       }
    }
    

    【讨论】:

    • 感谢@anthonybruni,“但是,我发现伪字段“_all”确实包含嵌套字段,因此此查询将在 myNestedObj.myTextField 中找到包含“食物”的文档(以及其他任何地方)”这个答案正是我所要求的
    【解决方案2】:

    试试:

    GET my_index/_search?q=paul
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      相关资源
      最近更新 更多