zoukankan      html  css  js  c++  java
  • 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 `

  • 相关阅读:
    看了前辈缠中说禅及其反响,忍不住想说些东西
    利弗莫尔的操盘精华篇
    缠中说禅:教你炒股票108课(转载)
    评温斯坦的炒股书(非常重要,常看看)
    本散女2
    使用PHP-GTK编写一个windows桌面应用程序
    php.exe php-cgi.exe php-win.exe的区别
    php调试利器之phpdbg
    yaf框架安装配置
    phalcon框架安装
  • 原文地址:https://www.cnblogs.com/ae6623/p/4955814.html
Copyright © 2011-2022 走看看