簡體   English   中英

Elasticsearch:基於過濾器構建二進制掩碼

[英]Elasticsearch: build binary mask based on filter

我有帶有這樣的數據的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}

有什么想法可以實現嗎? 看起來對Elasticsearch有用嗎? 也許我應該考慮切換其他存儲/數據庫。 ES版本為1.7.5

我不確定您的用例是什么,但這不是我建議您嘗試解決的相關分數。 這是其中一種情況的示例,並且有多種執行方法

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"
                }
            }
        ]
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM