首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在弹性搜索中创建和添加自定义分析器?

如何在弹性搜索中创建和添加自定义分析器?
EN

Stack Overflow用户
提问于 2016-01-24 22:40:35
回答 1查看 139关注 0票数 0

我的ES中有一批“智能手机”产品,我需要使用“智能手机”文本来查询它们。因此,我正在研究复合词标记过滤器。具体来说,我计划使用这样的自定义过滤器:

代码语言:javascript
运行
复制
curl -XPUT 'localhost:9200/_all/_settings -d '
{
  "analysis" : {
    "analyzer":{
      "second":{
        "type":"custom",
        "tokenizer":"standard",
        "filter":["myFilter"]
      }
      "filter": {
        "myFilter" :{
             "type" : "dictionary_decompounder"
             "word_list": ["smart", "phone"]
             }
             }             
    }
}
}
'

这是正确的做法吗?另外,我想问您如何创建自定义分析器并将其添加到ES中?我查了几个链接,但不知道怎么做。我想我是在寻找正确的语法。谢谢

编辑

我正在运行1.4.5版本。我验证了自定义分析器是成功添加的:

代码语言:javascript
运行
复制
{
  "test_index" : {
    "settings" : {
      "index" : {
        "creation_date" : "1453761455612",
        "analysis" : {
          "filter" : {
            "myFilter" : {
              "type" : "dictionary_decompounder",
              "word_list" : [ "smart", "phone" ]
            }
          },
          "analyzer" : {
            "second" : {
              "type" : "custom",
              "filter" : [ "lowercase", "myFilter" ],
              "tokenizer" : "standard"
            }
          }
        },
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "version" : {
          "created" : "1040599"
        },
        "uuid" : "xooKEdMBR260dnWYGN_ZQA"
      }
    }
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-24 23:13:25

你的方法看起来不错,我也会考虑加入小写令牌过滤器,这样即使是智能手机(注意大写“S”)也会被分成智能手机和手机。

然后你可以用这样的分析器创建索引,

代码语言:javascript
运行
复制
curl -XPUT 'localhost:9200/your_index -d '
{
  "settings": {
    "analysis": {
      "analyzer": {
        "second": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "myFilter"
          ]
        }
      },
      "filter": {
        "myFilter": {
          "type": "dictionary_decompounder",
          "word_list": [
            "smart",
            "phone"
          ]
        }
      }
    }
  },
  "mappings": {
    "my_type": {
      "properties": {
        "name": {
          "type": "string",
          "analyzer": "second"
        }
      }
    }
  }
}
'

在这里,您将创建名为your_index的索引,custom analyzer命名为second,并将其应用于name字段。

您可以检查分析器是否像预期的那样与分析api一起工作

代码语言:javascript
运行
复制
curl -XGET 'localhost:9200/your_index/_analyze' -d '
{
  "analyzer" : "second",
  "text" : "LG Android smartphone"
}'

希望这能帮上忙!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34982445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档