你如何过滤非空值elasticsearch?

How do you filter on not null values elasticsearch?

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

例如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'))

但是它 returns 个空值为 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": "*"
                                }
                            }
                        ]
                    } }
                ]
            }
            }
        ]
        }
    }
    }
  

我不熟悉 elasticsearch-dsl(python),但以下搜索查询将为您提供所需的相同搜索结果:

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

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

索引数据:

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

搜索查询:

您可以像这样将 bool 查询与 exists 查询结合起来:

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

你可以试试这个:

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

这是使用方法boolean compound query

                    query: {
                      bool: {
                        must: [
                          {
                            bool: {
                              must_not: {
                                missing: {
                                  field: 'follower',
                                  existence: true,
                                  null_value: true,
                                },
                              },
                            },
                          },
                          {
                            nested: {
                              path: 'follower',
                              query: {
                                match: {
                                  'follower.id': req.currentUser?.id,
                                },
                              },
                            },
                          },
                        ],
                      },
                    },