【问题标题】:How do you filter on not null values elasticsearch?你如何过滤非空值弹性搜索?
【发布时间】:2020-07-18 09:00:37
【问题描述】:

我正在尝试过滤掉不为空的值:

用sql举例

SELECT ALL FROM Mytable WHERE field_1 NOT NULL and field_2 ="alpha"

我应该如何在 elasticsearch-dsl(python) 中编写这个查询?

我尝试过这样的事情:

s = Mytable.search().query(
Q('match', field_2 ='alpha')
).filter(~Q('missing', field='field_1'))

但它返回 field_1 为空值的元素

我也试过了,还是不行

field_name_1 = 'field_2'
value_1 = "alpha"
field_name_2 = 'field_1'
value_2 = " "
filter = { 
    "query": {
        "bool": {
        "must": [
            {
            "match": {
                field_name_1 : value_1
            }
            },
            {
            "bool": {
                "should": [
                    {
                    "bool": {
                        "must_not": [
                            {
                                field_name_2: {
                                    "textContent": "*"
                                }
                            }
                        ]
                    } }
                ]
            }
            }
        ]
        }
    }
    }
  

【问题讨论】:

    标签: python elasticsearch elasticsearch-dsl


    【解决方案1】:

    我不熟悉 elasticsearch-dsl(python),但是下面的搜索查询会得到你想要的相同搜索结果:

    SELECT ALL FROM Mytable WHERE field_1 NOT NULL and field_2 ="alpha"
    

    在下面的搜索查询的帮助下,搜索结果将是name="alpha" AND cost 字段将not be null。您可以参考exists query 了解更多信息。

    索引数据:

     {  "name": "alpha","item": null }
     {  "name": "beta","item": null  }
     {  "name": "alpha","item": 1    }
     {  "name": "alpha","item": []   }
    

    搜索查询:

    您可以像这样将布尔查询与存在查询结合起来:

        {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "name": "alpha"
              }
            },
            {
              "exists": {
                "field": "item"
              }
            }
          ]
        }
      }
    }
    

    搜索结果:

    "hits": [
      {
        "_index": "my-index",
        "_type": "_doc",
        "_id": "4",
        "_score": 1.6931472,
        "_source": {
          "name": "alpha",
          "item": 1
        }
      }
    ]
    

    【讨论】:

    • @Xoshabi 你有机会看看我的回答吗,期待你的反馈
    • 谢谢@Bhavya;我发现我的数据库是空的,所以它没有返回任何东西。
    • @Xoshabi 感谢您接受答案 :) 您也可以通过单击答案左侧的向上箭头来对此表示赞同 :)
    【解决方案2】:

    你可以试试这个:

    s = Mytable.search()
    .query(Q('bool', must=[Q('match', field_2='alpha'), Q('exists', field='field_1')]))
    

    这是boolean compound query的使用方式

    【讨论】:

      【解决方案3】:
                          query: {
                            bool: {
                              must: [
                                {
                                  bool: {
                                    must_not: {
                                      missing: {
                                        field: 'follower',
                                        existence: true,
                                        null_value: true,
                                      },
                                    },
                                  },
                                },
                                {
                                  nested: {
                                    path: 'follower',
                                    query: {
                                      match: {
                                        'follower.id': req.currentUser?.id,
                                      },
                                    },
                                  },
                                },
                              ],
                            },
                          },
      

      【讨论】:

        猜你喜欢
        • 2015-11-20
        • 1970-01-01
        • 2018-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-02
        • 2017-04-29
        相关资源
        最近更新 更多