【问题标题】:Elastic: Search on Must Not Empty and some other criteria弹性:搜索不得为空和其他一些条件
【发布时间】: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


    【解决方案1】:

    看起来我做得对..但括号错了..

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      • 2018-04-14
      • 2023-03-31
      • 2016-03-02
      • 1970-01-01
      • 2020-10-19
      相关资源
      最近更新 更多