簡體   English   中英

通過 elasticSearch 計算子數組中不同值的計數?

[英]calculate count of distinct value in child array by elasticSearch?

我有這個結構的索引:

class Note {
    public string Text {get; set;}
    public string[] Tags {get; set;}
}

我想獲得分配給所有筆記的每個不同標簽的使用計數。 例如在這個數據上:

[
    {
        "_id" : 1
        "text":"first text",
        "tags" : ["TagA", "TagB"]
    },

    {
       "_id" : 2
       "text": "second text",
       "tags" : ["TagA", "TagC"]
    }
]

我期待這樣的結果:

[
    {
      "Tag":"TagA",
      "count":2,
    },
   
   {
      "Tag":"TagB",
      "count":1,
   },
   
   {
      "Tag":"TagC",
      "count":1,
   }

]

我可以通過 ElasticSearch 生成此結果嗎? 如果答案是“是”,請指導我。 另外,我想通過用戶輸入的一些詞來過濾標簽。

更新:這是我的索引的映射:

{
  "Nots" : {
    "mappings" : {
      "properties" : {
        "tags" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "text" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
} 

更新 2:

我通過以下代碼過濾了條目:

POST publishers_inventories/_search
{
  "size": 0, 
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "tags.keyword",
            "query": "*تگ*"
          }
        }
      ]
    }
  },
  "aggs": {
    "distinct_tags": {
      "terms": {
        "field": "tags.keyword",
        "size": 200
      }
    }
  }
}

但現在結果包含過濾文檔中包含的所有標簽。 例如,如果我搜索“Win”短語,它會返回標簽中包含“Win”的所有文檔,但所有其他短語都放在結果文檔中的“Win”旁邊。

是的,您可以簡單地使用這樣的terms聚合

{
  "size": 0,
  "query": {
    "match": {
      "tags": "win"
    }
  },
  "aggs": {
    "distinct_tags": {
      "terms": {
        "field": "tags.keyword",
        "size": 10
      }
    }
  }
}

暫無
暫無

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

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