Elasticsearch自定义分析器配置
问题描述:
我自己编写了一个弹性分析器,但配置分析器时遇到了一些问题。Elasticsearch自定义分析器配置
我安装了我的分析仪bin/plugin --url file:///[path_to_thulac.jar] --install analysis-smartcn
(基于smartcn,所以它的名字是smartcn)。并通过
curl -XPUT 'http://localhost:9200/about-index/_mapping/about' -d '
{
"properties": {
"searchable_text": {
"type": "string",
"analyzer": "smartcn"
}
}
}'
当我打电话curl -XGET 'localhost:9200/_analyze?analyzer=smartcn&pretty' -d '心理学概论'
,我得到了配置映射“心理学” &“概论”,它是我想要的答案。
但是当我打电话搜索API
curl 'http://localhost:9200/title-index/_search?pretty=true' -d '{
"query" : {
"query_string": {
"default_field": "searchable_text",
"query": "心理",
"analyzer": "smartcn"
}
},
"script_fields": {
"terms" : {
"script": "doc[field].values",
"params": {
"field": "searchable_text"
}
}
}
}'
我terms: ["2014", "心理", "概论", "理学", "秋"]
我对这个问题很困惑,有人可以告诉我为什么?谢谢。
答
您的映射设置不正确。使用适当的设置映射,该记录甚至不应该由您的查询返回。如果你申请的分析如下图所示的例子:
curl -XDELETE "localhost:9200/test-idx?pretty"
curl -XPUT "localhost:9200/test-idx?pretty" -d '{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
}
},
"mappings": {
"doc": {
"properties": {
"searchable_text": { "type": "string", "analyzer": "smartcn" }
}
}
}
}
'
curl -XPUT "localhost:9200/test-idx/doc/1?pretty" -d '{
"searchable_text": "心理学概论2014秋"
}'
curl -XPOST "localhost:9200/test-idx/_refresh?pretty"
以下搜索请求
curl "localhost:9200/test-idx/_search?pretty=true" -d '{
"query" : {
"query_string": {
"default_field": "searchable_text",
"query": "心理学"
}
},
"script_fields": {
"terms" : {
"script": "doc[field].values",
"params": {
"field": "searchable_text"
}
}
}
}
'
将返回:
"fields" : {
"terms" : [ [ "2014", "心理学", "概论", "秋" ] ]
}
你从分析仪相同的结果,以及:
curl -XGET 'localhost:9200/test-idx/_analyze?field=doc.searchable_text&pretty' -d '心理学概论2014秋'
{
"tokens" : [ {
"token" : "心理学",
"start_offset" : 0,
"end_offset" : 3,
"type" : "word",
"position" : 1
}, {
"token" : "概论",
"start_offset" : 3,
"end_offset" : 5,
"type" : "word",
"position" : 2
}, {
"token" : "2014",
"start_offset" : 5,
"end_offset" : 9,
"type" : "word",
"position" : 3
}, {
"token" : "秋",
"start_offset" : 9,
"end_offset" : 10,
"type" : "word",
"position" : 4
} ]
}
执行以下命令,以确保您的映射正确应用:
curl -XGET 'http://localhost:9200/about-index/_mapping'
文档的searchable_text字段是“心理学概论2014秋”,我认为令牌是由分词器分割后的结果。 – dreamszl 2015-03-20 05:27:27
@dreamszl看起来映射没有正确应用,我已经更新了我的答案。 – imotov 2015-03-20 16:34:32