我的ES索引产品包含这样的数据(简化版,实际上大约有20个字段):
{_id: 1, _score: 1, color: red, size: S}
{_id: 2, _score: 1, color: red, size: M}
{_id: 3, _score: 1, color: red, size: L}
{_id: 4, _score: 1, color: blue, size: S}
{_id: 5, _score: 1, color: blue, size: M}
{_id: 6, _score: 1, color: blue, size: L}
我想按属性(颜色和大小)过滤产品,但我需要在结果中显示所有产品,过滤条件应仅影响分数。例子:
Query: color == red
Result:
{_id: 1, _score: 1}
{_id: 2, _score: 1}
{_id: 3, _score: 1}
{_id: 4, _score: 0}
{_id: 5, _score: 0}
{_id: 6, _score: 0}
Query: size == M
Result:
{_id: 1, _score: 0}
{_id: 2, _score: 1}
{_id: 3, _score: 0}
{_id: 4, _score: 0}
{_id: 5, _score: 1}
{_id: 6, _score: 0}
Query: color == red && size == M
Result:
{_id: 1, _score: 0}
{_id: 2, _score: 1}
{_id: 3, _score: 0}
{_id: 4, _score: 0}
{_id: 5, _score: 0}
{_id: 6, _score: 0}
任何想法我怎样才能做到这一点?它看起来像弹性搜索的工作?也许我应该考虑切换一些其他商店/数据库。 ES版本是1.7.5
答案 0 :(得分:0)
我不确定您的用例是什么,但我不建议您尝试修复相关性得分。以下是其中一个案例的示例,有多种方法可以执行此操作
POST /_search
{
"query":{
"bool":{
"should":[
{
"constant_score":{
"filter":{
"term":{
"color":"red"
}
},
"boost":1
}
},
{
"bool":{
"must_not":{
"term":{
"color":"red"
}
},
"boost":0
}
}
]
}
}
}
,结果是
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "2",
"_score": 1,
"_source": {
"color": "red",
"size": "M"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"color": "red",
"size": "S"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "3",
"_score": 1,
"_source": {
"color": "red",
"size": "L"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "5",
"_score": 0,
"_source": {
"color": "blue",
"size": "M"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "4",
"_score": 0,
"_source": {
"color": "blue",
"size": "S"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "6",
"_score": 0,
"_source": {
"color": "blue",
"size": "L"
}
}
]
}
}