简体   繁体   English

elasticsearch嵌套文档查询

[英]elasticsearch nested document query

I'm new to elasticsearch, well have some idea on how to go about doing filters, queries, and aggregation but not sure how to solve this following problem below. 我是Elasticsearch的新手,对如何进行过滤器,查询和聚合有一些想法,但不确定如何解决以下以下问题。 I'm want to be able to query only the most recent deliveries (date and crate_quantity) of companies from the document shown below. 我希望能够从下面显示的文档中仅查询公司的最新交货(日期和crate_quantity)。 I'm not sure how to go about doing it. 我不确定如何去做。 Is there a way to use max aggregation to pull only the most recent deliveries from each document? 有没有一种使用最大聚合的方法来仅提取每个文档中的最新交货?

POST /sanfrancisco/devlivery
{
"company1": {
    "delivery": [
        {
            "date": "01/01/2013",
            "crate_quantity": 5
        },
        {
            "date": "01/12/2013",
            "crate_quantity": 3
        },
        {
            "date": "01/24/2013",
            "crate_quantity": 2
        }
    ]
}
}

POST /sanfrancisco/devlivery
{
"company2": {
    "delivery": [
        {
            "date": "01/01/2015",
            "crate_quantity": 14
        },
        {
            "date": "12/31/2014",
            "crate_quantity": 20
        },
        {
            "date": "11/24/2014",
            "crate_quantity": 13
        }
    ]
}
}

If you want the latest delivery for one company at a time, I would probably set it up using a parent/child relationship. 如果您想一次为一家公司提供最新交货,我可能会使用父子关系进行设置。 I used company as the parent and delivery as the child. 我以company为父母,以delivery为孩子。

I also added a custom date format so that your dates will be sorted the way you're expecting. 我还添加了自定义日期格式,以便您的日期可以按照您期望的方式排序。

I set up the index like this: 我这样设置索引:

DELETE /test_index

PUT /test_index
{
   "settings": {
      "number_of_shards": 1,
      "number_of_replicas": 0
   },
   "mappings": {
      "company": {
         "properties": {
            "name": {
               "type": "string",
               "index": "not_analyzed"
            }
         }
      },
      "delivery": {
         "_parent": {
            "type": "company"
         },
         "properties": {
            "crate_quantity": {
               "type": "long"
            },
            "date": {
               "type": "date",
               "format": "MM/dd/yyyy"
            }
         }
      }
   }
}

then indexed the documents using the bulk api : 然后使用批量api将文档编入索引:

PUT /test_index/_bulk
{"index": {"_index":"test_index", "_type":"company", "_id":1}}
{"name":"company1"}
{"index": {"_index":"test_index", "_type":"delivery", "_id":1, "_parent":1}}
{"date": "01/01/2013", "crate_quantity": 5}
{"index": {"_index":"test_index", "_type":"delivery", "_id":2, "_parent":1}}
{"date": "01/12/2013", "crate_quantity": 3}
{"index": {"_index":"test_index", "_type":"delivery", "_id":3, "_parent":1}}
{"date": "01/24/2013",  "crate_quantity": 2}
{"index": {"_index":"test_index", "_type":"company", "_id":2}}
{"name":"company2"}
{"index": {"_index":"test_index", "_type":"delivery", "_id":4, "_parent":2}}
{"date": "01/01/2015", "crate_quantity": 14}
{"index": {"_index":"test_index", "_type":"delivery", "_id":5, "_parent":2}}
{"date": "12/31/2014",  "crate_quantity": 20}
{"index": {"_index":"test_index", "_type":"delivery", "_id":6, "_parent":2}}
{"date": "11/24/2014",  "crate_quantity": 13 }

Now I can query for the latest delivery for a particular company by using a has_parent filter , sorting on date, and only accepting a single result, as follows: 现在,我可以通过使用has_parent过滤器 ,按日期排序并仅接受单个结果来查询特定公司的最新交货,如下所示:

POST /test_index/delivery/_search
{
   "size": 1,
   "sort": [
      {
         "date": {
            "order": "desc"
         }
      }
   ],
   "filter": {
      "has_parent": {
         "type": "company",
         "query": {
            "term": {
               "name": {
                  "value": "company1"
               }
            }
         }
      }
   }
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": null,
      "hits": [
         {
            "_index": "test_index",
            "_type": "delivery",
            "_id": "3",
            "_score": null,
            "_source": {
               "date": "01/24/2013",
               "crate_quantity": 2
            },
            "sort": [
               1358985600000
            ]
         }
      ]
   }
}

Here is the code I used while experimenting with this: 这是我尝试此操作时使用的代码:

http://sense.qbox.io/gist/c519b0654448c8b7b0c7c85d613f1e88c0ad1d19 http://sense.qbox.io/gist/c519b0654448c8b7b0c7c85d613f1e88c0ad1d19

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

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