【发布时间】:2018-01-04 07:53:54
【问题描述】:
我想我应该用一个例子来解释我的问题:
假设我使用同义词分析器创建了索引,并声明“laptop”、“phone”和“tablet”是可以概括为“mobile”的相似词:
PUT synonym
{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 2,
"analysis": {
"analyzer": {
"synonym": {
"tokenizer": "whitespace",
"filter": [
"synonym"
]
}
},
"filter": {
"synonym": {
"type": "synonym",
"synonyms": [
"phone, tablet, laptop => mobile"
]
}
}
}
}
},
"mappings": {
"synonym" : {
"properties" : {
"field1" : {
"type" : "text",
"analyzer": "synonym",
"search_analyzer": "synonym"
}
}
}
}
}
现在我正在创建一些文档:
PUT synonym/synonym/1
{
"field1" : "phone"
}
PUT synonym/synonym/2
{
"field1" : "tablet"
}
PUT synonym/synonym/3
{
"field1" : "laptop"
}
现在当我匹配laptop、tablet 或phone 的查询时,结果总是:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.2876821,
"hits": [
{
"_index": "synonym",
"_type": "synonym",
"_id": "2",
"_score": 0.2876821,
"_source": {
"field1": "tablet"
}
},
{
"_index": "synonym",
"_type": "synonym",
"_id": "1",
"_score": 0.18232156,
"_source": {
"field1": "phone"
}
},
{
"_index": "synonym",
"_type": "synonym",
"_id": "3",
"_score": 0.18232156,
"_source": {
"field1": "laptop"
}
}
]
}
}
你可以看到tablet的分数总是更高,即使我搜索laptop。
我知道那是因为我将它们声明为相似的词。
但是,我试图弄清楚如何查询,以便带有搜索词的文档可以首先出现在结果列表中的相似词之前。
可以通过boosting来完成,但一定有更简单的方法..
【问题讨论】:
标签: elasticsearch search elasticsearch-5 analyzer synonym