带有 ngram 索引的 Elasticsearch 未找到部分匹配项

joh*_*ser 0 lucene n-gram elasticsearch

所以我有一个像这样创建的弹性搜索索引:

curl -XPUT 'http://localhost:9200/person' -d '{
    "settings": {
        "number_of_shards": 1,
        "analysis": {
            "filter": {
                "autocomplete_filter": {
                    "type":     "edge_ngram",
                    "min_gram": 1,
                    "max_gram": 20
                }
            },
            "analyzer": {
                "autocomplete": {
                    "type":      "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase",
                        "autocomplete_filter"
                    ]
                }
            }
        }
    }
}'
Run Code Online (Sandbox Code Playgroud)

在查询名为“ian”的人时,我得到两个结果

curl -XGET http://localhost:9200/person/_search -d '{
        "query": {
                "match": {
                        "_all": "ian"
                }
        }
}’
Run Code Online (Sandbox Code Playgroud)

但是在仅查询字母时ia,我应该得到尽可能多或更多的结果,但我没有得到任何结果:

curl -XGET http://localhost:9200/person/_search -d '{
        "query": {
                "match": {
                        "_all": "ia"
                }
        }
}’ 
Run Code Online (Sandbox Code Playgroud)

我的edge_ngram过滤器设置有问题吗?我该如何解决这个问题?

编辑:澄清一下,我希望我的插入语句看起来像这样

curl -XPOST "http://localhost:9200/person/RANDOM_STRING HERE/ANOTHER_RANDOM_STRING" -d "{
 "field1" : "value",
 "field2" : "value",
 "field3" : "value"
}"
Run Code Online (Sandbox Code Playgroud)

插入后,我希望对所有字段进行 edge_ngram 分析,以便我可以按任何这些字段的部分字符串进行搜索并返回此结果。

Slo*_*ens 5

如果您只想对每种类型和所有属性使用分析器(除非另有说明),则只需为索引设置“默认”分析器。我在 ES 文档中找不到这个(它们并不总是非常用户友好),但这里有一个例子。我使用的是 ES 1.5,但我认为这并不重要。

PUT /person
{
   "settings": {
      "number_of_shards": 1,
      "analysis": {
         "filter": {
            "autocomplete_filter": {
               "type": "edge_ngram",
               "min_gram": 1,
               "max_gram": 20
            }
         },
         "analyzer": {
            "default": {
               "type": "custom",
               "tokenizer": "standard",
               "filter": [
                  "lowercase",
                  "autocomplete_filter"
               ]
            }
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

然后我索引了文档并运行了您的查询,它运行良好:

POST /person/doc/_bulk
{"index":{"_id":1}}
{"name":"Ian"}
{"index":{"_id":2}}
{"name":"Bob Smith"}

POST /person/_search
{
   "query": {
      "match": {
         "_all": "ia"
      }
   }
}
...
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1.4142135,
      "hits": [
         {
            "_index": "person",
            "_type": "doc",
            "_id": "1",
            "_score": 1.4142135,
            "_source": {
               "name": "Ian"
            }
         }
      ]
   }
}
Run Code Online (Sandbox Code Playgroud)

这是代码:

http://sense.qbox.io/gist/4e2114aafc4f3c507b4f23da8bb83f3ab00e2288