elasticSearch nested exist與missing查詢


elasticSearch nested查詢,簡單意義上,你可以理解為,它不會被索引,只是被暫時隱藏起來,而查詢的時候,開關就是使用nested query/filter去查詢

下面我有一個例子,是查詢文檔中,含有某字段的nested查詢,與不含有某字段的nested查詢辦法。

1.查詢文檔中存在某字段(account.userId)的nested

ES查詢語句

核心


{
              "query": {
                "nested": {
                  "path": "account",
                  "query": {
                    "match_all": {}
                  },
                  "filter": {
                    "exists": {
                      "field": "account.userId"
                    }
                  }
                }
              }
}

java查詢語句


//構建Nested查詢
NestedFilterBuilder nfb = FilterBuilders.nestedFilter("account", QueryBuilders.filteredQuery(
							QueryBuilders.matchAllQuery(), FilterBuilders.existsFilter("account.userId")));
					 

nfb嵌入到原有的查詢query中即可

完整查詢



{
  "query" : {
    "filtered" : {
      "query" : {
        "match_all" : { }
      },
      "filter" : {
        "and" : {
          "filters" : [ {
            "range" : {
              "oppType" : {
                "from" : 20,
                "to" : null,
                "include_lower" : true,
                "include_upper" : true
              }
            }
          }, {
            "term" : {
              "city" : 1
            }
          }, {
            "range" : {
              "status" : {
                "from" : 0,
                "to" : 12,
                "include_lower" : true,
                "include_upper" : false
              }
            }
          }, {
            "nested" : {
              "query" : {
                "filtered" : {
                  "query" : {
                    "match_all" : { }
                  },
                  "filter" : {
                    "exists" : {
                      "field" : "ajkAccount.ajkUserId"
                    }
                  }
                }
              },
              "path" : "ajkAccount"
            }
          }, {
            "or" : {
              "filters" : [ {
                "term" : {
                  "saleId" : "xxx"
                }
              }, {
                "terms" : {
                  "deptId" : [ "dept001" ]
                }
              } ]
            }
          } ]
        }
      }
    }
  }
}

curl -XPOST 'http://192.168.1.xx:9200/xxxIndex/xxxEntry/_search' '{"query":{"filtered":{"query":{"match_all":{}},"filter":{"and":{"filters":[{"range":{"oppType":{"from":null,"to":20,"include_lower":true,"include_upper":false}}},{"term":{"city":1}},{"range":{"status":{"from":0,"to":11,"include_lower":true,"include_upper":true}}},{"query":{"nested":{"path":"account","query":{"match_all":{}},"filter":{"exists":{"field":"account.userId"}}}}}]}}}}}'

2.查詢文檔中不存在某字段(missing account.userId)的nested

ES查詢語句

核心


{
          "not":{
            "nested" : {
              "query" : {
                "filtered" : {
                  "query" : {
                    "match_all" : { }
                  },
                  "filter" : {
                    "exists" : {
                      "field" : "account.userId"
                    }
                  }
                }
              },
              "path" : "account"
            }
            }
 }

java代碼


//構建Nested查詢
NestedFilterBuilder nfb = FilterBuilders.nestedFilter("account", QueryBuilders.filteredQuery(
							QueryBuilders.matchAllQuery(), FilterBuilders.existsFilter("account.userId")));

//添加Not
FilterBuilders.notFilter(nfb);

完整ES查詢


{
  "query" : {
    "filtered" : {
      "query" : {
        "match_all" : { }
      },
      "filter" : {
        "and" : {
          "filters" : [ {
            "range" : {
              "oppType" : {
                "from" : 20,
                "to" : null,
                "include_lower" : true,
                "include_upper" : true
              }
            }
          }, {
            "term" : {
              "city" : 1
            }
          }, {
            "range" : {
              "status" : {
                "from" : 0,
                "to" : 12,
                "include_lower" : true,
                "include_upper" : false
              }
            }
          }, {
            "not" : {
              "filter" : {
                "nested" : {
                  "query" : {
                    "filtered" : {
                      "query" : {
                        "match_all" : { }
                      },
                      "filter" : {
                        "exists" : {
                          "field" : "account.userId"
                        }
                      }
                    }
                  },
                  "path" : "account"
                }
              }
            }
          }, {
            "or" : {
              "filters" : [ {
                "term" : {
                  "saleId" : "xxxxx"
                }
              }, {
                "terms" : {
                  "deptId" : [ "dept01" ]
                }
              } ]
            }
          } ]
        }
      }
    }
  }
}


curl -XPOST 'http://192.168.1.xx:9200/xxxIndex/xxxEntry/_search' '{"query":{"filtered":{"query":{"match_all":{}},"filter":{"and":{"filters":[{"range":{"oppType":{"from":20,"to":null,"include_lower":true,"include_upper":true}}},{"term":{"city":1}},{"range":{"status":{"from":0,"to":12,"include_lower":true,"include_upper":false}}},{"not":{"filter":{"nested":{"query":{"filtered":{"query":{"match_all":{}},"filter":{"exists":{"field":"account.userId"}}}},"path":"account"}}}},{"or":{"filters":[{"term":{"saleId":"xxxxx"}},{"terms":{"deptId":["dept01"]}}]}}]}}}}}'

參考
http://grokbase.com/t/gg/elasticsearch/13a49a2hmq/check-if-field-exists-in-a-nested-object
https://github.com/elastic/elasticsearch/issues/3495 `


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM