elasticsearch phrase_prefix 预期结果
elasticsearch phrase_prefix expected results
我在使用弹性搜索时遇到一些奇怪的行为。
我正在使用带有自定义分词器的自定义分析器,它会在以下情况下泄露单词
space, + , -.
当我搜索时
{
"query": {
"match_phrase_prefix": {
"name.default": {
"query": "paris oly"
}
}
}
}
我得到了预期的结果
巴黎奥林匹亚等...
但是当我搜索时
{
"query": {
"match_phrase_prefix": {
"name.default": {
"query": "paris ol"
}
}
}
}
我没有得到任何结果。
设置:
"analysis": {
"analyzer": {
"customAnalyzer": {
"type": "custom",
"filter": "lowercase",
"tokenizer": "customTokenizer"
},
"tokenizer": {
"customTokenizer": {
"pattern": "[\+\s-]",
"type": "pattern"
}
}
}
字段映射:
{
"name": {
"properties": {
"default": {
"type": "string",
"analyzer": "customAnalyzer"
}
}
}
}
文档部分示例(请求的字段):
{
"name": {
"jp": "パリ オリンピア (劇場)",
"default": "Paris Olympia",
}
}
{
"TYPE_NAME": {
"dynamic_templates": [
{
"name": {
"path_match": "*name.*",
"match_mapping_type": "string",
"mapping": {
"type": "string",
"analyzer": "customAnalyzer"
}
}
}
],
"properties": {
"point": {
"type": "geo_point"
}
}
}
}
当我尝试测试您 post 编辑的内容时,它对我有用。我会 post 我做了什么,你可以看看它,看看你是否能找出你的设置有什么不同,如果你有进一步的问题,我会尽力提供帮助。
我使用映射创建了一个索引,analyzer/tokenizer 你 posted,然后添加了你 posted 的文档:
DELETE /test_index
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"analysis": {
"tokenizer": {
"customTokenizer": {
"pattern": "[\+\s-]",
"type": "pattern"
}
},
"analyzer": {
"customAnalyzer": {
"type": "custom",
"filter": "lowercase",
"tokenizer": "customTokenizer"
}
}
}
},
"mappings": {
"doc": {
"properties": {
"name": {
"properties": {
"default": {
"type": "string",
"analyzer": "customAnalyzer"
}
}
}
}
}
}
}
PUT /test_index/doc/1
{
"name": {
"jp": "パリ オリンピア (劇場)",
"default": "Paris Olympia"
}
}
然后您 post 的任一查询都为我返回了文档:
POST /test_index/_search
{
"query": {
"match_phrase_prefix": {
"name.default": {
"query": "paris oly"
}
}
}
}
...
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.38356602,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 0.38356602,
"_source": {
"name": {
"jp": "パリ オリンピア (劇場)",
"default": "Paris Olympia"
}
}
}
]
}
}
或
POST /test_index/_search
{
"query": {
"match_phrase_prefix": {
"name.default": {
"query": "paris ol "
}
}
}
}
...
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.38356602,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 0.38356602,
"_source": {
"name": {
"jp": "パリ オリンピア (劇場)",
"default": "Paris Olympia"
}
}
}
]
}
}
为了方便起见,这是我使用的代码:
http://sense.qbox.io/gist/4e58344580dcf01299f7cc2199d0fb7694d2a051
所以一定有其他事情发生了。你能说出你的设置和我试过的有什么不同吗?
编辑: 我确实必须切换分词器和分析器的顺序,否则会出错。所以你可能想调查一下。
我在使用弹性搜索时遇到一些奇怪的行为。 我正在使用带有自定义分词器的自定义分析器,它会在以下情况下泄露单词 space, + , -.
当我搜索时
{
"query": {
"match_phrase_prefix": {
"name.default": {
"query": "paris oly"
}
}
}
}
我得到了预期的结果 巴黎奥林匹亚等... 但是当我搜索时
{
"query": {
"match_phrase_prefix": {
"name.default": {
"query": "paris ol"
}
}
}
}
我没有得到任何结果。
设置:
"analysis": {
"analyzer": {
"customAnalyzer": {
"type": "custom",
"filter": "lowercase",
"tokenizer": "customTokenizer"
},
"tokenizer": {
"customTokenizer": {
"pattern": "[\+\s-]",
"type": "pattern"
}
}
}
字段映射:
{
"name": {
"properties": {
"default": {
"type": "string",
"analyzer": "customAnalyzer"
}
}
}
}
文档部分示例(请求的字段):
{
"name": {
"jp": "パリ オリンピア (劇場)",
"default": "Paris Olympia",
}
}
{
"TYPE_NAME": {
"dynamic_templates": [
{
"name": {
"path_match": "*name.*",
"match_mapping_type": "string",
"mapping": {
"type": "string",
"analyzer": "customAnalyzer"
}
}
}
],
"properties": {
"point": {
"type": "geo_point"
}
}
}
}
当我尝试测试您 post 编辑的内容时,它对我有用。我会 post 我做了什么,你可以看看它,看看你是否能找出你的设置有什么不同,如果你有进一步的问题,我会尽力提供帮助。
我使用映射创建了一个索引,analyzer/tokenizer 你 posted,然后添加了你 posted 的文档:
DELETE /test_index
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"analysis": {
"tokenizer": {
"customTokenizer": {
"pattern": "[\+\s-]",
"type": "pattern"
}
},
"analyzer": {
"customAnalyzer": {
"type": "custom",
"filter": "lowercase",
"tokenizer": "customTokenizer"
}
}
}
},
"mappings": {
"doc": {
"properties": {
"name": {
"properties": {
"default": {
"type": "string",
"analyzer": "customAnalyzer"
}
}
}
}
}
}
}
PUT /test_index/doc/1
{
"name": {
"jp": "パリ オリンピア (劇場)",
"default": "Paris Olympia"
}
}
然后您 post 的任一查询都为我返回了文档:
POST /test_index/_search
{
"query": {
"match_phrase_prefix": {
"name.default": {
"query": "paris oly"
}
}
}
}
...
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.38356602,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 0.38356602,
"_source": {
"name": {
"jp": "パリ オリンピア (劇場)",
"default": "Paris Olympia"
}
}
}
]
}
}
或
POST /test_index/_search
{
"query": {
"match_phrase_prefix": {
"name.default": {
"query": "paris ol "
}
}
}
}
...
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.38356602,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 0.38356602,
"_source": {
"name": {
"jp": "パリ オリンピア (劇場)",
"default": "Paris Olympia"
}
}
}
]
}
}
为了方便起见,这是我使用的代码:
http://sense.qbox.io/gist/4e58344580dcf01299f7cc2199d0fb7694d2a051
所以一定有其他事情发生了。你能说出你的设置和我试过的有什么不同吗?
编辑: 我确实必须切换分词器和分析器的顺序,否则会出错。所以你可能想调查一下。