【发布时间】:2018-09-06 10:50:19
【问题描述】:
我喜欢使用 Edge-NGrams 从 Elasticsearch 获得的结果来索引数据和用于搜索的不同分析器。但是,我更喜欢匹配的较短术语比较长术语排名更高。
例如,使用术语ABC100 和ABC100xxx。如果我使用术语ABC 执行查询,我会以相同分数返回这两个文档。我希望ABC100 的得分高于ABC100xxx,因为ABC 更接近ABC100 根据Levenshtein distance algorithm 之类的东西。
设置索引:
PUT stackoverflow
{
"settings": {
"index": {
"number_of_replicas": 0,
"number_of_shards": 1
},
"analysis": {
"filter": {
"edge_ngram": {
"type": "edgeNGram",
"min_gram": "1",
"max_gram": "20"
}
},
"analyzer": {
"my_analyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"edge_ngram"
]
}
}
}
},
"mappings": {
"doc": {
"properties": {
"product": {
"type": "text",
"analyzer": "my_analyzer",
"search_analyzer": "whitespace"
}
}
}
}
}
插入文档:
PUT stackoverflow/doc/1
{
"product": "ABC100"
}
PUT stackoverflow/doc/2
{
"product": "ABC100xxx"
}
搜索查询:
GET stackoverflow/_search?pretty
{
"query": {
"match": {
"product": "ABC"
}
}
}
结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.28247002,
"hits": [
{
"_index": "stackoverflow",
"_type": "doc",
"_id": "2",
"_score": 0.28247002,
"_source": {
"product": "ABC100xxx"
}
},
{
"_index": "stackoverflow",
"_type": "doc",
"_id": "1",
"_score": 0.28247002,
"_source": {
"product": "ABC100"
}
}
]
}
}
有谁知道我如何将ABC100 等更短的词排在ABC100xxx 之上?
【问题讨论】:
标签: elasticsearch