ElasticSearch索引和搜索分析器一起使用
我刚刚看了这段视频,有一个关于ElasticSearch分析器的问题。 我读过官方文档和其他一些关于分析和分析器的文章,我有点困惑 例如,我有以下索引配置:ElasticSearch索引和搜索分析器一起使用,search,
elasticsearch,full-text-search,Search,
elasticsearch,Full Text Search,我刚刚看了这段视频,有一个关于ElasticSearch分析器的问题。 我读过官方文档和其他一些关于分析和分析器的文章,我有点困惑 例如,我有以下索引配置: "settings" : { "analysis" : { "filter" : { "autocomplete" : { "type" : "edge_ngram", "min_gram" : 1, "max_gram" : 20
"settings" : {
"analysis" : {
"filter" : {
"autocomplete" : {
"type" : "edge_ngram",
"min_gram" : 1,
"max_gram" : 20
}
},
"analyzer" : {
"autocomplete" : {
"type" : "custom",
"tokenizer" : "standard",
"filter" : ["lowercase", "autocomplete"]
}
}
}
},
"mappings" : {
"user" : {
"properties" : {
"name" : {
"type" : "multi_field",
"fields" : {
"name" : {
"type" : "string",
"analyzer" : "standard"
},
"autocomplete" : {
"type" : "string",
"index_analyzer" : "autocomplete",
"search_analyzer" : "standard"
}
}
}
}
}
}
然后我分别执行以下搜索请求:
{
"match" : {
"name.autocomplete" : "john smi"
}
}
这是:
{
"match" : {
"name" : "john smi"
}
}
如果我理解正确,我必须看到相同的结果,因为在这两种情况下,ES都应该使用标准分析仪,但我得到了不同的结果。为什么?
更新
我在索引中有以下人名集合:约翰·史密斯、约翰·史密斯。当我尝试你这里的东西时,我得到了相同的结果,并带有所需的包装。首先我创建了一个索引:
curl -XPOST "http://localhost:9200/test_index/" -d'
{
"settings": {
"analysis": {
"filter": {
"autocomplete": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete"
]
}
}
}
},
"mappings": {
"user": {
"properties": {
"name": {
"type": "multi_field",
"fields": {
"name": {
"type": "string",
"analyzer": "standard"
},
"autocomplete": {
"type": "string",
"index_analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
}
}
}
}
}'
然后添加一个文档:
curl -XPUT "http://localhost:9200/test_index/user/1" -d'
{
"name": "John Smith"
}'
curl -XPOST "http://localhost:9200/test_index/user/_search" -d'
{
"query": {
"match": {
"name.autocomplete": "john smith"
}
}
}'
...
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2712221,
"hits": [
{
"_index": "test_index",
"_type": "user",
"_id": "1",
"_score": 0.2712221,
"_source": {
"name": "John Smith"
}
}
]
}
}
第一次搜索将生成文档:
curl -XPUT "http://localhost:9200/test_index/user/1" -d'
{
"name": "John Smith"
}'
curl -XPOST "http://localhost:9200/test_index/user/_search" -d'
{
"query": {
"match": {
"name.autocomplete": "john smith"
}
}
}'
...
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2712221,
"hits": [
{
"_index": "test_index",
"_type": "user",
"_id": "1",
"_score": 0.2712221,
"_source": {
"name": "John Smith"
}
}
]
}
}
第二条也是如此:
curl -XPOST "http://localhost:9200/test_index/user/_search" -d'
{
"query": {
"match": {
"name": "john smith"
}
}
}'
...
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2712221,
"hits": [
{
"_index": "test_index",
"_type": "user",
"_id": "1",
"_score": 0.2712221,
"_source": {
"name": "John Smith"
}
}
]
}
}
你的设置是否与我在这里所做的有所不同
以下是我用于解决此问题的代码:
我已经更新了这里的代码输入名称和查询示例。请检查。您必须保存您的更改并给我新的链接,以便我能够看到它们。进行更改并点击右上角的“保存代码”按钮,您将被重定向到新的url。将那个url粘贴到这里,这样我就可以看到你做了什么。你给我的那个和我给你的是同一个,所以我看不到你的更改。