【问题标题】:Elasticsearch DSL query for both nested field and normal field嵌套字段和普通字段的 Elasticsearch DSL 查询
【发布时间】:2020-10-10 06:11:57
【问题描述】:

我需要帮助来编写查询以过滤具有以下结构的文档

{
"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

我也在检查未映射测试字段但文档具有此字段的映射文档

【问题讨论】:

  • 您希望对您提到的字段应用什么样的过滤器?能具体一点吗

标签: elasticsearch elasticsearch-query elasticsearch-nested


【解决方案1】:

我想过滤 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"
                        }
                    ]
                }
            }
        ]

您可以参考这篇文章了解更多关于Query-filter context和Nested Query filter的信息

【讨论】:

  • @Ashu 你有没有机会仔细阅读我的回答,期待得到你的反馈,如果有帮助,请不要忘记点赞并接受:)
猜你喜欢
  • 1970-01-01
  • 2021-02-11
  • 2017-10-10
  • 2021-11-15
  • 2020-09-04
  • 2020-02-15
  • 1970-01-01
  • 2021-07-08
  • 2018-10-17
相关资源
最近更新 更多