该boost
值不是绝对的 - 它与其他因素相结合来确定每个术语的相关性。
你有两个“性别”(我会假设),但有很多不同的“喜欢”。所以male
被认为几乎无关紧要,因为它在您的数据中经常出现。然而,cars
可能只出现几次,因此被认为更相关。
此逻辑对全文搜索很有用,但不适用于枚举,枚举本质上是用作过滤器。
omit_term_freq_and_positions
幸运的是,您可以使用和在每个字段的基础上禁用此功能omit_norms
。
尝试如下设置映射:
curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1' -d '
{
"mappings" : {
"test" : {
"properties" : {
"likes" : {
"index" : "not_analyzed",
"omit_term_freq_and_positions" : 1,
"omit_norms" : 1,
"type" : "string"
},
"gender" : {
"index" : "not_analyzed",
"omit_term_freq_and_positions" : 1,
"omit_norms" : 1,
"type" : "string"
},
"age" : {
"type" : "integer"
}
}
}
}
}
'
更新:完整的工作示例:
删除现有索引:
curl -XDELETE 'http://127.0.0.1:9200/users/?pretty=1'
使用新映射创建索引:
curl -XPUT 'http://127.0.0.1:9200/users/?pretty=1' -d '
{
"mappings" : {
"profile" : {
"properties" : {
"likes" : {
"index" : "not_analyzed",
"omit_term_freq_and_positions" : 1,
"type" : "string",
"omit_norms" : 1
},
"age" : {
"type" : "integer"
},
"gender" : {
"index" : "not_analyzed",
"omit_term_freq_and_positions" : 1,
"type" : "string",
"omit_norms" : 1
}
}
}
}
}
'
索引测试文档:
curl -XPOST 'http://127.0.0.1:9200/users/profile/_bulk?pretty=1' -d '
{"index" : {"_id" : 1}}
{"nickname" : "bob", "likes" : "airplanes", "age" : 48, "gender" : "male"}
{"index" : {"_id" : 2}}
{"nickname" : "carlos", "likes" : "food", "age" : 24, "gender" : "male"}
{"index" : {"_id" : 3}}
{"nickname" : "julio", "likes" : "ladies", "age" : 18, "gender" : "male"}
{"index" : {"_id" : 4}}
{"nickname" : "maria", "likes" : "cars", "age" : 25, "gender" : "female"}
{"index" : {"_id" : 5}}
{"nickname" : "anna", "likes" : "clothes", "age" : 50, "gender" : "female"}
'
刷新索引(以确保可以搜索到最新的文档):
curl -XPOST 'http://127.0.0.1:9200/users/_refresh?pretty=1'
搜索:
curl -XGET 'http://127.0.0.1:9200/users/profile/_search?pretty=1' -d '
{
"query" : {
"bool" : {
"minimum_number_should_match" : 1,
"should" : [
{
"term" : {
"gender" : {
"boost" : 10,
"term" : "male"
}
}
},
{
"term" : {
"likes" : {
"boost" : 5,
"term" : "cars"
}
}
},
{
"range" : {
"age" : {
"boost" : 1,
"from" : 50
}
}
}
]
}
}
}
'
结果:
# {
# "hits" : {
# "hits" : [
# {
# "_source" : {
# "nickname" : "bob",
# "likes" : "airplanes",
# "age" : 48,
# "gender" : "male"
# },
# "_score" : 0.053500723,
# "_index" : "users",
# "_id" : "1",
# "_type" : "profile"
# },
# {
# "_source" : {
# "nickname" : "carlos",
# "likes" : "food",
# "age" : 24,
# "gender" : "male"
# },
# "_score" : 0.053500723,
# "_index" : "users",
# "_id" : "2",
# "_type" : "profile"
# },
# {
# "_source" : {
# "nickname" : "julio",
# "likes" : "ladies",
# "age" : 18,
# "gender" : "male"
# },
# "_score" : 0.053500723,
# "_index" : "users",
# "_id" : "3",
# "_type" : "profile"
# },
# {
# "_source" : {
# "nickname" : "anna",
# "likes" : "clothes",
# "age" : 50,
# "gender" : "female"
# },
# "_score" : 0.029695695,
# "_index" : "users",
# "_id" : "5",
# "_type" : "profile"
# },
# {
# "_source" : {
# "nickname" : "maria",
# "likes" : "cars",
# "age" : 25,
# "gender" : "female"
# },
# "_score" : 0.015511602,
# "_index" : "users",
# "_id" : "4",
# "_type" : "profile"
# }
# ],
# "max_score" : 0.053500723,
# "total" : 5
# },
# "timed_out" : false,
# "_shards" : {
# "failed" : 0,
# "successful" : 5,
# "total" : 5
# },
# "took" : 4
# }
更新:替代方法
在这里,我提出了一个替代查询,它虽然更详细,但可以为您提供更可预测的结果。它涉及使用自定义过滤器得分查询。首先,我们将文档过滤为至少匹配一个条件的文档。因为我们使用恒定分数查询,所以所有文档的初始分数都是 1。
自定义过滤器分数允许我们在每个文档匹配过滤器时提升它:
curl -XGET 'http://127.0.0.1:9200/_all/_search?pretty=1' -d '
{
"query" : {
"custom_filters_score" : {
"query" : {
"constant_score" : {
"filter" : {
"or" : [
{
"term" : {
"gender" : "male"
}
},
{
"term" : {
"likes" : "cars"
}
},
{
"range" : {
"age" : {
"gte" : 50
}
}
}
]
}
}
},
"score_mode" : "total",
"filters" : [
{
"boost" : "10",
"filter" : {
"term" : {
"gender" : "male"
}
}
},
{
"boost" : "5",
"filter" : {
"term" : {
"likes" : "cars"
}
}
},
{
"boost" : "1",
"filter" : {
"range" : {
"age" : {
"gte" : 50
}
}
}
}
]
}
}
}
'
您会看到与每个文档相关的分数都是很好的整数,很容易追溯到匹配的子句:
# [Fri Jun 8 21:30:24 2012] Response:
# {
# "hits" : {
# "hits" : [
# {
# "_source" : {
# "nickname" : "bob",
# "likes" : "airplanes",
# "age" : 48,
# "gender" : "male"
# },
# "_score" : 10,
# "_index" : "users",
# "_id" : "1",
# "_type" : "profile"
# },
# {
# "_source" : {
# "nickname" : "carlos",
# "likes" : "food",
# "age" : 24,
# "gender" : "male"
# },
# "_score" : 10,
# "_index" : "users",
# "_id" : "2",
# "_type" : "profile"
# },
# {
# "_source" : {
# "nickname" : "julio",
# "likes" : "ladies",
# "age" : 18,
# "gender" : "male"
# },
# "_score" : 10,
# "_index" : "users",
# "_id" : "3",
# "_type" : "profile"
# },
# {
# "_source" : {
# "nickname" : "maria",
# "likes" : "cars",
# "age" : 25,
# "gender" : "female"
# },
# "_score" : 5,
# "_index" : "users",
# "_id" : "4",
# "_type" : "profile"
# },
# {
# "_source" : {
# "nickname" : "anna",
# "likes" : "clothes",
# "age" : 50,
# "gender" : "female"
# },
# "_score" : 1,
# "_index" : "users",
# "_id" : "5",
# "_type" : "profile"
# }
# ],
# "max_score" : 10,
# "total" : 5
# },
# "timed_out" : false,
# "_shards" : {
# "failed" : 0,
# "successful" : 20,
# "total" : 20
# },
# "took" : 6
# }