【问题标题】:Search Multiple Fields in Kibana /ElasticSearch在 Kibana /ElasticSearch 中搜索多个字段
【发布时间】:2020-09-03 12:11:48
【问题描述】:

在 SQL 中我有

select Column1 , column2, column3 from Table where Column4 in ['a','b','c','d']

我正在尝试在 Kibana 中实现 SQL 语句,我在编写 In 条件时面临挑战。

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "field1": "X"
          }
        },
        {
          "term": {
            "field2": "Z"
          }
        }
      ],

    }
  }
}

【问题讨论】:

    标签: elasticsearch kibana elastic-stack elk


    【解决方案1】:

    要获得上述结果,您可以使用bool 查询must 子句或带有minimum_should_match 参数的should 子句。您可以通过here 了解更多相关信息。

    为上述列创建的映射是:

    映射

    "mappings" : {
            "properties" : {
                "column1" : {
                    "type":"text"
                },
                "column2" : {
                    "type":"text"
                },
                "column3" : {
                    "type":"text"
                }, 
                "column4" : {
                    "type":"text"
                }
            }
        }
    

    您可以通过两种方式获取搜索结果:

    1. 您可以使用match 查询在must 子句中传递所有搜索值。 Match 默认使用 OR 运算符。因此,它将匹配任何column4 值与这些“a b c d”中的任何一个匹配的文档。您可以从here 了解匹配查询的默认运算符。

    查询:

        {
        "_source": [
            "column1",
            "column2",
            "column3"
        ],
        "query": {
            "match": {
                "column4": "a b c d"
            }
        }
    }
    
    1. 您也可以对每个值使用匹配查询子句。并使用minimum_should_match 参数限制为仅匹配一个。

    查询 2:

     {
         "_source": ["column1", "column2", "column3"],
        "query" : {
            "bool" : {
                "should" : [
                    {
                        "match" : {
                            "column4" : "a"
                        }
                    },
                    {
                        "match" : {
                            "column4" : "b"
                        }
                    },
                    {
                        "match" : {
                            "column4" : "c"
                        }
                    },
                    {
                        "match" : {
                            "column4" : "d"
                        }
                    }
                ],
                "minimum_should_match" : "1"
            }
        }
    }
    

    对于column4 的数据类型,您可以使用第一个查询。

    【讨论】:

    • Query1 可以是一个简单的匹配查询,而不是将其包装在 bool 查询中。
    • @KumarV 是的,我错过了那件事。谢谢你让我知道。将更新我的答案。
    【解决方案2】:

    只需将所需的值写入数组即可。

    在你的例子中,写 [a,b,c,d] 而不是 "X"

    【讨论】:

      【解决方案3】:

      我们也可以使用“query_string”查询来达到同样的效果。由于我们希望匹配同一字段上的多个值,因此 query_string 可以与设置为搜索字段的 default_field 一起使用。

      {
         "_source": ["column1", "column2", "column3"],
         "query": {
              "query_string" : {
                  "query" : "a OR b OR c OR d",
                  "default_field" : "column4"
              }
          }
      }
      

      【讨论】:

      • 虽然此代码可能提供问题的解决方案,但强烈建议您提供有关此代码为何和/或如何回答问题的附加上下文。从长远来看,只有代码的答案通常会变得毫无用处,因为未来遇到类似问题的观众无法理解解决方案背后的原因。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 2014-01-16
      • 2013-08-13
      相关资源
      最近更新 更多