簡體   English   中英

或在嵌套對象ElasticSearch中查詢

[英]OR query in nested objects ElasticSearch

我使用ElasticSearch版本1.7.5,並且嘗試獲取缺少某些字段的所有文檔。

我的映射:

...
"participant": {
    "properties": {
        "id": {
            "type": "string"
        },
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        },
        "name": {
            "type": "string"
        }
},
"coordinator": {
    "properties": {
        "id": {
            "type": "string"
        },
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        },
        "name": {
            "type": "string"
        }
}
...

我想查詢尚未分配c oordinator.id或participant.id的所有文檔。

我的查詢看起來像:

"query": {
    "nested": {
        "path": "coordinator, participant",
        "query": {
            "constant_score": {
                "filter": {
                    "or": [
                        {
                            "missing": {
                                "field": "coordinator.id"
                            }
                        },
                        {
                            "missing": {
                                "field": "participant.id"
                            }
                        },
                    ]
                }
            }
        }
    }
}

您通過布爾查詢執行OR查詢:

https://www.elastic.co/guide/en/elasticsearch/reference/1.7/query-dsl-bool-filter.html

因此,此查詢將起作用:

{
  "query": {
    "bool": {
      "should": [
        {
          "constant_score": {
            "filter": {
              "missing": {
                "field": "participant.id"
              }
            }
          }
        },
        {
          "constant_score": {
            "filter": {
              "missing": {
                "field": "coordinator.id"
              }
            }
          }
        }
      ]
    }
  }
}

我注意到您正在使用嵌套查詢,盡管該映射未指出協調器和參與者是嵌套字段類型,因此無法使用:

https://www.elastic.co/guide/en/elasticsearch/reference/1.7/mapping-nested-type.html

將某些內容設置為嵌套類型僅在您需要將搜索詞分組在一起時才有用,因此我認為您不需要。

暫無
暫無

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

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