簡體   English   中英

索引中數組的彈性搜索查詢

[英]Elastic Search query for array in index

我有索引(默認架構),其中包含字段“人員”,此字段是一個數組。

在索引中有兩個對象:

{"took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.8630463,
    "hits": [
      {
        "_index": "warehouse",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.8630463,
        "_source": {
          "order": 2,
          "status": "done",
          "personnel": [
            {
              "name": "mike",
              "function": "packer"
            },
            {
              "name": "henry",
              "function": "checker"
            } ] }
      },
      {
        "_index": "warehouse",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.8630463,
        "_source": {
          "order": 1,
          "status": "done",
          "personnel": [
            {
              "name": "jon",
              "function": "packer"
            },
            {
              "name": "mike",
              "function": "checker"
            }
 ] } } ] } }

我想查詢它以獲得狀態已完成的訂單 ,並且打包此訂單的人是邁克。

我很多疑問,但我總是得到兩個訂單,因為mike存在於兩個訂單中(女巫功能不同)

示例查詢:

{"query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "status": "done"
                    }
                },
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "personnel.name": "mike"
                                }
                            },
                            {
                                "term": {
                                    "personnel.function": "packer"
                                }
            } ] } } ] } } }

我的問題是如何准備查詢只返回一個訂單,其中邁克是打包機

好吧,我的回答只是猜測,因為你沒有提供你遇到的問題的描述。 但是您使用嵌套部分查詢應該是這樣的:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "status": "done"
                    }
                },
                {
                    "nested": {
                        "path": "personnel",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "term": {
                                            "personnel.name": "mike"
                                        }
                                    },
                                    {
                                        "term": {
                                            "personnel.function": "packer"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM