有映射,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
我想搜索“小”这个词,但我总是希望结果的顺序如下:
- 部分中“小”的结果
- “小”在类别中的结果
- 标题中包含“小”的结果
所以我用查询搜索:
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 应该是第二个,因为匹配在该部分中。3 应该在 7 和 5 之后,因为匹配在类别和标题中。
所以,我的问题是,我怎样才能获得部分匹配总是更重要的结果,然后是类别匹配,这总是比标题匹配更重要。
提前致谢!
编辑:
使用搜索类型“dfs_query_then_fetch”解决了问题,该类型计算所有分片的 TF-IDF 值。有关更多信息,请参阅http://www.elasticsearch.org/guide/reference/api/search/search-type/