gpt4 book ai didi

elasticsearch - elasticsearch将 “and filter”与 “bool filter”混合

转载 作者:行者123 更新时间:2023-12-03 02:04:41 30 4
gpt4 key购买 nike

我在Elasticsearch上工作,我尝试混合两个工作查询。第一个与“和过滤器”,第二个与“ bool(boolean) 过滤器”,但我失败了。

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

  • “和过滤器”:

  • 我需要“和过滤器”来查询数据,例如,字段必须等于“非洲”或“亚洲”或为空。这是工作查询的示例:
    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
    }
    }

    }
  • 第二个查询:

  • 现在,我需要使用“D_TYPESTATUS”字段来限制结果数据,该字段必须与值“type”不同并且不能为null。

    这个查询工作是为了做到这一点:
    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(boolean) 过滤器”?
    非常感谢

    最佳答案

    我一定想念一些东西,因为这很容易:

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

    关于elasticsearch - elasticsearch将 “and filter”与 “bool filter”混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28048247/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com