簡體   English   中英

Elasticsearch 在給定過濾器后從數組字段返回唯一字符串

[英]Elasticsearch return unique string from array field after a given filter

如何從彈性搜索記錄中獲取具有給定前綴的所有 id 的所有值並使它們唯一。

記錄

PUT items/1
{ "ids" :  [ "apple_A", "orange_B" ] }

PUT items/2
{ "ids" :  [ "apple_A", "apple_B" ] }

PUT items/3
{ "ids" :  [ "apple_C", "banana_A" ] }

我需要的是找到給定前綴的所有唯一ID,例如,如果輸入是蘋果,則ID的output應該是["apple_A", "apple_B", "apple_C"]

到目前為止,我嘗試使用術語聚合,通過以下查詢,我能夠過濾掉具有給定前綴的 id 的文檔,但在聚合中它將返回文檔的所有 id 部分。

{
  "aggregations": {
    "filterIds": {
      "filter": {
        "bool": {
          "filter": [
            {
              "prefix": {
                "ids.keyword": {
                  "value": "apple"
                }
              }
            }
          ]
        }
      },
      "aggregations": {
        "uniqueIds": {
          "terms": {
            "field": "ids.keyword",
          }
        }
      }
    }
  }
}

如果我們將前綴輸入作為蘋果,它將返回聚合列表為 [ "appleA", "orange_B", "apple_B","apple_C", "banana_A"]。 基本上返回所有具有匹配過濾器的 id。

是否只獲取與數組中前綴匹配的 id 而不是文檔數組中的所有 id?

您可以使用include參數限制返回值:

POST items/_search
{
  "size": 0,
  "aggregations": {
    "filterIds": {
      "filter": {
        "bool": {
          "filter": [
            {
              "prefix": {
                "ids.keyword": {
                  "value": "apple"
                }
              }
            }
          ]
        }
      },
      "aggregations": {
        "uniqueIds": {
          "terms": {
            "field": "ids.keyword",
            "include": "apple.*"    <--
          }
        }
      }
    }
  }
}

請檢查處理在include中使用正則表達式的其他線程——它與您的用例非常相似。

暫無
暫無

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

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