弹性搜索混合 "and filter" 与 "bool filter"

elasticsearch mix "and filter" with "bool filter"

我在 elasticsearch 上工作,我尝试混合使用两个工作查询。第一个是 "and filter",第二个是 "bool filter",但我失败了。

我的查询是从用户界面动态生成的。

  1. "and filter" :

我需要 "and filter" 来查询数据,例如字段必须等于 "africa" 或 "asia" 或为空。这是工作查询的示例:

curl -XGET 'http://localhost:9200/botanique/specimens/_search?pretty' -d '
{
    "fields" : ["D_TYPESTATUS", "O_HASMEDIA"],
    "aggs" : {
        "D_TYPESTATUS_MISSING" : {
            "missing" : {
                "field" : "D_TYPESTATUS"
            }
        },
        "D_TYPESTATUS" : {
            "terms" : {
                "field" : "D_TYPESTATUS",
                "size" : 10
            }
        }
    },
    "query" : {
        "filtered" : {
            "filter" : {
                "and" : [
                    { "or" : [{
                                "term" : {
                                    "O_HASMEDIA" : "true"
                                }
                            }
                        ]
                    }, {
                        "or" : [{
                                "term" : {
                                    "T_GENUS" : "flemingia"
                                }
                            }
                        ]
                    }, {
                        "or" : [{
                                "term" : {
                                    "L_CONTINENT" : "africa"
                                }
                            }, {
                                "term" : {
                                    "L_CONTINENT" : "asia"
                                }
                            }, {
                                "missing" : {
                                    "field" : "L_CONTINENT"
                                }
                            }
                        ]
                    }, {
                        "or" : [{
                                "term" : {
                                    "I_INSTITUTIONCODE" : "mnhn"
                                }
                            }
                        ]
                    }
                ]
            }
        }
     }
}'

这个查询工作正常,这是结果:

 "hits" : {
    "total" : 1006,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "botanique",
      "_type" : "specimens",
      "_id" : "9459AB31EC354F1FAE270BDB6C22CDF7",
      "_score" : 1.0,
      "fields" : {
        "O_HASMEDIA" : [ true ],
        "D_TYPESTATUS" : "syntype"
      }
    }, 
    ....
  },
  "aggregations" : {
    "D_TYPESTATUS" : {
      "buckets" : [ {
        "key" : "syntype",
        "doc_count" : 6
      }, {
        "key" : "type",
        "doc_count" : 5
      }, {
        "key" : "isotype",
        "doc_count" : 2
      } ]
    },
    "D_TYPESTATUS_MISSING" : {
      "doc_count" : 993
    }
  }

}

  1. 第二个查询:

现在我需要用以下字段限制结果数据:"D_TYPESTATUS" 必须不同于值 "type" 并且不能为空。

此查询用于执行此操作:

curl -XGET 'http://localhost:9200/botanique/specimens/_search?size=10&pretty' -d '    {
    "fields" : ["D_TYPESTATUS", "O_HASMEDIA"],
    "aggs" : {
        "D_TYPESTATUS_MISSING" : {
            "missing" : {"field" : "D_TYPESTATUS"}
        },
        "D_TYPESTATUS" : {
            "terms" : {"field" : "D_TYPESTATUS","size" : 20}
        }
    },
    "query" : { 
        "filtered" : {
            "query" : {
                "query_string" : {  "query" : "liliaceae"  }
            },
            "filter" : {
                "bool" : {
                    "must_not" : [{
                            "term" : {
                                "D_TYPESTATUS" : "type"
                            }
                        }
                    ],
                    "must":{
                            "exists" : {
                                "field" : "D_TYPESTATUS"
                        }
                    }
                }
            }
        }
    }
}'

结果:

 {[ {
    "_index" : "botanique_tmp2",
    "_type" : "specimens",
    "_id" : "0C388B4A3186410CBA46826BA296ECBC",
    "_score" : 0.9641713,
    "fields" : {
      "D_TYPESTATUS" : [ "isotype" ],
      "O_HASMEDIA" : [ true ]
    }
  } , ... ]},
"aggregations" : {
  "D_TYPESTATUS" : {
    "buckets" : [ {
      "key" : "isotype",
      "doc_count" : 40
    }, {
      "key" : "syntype",
      "doc_count" : 37
    }, {
      "key" : "holotype",
      "doc_count" : 6
    }, {
      "key" : "paratype",
      "doc_count" : 3
    }, {
      "key" : "isonéotype",
      "doc_count" : 2
    } ]
  },
  "D_TYPESTATUS_MISSING" : {
    "doc_count" : 0
  }
}

如何将"bool filter"整合到"and filter"中?? 非常感谢

我一定是漏掉了什么,因为它很简单:

{
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "or": [
              {
                "term": {
                  "O_HASMEDIA": "true"
                }
              }
            ]
          },
          {
            "or": [
              {
                "term": {
                  "T_GENUS": "flemingia"
                }
              }
            ]
          },
          {
            "or": [
              {
                "term": {
                  "L_CONTINENT": "africa"
                }
              },
              {
                "term": {
                  "L_CONTINENT": "asia"
                }
              },
              {
                "missing": {
                  "field": "L_CONTINENT"
                }
              }
            ]
          },
          {
            "or": [
              {
                "term": {
                  "I_INSTITUTIONCODE": "mnhn"
                }
              }
            ]
          },
          {
            "bool": {
              "must_not": [
                {
                  "term": {
                    "D_TYPESTATUS": "type"
                  }
                }
              ],
              "must": {
                "exists": {
                  "field": "D_TYPESTATUS"
                }
              }
            }
          }
        ]
      }
    }
  }
}