elasticsearch ElasticSearch-搜索具有相同字段名但不同分析器的不同文档类型
假设我制作了一个简单的ElasticSearch索引:elasticsearch ElasticSearch-搜索具有相同字段名但不同分析器的不同文档类型,elasticsearch,lucene,search-engine,analyzer,elasticsearch,Lucene,Search Engine,Analyzer,假设我制作了一个简单的ElasticSearch索引: curl -XPUT 'http://localhost:9200/test/' -d '{ "settings": { "analysis": { "char_filter": { "de_acronym": { "type": "mapping", "mappings": [".
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"]
}
}
}
}
}'
我创建了两个doc_类型,它们具有相同的属性name
,但它们的分析方式略有不同:
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"
}
}
}
}'
接下来,假设我在每个文档类型中放置了一个同名文档:
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会用适当的分析器搜索每种文档类型。有办法做到这一点吗
ElasticSearch文档说:
1) 在查询本身中定义的分析器,或者
2) 在字段映射中定义的分析器,否则
在这种情况下,ElasticSearch是否任意选择要使用的字段映射?看看github中的问题,它似乎是从google Group中的post开始的。我相信它回答了你的问题:
如果它在一个过滤的查询中,我们无法推断它,所以我们只需选择其中一个并使用它的分析设置