有映射,3个字段和9个文档:
#! /bin/bash
#DELETE
curl -XDELETE 'http://localhost:9200/test'
echo
# CREATE
curl -XPUT 'http://localhost:9200/test?pretty=1' -d '{
"settings": {
"analysis" : {
"analyzer" : {
"my_analyz_1" : {
"filter" : [
"standard",
"lowercase",
"asciifolding"
],
"type" : "custom",
"tokenizer" : "standard"
}
}
}
}
}'
echo
# DEFINE
curl -XPUT 'http://localhost:9200/test/posts/_mapping?pretty=1' -d '{
"posts" : {
"properties" : {
"section" : {
"type" : "string",
"analyzer" : "my_analyz_1"
},
"category" : {
"type" : "string",
"analyzer" : "my_analyz_1"
},
"title" : {
"type" : "string",
"analyzer" : "my_analyz_1"
}
}
}
}'
echo
# INSERT
curl localhost:9200/test/posts/1 -d '{section: "Bicycle", category: "Small", title: "Diamondback Grind-16"}'
curl localhost:9200/test/posts/2 -d '{section: "Bicycle", category: "Big", title: "Diamondback JrViper"}'
curl localhost:9200/test/posts/3 -d '{section: "Bicycle", category: "Small", title: "2-Hip Cyclone small"}'
curl localhost:9200/test/posts/4 -d '{section: "Bicycle", category: "Big", title: "2-Hip Bizzle"}'
curl localhost:9200/test/posts/5 -d '{section: "Small", category: "Small", title: "Toyota"}'
curl localhost:9200/test/posts/6 -d '{section: "Car", category: "Big", title: "Subaru Impreza small"}'
curl localhost:9200/test/posts/7 -d '{section: "Small", category: "Big", title: "Toyota Corona MARK II"}'
curl localhost:9200/test/posts/8 -d '{section: "Car", category: "Small", title: "Hyundai Elantra"}'
curl localhost:9200/test/posts/9 -d '{section: "Car", category: "Big", title: "Ford Maverick small"}'
echo
# REFRESH
curl -XPOST localhost:9200/test/_refresh
echo
我想搜索单词"small",但是我总是希望结果的顺序如下:
- 小节 中出现"small"的结果
- 分类 中"small"的结果
- 标题 中出现'small'的结果
所以我搜索query:
curl "localhost:9200/test/posts/_search?pretty=1" -d '{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "small",
"fields": ["section^3", "category^2", "title"]
}
}
]
}
}
}'
,结果为:
{"_id": 7} {section: "Small", category: "Big", title: "Toyota Corona MARK II"}
{"_id": 1} {section: "Bicycle", category: "Small", title: "Diamondback Grind-16"}
{"_id": 5} {section: "Small", category: "Small", title: "Toyota"}
{"_id": 3} {section: "Bicycle", category: "Small", title: "2-Hip Cyclone small"}
{"_id": 8} {section: "Car", category: "Small", title: "Hyundai Elantra"}
{"_id": 9} {section: "Car", category: "Big", title: "Ford Maverick small"
{"_id": 6} {section: "Car", category: "Big", title: "Subaru Impreza small"}
这不是我想要的。5应该是第二个,因为match在section中。3应该在7和5之后,因为match在category和title中。
所以,我的问题是,我如何才能获得匹配在section 总是更重要的结果,然后匹配在类别,这是总是比匹配在标题。
提前感谢!
编辑:使用搜索类型'dfs_query_then_fetch'解决了问题,它计算所有分片的TF-IDF值。更多信息请参见http://www.elasticsearch.org/guide/reference/api/search/search-type/
您是否尝试将use_dis_max
设置为false
?
这应该意味着在category
和title
中带有"small"的文档将高于仅在category
中带有"small"的文档。
至于你在第二和第三个结果之间看到的奇怪行为,我有点迷路了…你能做这个查询并问一下吗解释分数是如何计算的?