简体   繁体   English

Elasticsearch - 如何查询嵌套字段?

[英]Elasticsearch - How to query nested fields?

I created an ES index with the command below:我使用以下命令创建了一个 ES 索引:

curl -XPUT -H "Content-Type: application/json" http://localhost:9200/nested_test?pretty=true -d '{"mappings": {"properties": {"name": {"type": "keyword"}, "related": {"type": "nested", "properties": {"name": {"type": "keyword"}, "count": {"type": "long"}}}}}}'

Which gave me the response:这给了我回应:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "nested_test"
}

Then I put some sample data in it:然后我在里面放了一些样本数据:

curl -XPUT -H "Content-Type: application/json" http://localhost:9200/nested_test/_doc/1?pretty=true -d '{"name": "php", "related": [{"name": "c#", "count": 6806}, {"name": "c", "count": 4080}, {"name": "java", "count": 9745}, {"name": "javascript", "count": 9141}]}'

Which gave me the response:这给了我回应:

{
  "_index" : "nested_test",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

And then I finally tried to query it using the following command:然后我终于尝试使用以下命令查询它:

curl -X GET http://localhost:9200/nested_test/_search?pretty=true -H 'Content-Type: application/json' -d '{"query": {"nested": {"path": "related", "query": {"match": {"related.name": "javascript"}}}}}'

And it gave me the following response:它给了我以下回应:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.2039728,
    "hits" : [
      {
        "_index" : "nested_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.2039728,
        "_source" : {
          "name" : "php",
          "related" : [
            {
              "name" : "c#",
              "count" : 6806
            },
            {
              "name" : "c",
              "count" : 4080
            },
            {
              "name" : "java",
              "count" : 9745
            },
            {
              "name" : "javascript",
              "count" : 9141
            }
          ]
        }
      }
    ]
  }
}

When I was expecting the following output:当我期待以下 output 时:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.2039728,
    "hits" : [
      {
        "_index" : "nested_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.2039728,
        "_source" : {
          "name" : "php",
          "related" : [
            {
              "name" : "javascript",
              "count" : 9141
            }
          ]
        }
      }
    ]
  }
}

Can anyone point out what it is that I am doing wrong?谁能指出我做错了什么?

Great start!!伟大的开始! You simply need to ask for nested inner hits only:您只需要请求嵌套的内部点击

curl -X GET http://localhost:9200/nested_test/_search?pretty=true -H 'Content-Type: application/json' -d '{
  "_source": {
     "_excludes": ["related"]
  },
  "query": {
    "nested": {
      "path": "related",
      "query": {
        "match": {
          "related.name": "javascript"
        }
      },
      "inner_hits": {}                   <--- add this
    }
  }
}'

Also note that I'm excluding the "related" field from returning in the _source document, because you'll get the inner hits you're after in the fields section (sibling to _source ).另请注意,我将“相关”字段排除在_source文档中的返回之外,因为您将在fields部分(与_source同级)中获得您所追求的内部点击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM