Elasticsearch-如何为某些字段返回不同的文档

时间:2019-08-29 17:14:43

标签: elasticsearch

我有下一个Elasticsearch查询,我需要知道如何仅对某些字段获得不同的结果。 (就像一个sql一样:SELECT DISTINCT column1,column2,... FROM table_name:wink:

这是我的查询

{
  "_source": ["part", "manufacturer", "shortdesc"],
  "query": {
  "match": {
       "part": "2n2222"
    }
  }
}

这是我得到的结果:

{
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "13921",
    "_score" : 207.16005,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Microsemi Corporation"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "13923",
    "_score" : 207.16005,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Microsemi Corporation"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "811202",
    "_score" : 202.03964,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Microsemi Corporation"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "534059",
    "_score" : 202.03964,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Microsemi Corporation"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "534062",
    "_score" : 202.03964,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Microsemi Corporation"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "144303",
    "_score" : 202.03964,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Microsemi Corporation"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "557240",
    "_score" : 202.03964,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Infineon"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "13924",
    "_score" : 201.24086,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Microsemi Corporation"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "557235",
    "_score" : 201.24086,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Microsemi Corporation"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "55566",
    "_score" : 201.24086,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Microsemi Corporation"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "50873",
    "_score" : 201.24086,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Microsemi Corporation"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "13915",
    "_score" : 199.76857,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Microsemi Corporation"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "591924",
    "_score" : 199.76857,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Microsemi Corporation"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "526043",
    "_score" : 199.76857,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Microsemi Corporation"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "423282",
    "_score" : 198.89282,
    "_source" : {
      "part" : "2N2222A",
      "manufacturer" : "Microsemi Corporation"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "565951",
    "_score" : 193.51782,
    "_source" : {
      "part" : "P2N2222A",
      "manufacturer" : "ON Semiconductor"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "13920",
    "_score" : 192.1505,
    "_source" : {
      "part" : "P2N2222A",
      "manufacturer" : "ON Semiconductor"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "2885944",
    "_score" : 191.28773,
    "_source" : {
      "part" : "Q2N2222A",
      "manufacturer" : "Freescale Semiconductor"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "765656",
    "_score" : 191.28773,
    "_source" : {
      "part" : "2N2222AL",
      "manufacturer" : "Microsemi"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "491090",
    "_score" : 190.78474,
    "_source" : {
      "part" : "2N2222AUB",
      "manufacturer" : "Microsemi Corporation"
    }
  }

如果一条记录包含相同的零件和制造商,则视为重复。我需要为这些字段获取不同的值。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

我相信您需要在查询中使用聚合来获得独特的配对行为。有关非重复值查询的示例,请参见this

链接问题和案例之间的主要区别在于,您有两个字段,并且需要所有唯一的对,而不是两个字段的唯一值。

编辑: 刚刚测试了一下,它的行为似乎与您要尝试的类似。您可以通过删除/禁用术语聚合的doc_count计数并像在问题中一样使用_source来优化它。您还可以添加查询和匹配子句以过滤到给定的零件/制造商。

EDIT2:如问题中所示,将查询/匹配项添加到请求中。

GET YOURINDEX/_search
{
"query": {
    "match": {
      "part.keyword": "2n2222"
    }
  }, 
  "size": 0,
  "aggs": {
    "actions": {
      "terms": {
        "field": "part.keyword"
      },
      "aggs": {
        "emails": {
          "terms": {
            "field": "manufacturer.keyword"
          }
        }
      }
    }
  }
}