【发布时间】:2018-12-07 22:42:49
【问题描述】:
尝试翻译以下搜索。本质上,message1、message2 可以有空字符串值。如果 search1Value 或 search2Value 是空字符串,我不希望为 OR 条件中存在空字符串的那部分返回任何记录。
message1 != "" and message1 = search1Value
OR
message2 != "" and message2 = search2Value
所以如果索引中的可用文档示例...
id, message1, message2
1, "", "abc"
2, "", ""
3, "def", ""
4, "", "ghi"
如果 searchValue1 是空字符串,并且 searchValue2 是 abc 我要回,只录1,不录1、2、4。
这里是设置
PUT test_index
{
"mappings": {
"_doc": {
"properties": {
"message1": {
"type": "keyword",
"ignore_above": 20
},
"message2": {
"type": "keyword",
"ignore_above": 20
}
}
}
}
}
数据...
PUT test_index/_doc/1
{
"message1": "",
"message2": "abc"
}
PUT test_index/_doc/2
{
"message1": "",
"message2": ""
}
PUT test_index/_doc/3
{
"message1": "def",
"message2": ""
}
PUT test_index/_doc/4
{
"message1": "",
"message2": "ghi"
}
搜索...
GET test_index/_search
{
"query": {
"bool" : {
"should": [{
"bool": {
"must": [{
"bool": {
"must_not": [{
"term": { "message1" : "" }
}]
},
"bool": {
"must": [{
"term": { "message1" : "" }
}]
}
}]
},
"bool": {
"must": [{
"bool": {
"must_not": [{
"term": { "message2" : "" }
}]
},
"bool": {
"must": [{
"term": { "message2" : "abc" }
}]
}
}]
}
}]
}
}
}
上面的 dsl 不起作用...似乎无法让它做我想做的事。
【问题讨论】:
标签: elasticsearch dsl