假设我做了一个简单的ElasticSearch索引:
curl -XPUT 'http://localhost:9200/test/' -d '{
"settings": {
"analysis": {
"char_filter": {
"de_acronym": {
"type": "mapping",
"mappings": [".=>"]
}
},
"analyzer": {
"analyzer1": {
"type": "custom",
"tokenizer": "keyword",
"char_filter": ["de_acronym"]
}
}
}
}
}'
并且我创建了两个具有相同属性
name
的doc_type,但是对它们的分析略有不同:curl -XPUT 'http://localhost:9200/test/_mapping/docA' -d '{
"docA": {
"properties": {
"name": {
"type": "string",
"analyzer": "simple"
}
}
}
}'
curl -XPUT 'http://localhost:9200/test/_mapping/docB' -d '{
"docB": {
"properties": {
"name": {
"type": "string",
"analyzer": "analyzer1"
}
}
}
}'
接下来,假设我在每个doc_type中放置了一个具有相同名称的文档:
curl -XPUT 'http://localhost:9200/test/docA/1' -d '{ "name" : "U.S. Army" }'
curl -XPUT 'http://localhost:9200/test/docB/1' -d '{ "name" : "U.S. Army" }'
让我们尝试同时在两种文档类型中搜索“美国陆军”:
curl -XGET 'http://localhost:9200/test/_search?pretty' -d '{
"query": {
"match_phrase": {
"name": {
"query": "U.S. Army"
}
}
}
}'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.5,
"hits" : [ {
"_index" : "test",
"_type" : "docA",
"_id" : "1",
"_score" : 1.5,
"_source":{ "name" : "U.S. Army" }
} ]
}
}
我只有一个结果!当我指定docB的分析器时,我得到了另一个结果:
curl -XGET 'http://localhost:9200/test/_search?pretty' -d '
{
"query": {
"match_phrase": {
"name": {
"query": "U.S. Army",
"analyzer": "analyzer1"
}
}
}
}'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : "test",
"_type" : "docB",
"_id" : "1",
"_score" : 1.0,
"_source":{ "name" : "U.S. Army" }
} ]
}
}
我的印象是ES会使用适当的分析器搜索每个doc_type。有没有办法做到这一点?
ElasticSearch文档说precedence for search analyzer去了:
1)在查询本身中定义的分析器,否则
2)分析器在字段映射中定义,否则
...
在这种情况下,ElasticSearch是否会随意选择要使用的字段映射?
最佳答案
看一下github中的this问题,该问题似乎始于ES google组中的this帖子。我相信它可以回答您的问题:
if its in a filtered query, we can't infer it, so we simply pick one of those and use its analysis settings
关于elasticsearch - ElasticSearch-使用相同的字段名称但使用不同的分析器搜索不同的doc_types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27516795/