疲劳是最舒适的枕头,努力工作吧。

大佬们,聚合交集该怎么做呢

Elasticsearch | 作者 ezio_o | 发布于2019年09月27日 | 阅读数:1626

举个例子说,  一个产品A 有属性集合[1,2,3,4]  , 一个产品B  有属性集合[2,3] , 该怎么使用聚合aggs  拿出他们的共有属性集合[2,3]呢?
 
已邀请:

doom

赞同来自:

PUT /product
{
  "settings": {
    "number_of_replicas": 0,
    "number_of_shards": 1
  },
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "type": "text",
          "index": true,
          "analyzer": "keyword",
          "search_analyzer": "ik_smart"
        },
        "tags": {
          "type": "text",
          "index": true,
          "analyzer": "keyword",
          "search_analyzer": "ik_smart"
        }
      }
    }
  }
}
PUT /product/_doc/1
{
  "name": "a",
  "tags": ["1","2","3","4"]
}
PUT /product/_doc/2
{
  "name": "b",
  "tags": ["2","3"]
}
用布尔查询,获取共有的值
GET product/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {"tags": "2"}},
        {"match": {"tags": "3"}}
      ]
    }
  }
}
 

要回复问题请先登录注册