Kibana 查询字符串长度

问题描述 投票:0回答:5

Kibana中有没有办法查询一定长度的值?

例如,给定以下两个 KV 对:

key: "some"
key: "something"

我想搜索 key.length > 5 并仅检索“某物”。

我看到的另一个选项是从logstash 添加标签,但随后我将不得不重新加载几百GB。

elasticsearch kibana
5个回答
10
投票

您可以使用脚本查询在 Kibana 中执行此操作。 Kibana 中的脚本查询,有一个 key 长度超过 5 的脚本查询示例:

{
    "query": {
        "filtered": {
            "filter": {
                "script": {
                    "script": "doc['key'].getValue().length() > 5"
                }
            }
        }
    }
}

并且您还需要在elasticsearch启用脚本搜索,您需要将以下配置添加到elasticsearch.yml中:

 script.engine.groovy.inline.search: on

6
投票

您可以通过 Lucene 查询语法使用正则表达式查询来完成此操作。例如,

key:/.{6,}/
仅匹配关键字段包含 6 个或更多字符的记录。

HT 关于 Solr 的类似问题(也使用 Lucene 的查询引擎)。


4
投票

如果您可以重新索引索引或者只是创建索引,则可以创建自定义分词器,如下所示:

PUT test_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "character_analyzer": {
          "type": "custom",
          "tokenizer": "character_tokenizer"
        }
      },
      "tokenizer": {
        "character_tokenizer": {
          "type": "nGram",
          "min_gram": 1,
          "max_gram": 1
        }
      }
    }
  }, 
  "mappings": {
    "person": {
      "properties": {
        "name": { 
          "type": "text",
          "fields": {
            "keyword": { 
              "type": "keyword"
            },
            "words_count": { 
              "type": "token_count",
              "analyzer": "standard"
            },
            "length": { 
              "type": "token_count",
              "analyzer": "character_analyzer"
            }
          }
        }
      }
    }
  }
}

PUT test_index/person/1
{
  "name": "John Smith"
}

PUT test_index/person/2
{
  "name": "Rachel Alice Williams"
}

GET test_index/person/_search
{
  "query": {
    "term": {
      "name.length": 10
    }
  }
}

3
投票

您可以通过直接在 Kibana 中创建脚本字段来完成此操作。

  • 在 Kibana 中,单击“设置”选项卡,然后单击您的索引模式

  • 您应该看到 2 个选项卡“字段”和“脚本字段”。

  • 单击“脚本化字段”选项卡。然后“添加脚本字段”。

  • 在脚本字段中输入“名称”并输入以下内容:-

    doc['key'].value.length > 5

  • 单击底部的“创建字段”。现在您的脚本字段将被添加并可以从“发现”页面查看。


0
投票

Consegui 解决方案:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "field1": 8854
          }
        }
      ],
      "must_not": [
        {
          "regexp": {
            "field2": {
              "value": ".{0,10}"
            }
          }
        }
      ]
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.