python - 你如何过滤非空值弹性搜索?
问题描述
我正在尝试过滤掉不为 null 的值:
以 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": "*"
}
}
]
} }
]
}
}
]
}
}
}
解决方案
我不熟悉 elasticsearch-dsl(python),但是以下搜索查询将为您提供与您想要的相同的搜索结果:
SELECT ALL FROM Mytable WHERE field_1 NOT NULL and field_2 ="alpha"
在下面的搜索查询的帮助下,搜索结果将是name="alpha"
ANDcost
字段not be null
。您可以参考存在查询以了解更多信息。
指数数据:
{ "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
}
}
]
推荐阅读
- java - 遗传算法:什么都没有添加?
- c# - JsonConvert.DeserializeObject 无法反序列化有效的 JSON
- javascript - 使用过滤在javascript中合并多个csv文件
- sql - Oracle:ORA-00984:此处不允许列
- haxe - 使用 Lime 编译 Android 时出错 (Windows 10)
- powershell - Get-IISSite,对绑定进行排序并导出为 CSV
- python - 如何连接到列代表的信号
- prolog - 选择最佳组件的最佳方法?
- c++ - 基于信号/频谱分析的语音识别
- ruby - 在 Ruby 的 Imap 模块中删除 Gmail 邮件