簡體   English   中英

Elasticsearch DSL 查詢嵌套字段和普通字段

[英]Elasticsearch DSL query for both nested field and normal field

我需要幫助來編寫查詢以過濾具有以下結構的文檔

{
"enviID" : 123,
"empID" : 456,
"projects" : [{"id": 123, "name":"abc"},{"id": 456, "name":"xyz"}],
tests : [{"id": 999, "name":"xxx"},{"id": 000, "name":"yyy"}]
}

我要過濾

envId, empId, project.Id, tests.id

我也在檢查未映射測試字段但文檔具有此字段的映射文檔

我想過濾 envId、empId、project.Id、tests.id

由於您尚未指定要應用於字段的任何特定過濾器,因此下面顯示的搜索查詢會根據文檔的Id過濾文檔

映射:

{
  "mappings": {
    "properties": {
      "enviID": {
        "type": "integer"
      },
      "empID": {
        "type": "integer"
      },
      "projects": {
        "type": "nested",
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "text"
          }
        }
      },
      "tests": {
        "type": "nested",
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "text"
          }
        }
      }
    }
  }
}

搜索查詢

{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "enviID": {
                    "value": "123"
                  }
                }
              },
              {
                "term": {
                  "empID": {
                    "value": "456"
                  }
                }
              },
              {
                "nested": {
                  "path": "projects",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "match": {
                            "projects.id": 123
                          }
                        }
                      ]
                    }
                  }
                }
              },
              {
                "nested": {
                  "path": "tests",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "match": {
                            "tests.id": 999
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

搜索結果

"hits": [
            {
                "_index": "my_index",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.0,
                "_source": {
                    "enviID": 123,
                    "empID": 456,
                    "projects": [
                        {
                            "id": 123,
                            "name": "abc"
                        },
                        {
                            "id": 456,
                            "name": "xyz"
                        }
                    ],
                    "tests": [
                        {
                            "id": 999,
                            "name": "xxx"
                        },
                        {
                            "id": 0,
                            "name": "yyy"
                        }
                    ]
                }
            }
        ]

您可以參考此內容以了解有關查詢過濾器上下文嵌套查詢過濾器的更多信息

暫無
暫無

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

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