elasticsearch - Elasticsearch Analyzer的前4个字符和后4个字符

标签 elasticsearch query-analyzer

使用Elasticsearch,我想指定一个搜索分析器,其中前4个字符和后4个字符被标记。
For example: supercalifragilisticexpialidocious => ["supe", "ious"]
我有一个ngram如下

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "ngram",
          "min_gram": 4,
          "max_gram": 4
        }
      }
    }
  }
}

我正在按以下方式测试分析仪
POST my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "supercalifragilisticexpialidocious."
}

然后返回“ super ”……大量我不想要的东西,“变得很可爱”。对我来说,问题是,如何仅从上面指定的ngram token 生成器获取第一个和最后一个结果?
{
  "tokens": [
    {
      "token": "supe",
      "start_offset": 0,
      "end_offset": 4,
      "type": "word",
      "position": 0
    },
    {
      "token": "uper",
      "start_offset": 1,
      "end_offset": 5,
      "type": "word",
      "position": 1
    },
...
    {
      "token": "ciou",
      "start_offset": 29,
      "end_offset": 33,
      "type": "word",
      "position": 29
    },
    {
      "token": "ious",
      "start_offset": 30,
      "end_offset": 34,
      "type": "word",
      "position": 30
    },
    {
      "token": "ous.",
      "start_offset": 31,
      "end_offset": 35,
      "type": "word",
      "position": 31
    }
  ]
}

最佳答案

实现此目的的一种方法是利用 pattern_capture token filter并采用前4个和后4个字符。

首先,如下定义索引:

PUT my_index
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "my_analyzer": {
            "type": "custom",
            "tokenizer": "keyword",
            "filter": [
              "lowercase",
              "first_last_four"
            ]
          }
        },
        "filter": {
          "first_last_four": {
            "type": "pattern_capture",
            "preserve_original": false,
            "patterns": [
              """(\w{4}).*(\w{4})"""
            ]
          }
        }
      }
    }
  }
}

然后,您可以测试新的自定义分析器:
POST test/_analyze
{
  "text": "supercalifragilisticexpialidocious",
  "analyzer": "my_analyzer"
}

并查看您期望的 token 是否存在:
{
  "tokens" : [
    {
      "token" : "supe",
      "start_offset" : 0,
      "end_offset" : 34,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "ious",
      "start_offset" : 0,
      "end_offset" : 34,
      "type" : "word",
      "position" : 0
    }
  ]
}

关于elasticsearch - Elasticsearch Analyzer的前4个字符和后4个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57706361/

相关文章:

postgresql - 在数据库中搜索(相似)字符串的可扩展方式

spring - 如何在 Java API 中使用 Analyzer 和两个参数执行 Elasticsearch 词查询

elasticsearch - ElasticSearch search_analyzer已应用,但未返回结果

elasticsearch - Elasticsearch-在一起键入的单词中检测单词

amazon-web-services - 与低级 Rest API 的 SSL/HTTPS 连接?

c# - Elasticsearch,如何让NEST map response to class

elasticsearch - 如何使Elasticsearch忽略某些查询之间的空格?

postgresql - 在处理文本搜索和地理空间数据时如何影响 Postgres 查询分析器

sql-server - 在 SQL Server 中打印文本字段内容的最简单方法

sql - 逻辑读取是如何计算的?