Elasticsearch 自定义分析器不工作

标签 elasticsearch analyzer

我正在使用 elasticsearch 作为我的搜索引擎,我现在正在尝试创建一个自定义分析器来使字段值变为小写。以下是我的代码:

创建索引和映射

create index with a custom analyzer named test_lowercase:

curl -XPUT 'localhost:9200/test/' -d '{
  "settings": {
    "analysis": {
      "analyzer": {
        "test_lowercase": {
          "type": "pattern",
          "pattern": "^.*$"
        }
      }
    }
  }
}'

create a mapping using the test_lowercase analyzer for the address field:

curl -XPUT 'localhost:9200/test/_mapping/Users' -d '{
  "Users": {
    "properties": {
      "name": {
        "type": "string"
      },
      "address": {
        "type": "string",
        "analyzer": "test_lowercase"
      }
    }
  }
}'

验证 test_lowercase 分析器是否工作:

curl -XGET 'localhost:9200/test/_analyze?analyzer=test_lowercase&pretty' -d '
Beijing China
'
{
  "tokens" : [ {
    "token" : "\nbeijing china\n",
    "start_offset" : 0,
    "end_offset" : 15,
    "type" : "word",
    "position" : 0
  } ]
}

正如我们所见,字符串 'Beijing China' 被索引为单个小写的整个术语 'beijing china',因此 test_lowercase 分析器工作正常。

验证“地址”字段是否使用小写分析器:

curl -XGET 'http://localhost:9200/test/_analyze?field=address&pretty' -d '
Beijing China
'
{
  "tokens" : [ {
    "token" : "\nbeijing china\n",
    "start_offset" : 0,
    "end_offset" : 15,
    "type" : "word",
    "position" : 0
  } ]
}
curl -XGET 'http://localhost:9200/test/_analyze?field=name&pretty' -d '
Beijing China
'
{
  "tokens" : [ {
    "token" : "beijing",
    "start_offset" : 1,
    "end_offset" : 8,
    "type" : "<ALPHANUM>",
    "position" : 0
  }, {
    "token" : "china",
    "start_offset" : 9,
    "end_offset" : 14,
    "type" : "<ALPHANUM>",
    "position" : 1
  } ]
}

我们可以看到,对于同一个字符串 'Beijing China',如果我们使用 field=address 来分析,它会创建一个单独的项 'beijing china',当使用 field=name 时,我们会得到两个项 'beijing' 和“china”,所以字段地址似乎使用了我的自定义分析器“test_lowercase”。

将文档插入测试索引以查看分析器是否适用于文档

curl -XPUT 'localhost:9200/test/Users/12345?pretty' -d '{"name": "Jinshui Tang",  "address": "Beijing China"}'

很遗憾,文档已成功插入,但地址字段未被正确解析。我无法通过使用通配符查询来搜索它,如下所示:

curl -XGET 'http://localhost:9200/test/Users/_search?pretty' -d '
{
  "query": {
    "wildcard": {
      "address": "*beijing ch*"
    }
  }
}'
{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

列出为文档分析的所有术语:

所以我运行以下命令查看文档的所有术语,我发现'Beijing China'根本不在术语向量中。

curl -XGET 'http://localhost:9200/test/Users/12345/_termvector?fields=*&pretty'
{
  "_index" : "test",
  "_type" : "Users",
  "_id" : "12345",
  "_version" : 3,
  "found" : true,
  "took" : 2,
  "term_vectors" : {
    "name" : {
      "field_statistics" : {
        "sum_doc_freq" : 2,
        "doc_count" : 1,
        "sum_ttf" : 2
      },
      "terms" : {
        "jinshui" : {
          "term_freq" : 1,
          "tokens" : [ {
            "position" : 0,
            "start_offset" : 0,
            "end_offset" : 7
          } ]
        },
        "tang" : {
          "term_freq" : 1,
          "tokens" : [ {
            "position" : 1,
            "start_offset" : 8,
            "end_offset" : 12
          } ]
        }
      }
    }
  }
}

可以看到name解析正确,变成了'jinshui'和'tang'两个词,但是地址丢了。

有人可以帮忙吗?有什么遗漏吗?

非常感谢!

最佳答案

要将文本小写,您不需要模式。使用这样的东西:

PUT /test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "test_lowercase": {
          "type": "custom",
          "filter": [
            "lowercase"
          ],
          "tokenizer": "keyword"
        }
      }
    }
  }
}

PUT /test/_mapping/Users
{
  "Users": {
    "properties": {
      "name": {
        "type": "string"
      },
      "address": {
        "type": "string",
        "analyzer": "test_lowercase"
      }
    }
  }
}

PUT /test/Users/12345
{"name": "Jinshui Tang",  "address": "Beijing China"}

为了验证你做对了,使用这个:

GET /test/Users/_search
{
  "fielddata_fields": ["name", "address"]
}

您将确切地看到 Elasticsearch 如何为您的数据编制索引:

        "fields": {
           "name": [
              "jinshui",
              "tang"
           ],
           "address": [
              "beijing",
              "china"
           ]
        }

关于Elasticsearch 自定义分析器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33121343/

相关文章:

elasticsearch - 即使我的索引中没有太多数据,也超过了高磁盘水印

elasticsearch - 根据上次索引时间在查询时间提升 Elasticsearch 结果

elasticsearch - 在 Elasticsearch 中获取索引文档的倒排索引

ios - Cocos2D 库泄露?

iphone - iOS 版 Facebook API 中的分析器警告

regex - 我可以在Elasticsearch中的停止分析器的停用词中指定regexp吗?

elasticsearch - Elasticsearch将名称与首字母匹配

elasticsearch - 如何在 Elasticsearch 中提升特定术语?

elasticsearch - Elasticsearch:对_all查询有效,但对特定字段不查询

memory - 如何分析堆转储