首页 文章

Elasticsearch嵌套对象query_string

提问于
浏览
0

我对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对象中,对于嵌套搜索,我将不得不使用嵌套查询?

2 回答

  • 0

    您不能从根的query_string引用嵌套字段 . 即这不起作用:

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

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

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

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

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

    尝试:

    GET my_index/_search?q=paul
    

相关问题