elasticsearch - 弹性查询以查找来自不同组织的内容中的相似标签

标签 elasticsearch

我使用来自不同组织的内容源,这些组织都提供元数据标签。我想要一个由不同组织提供的术语列表。

Elasticsearch中的数据样本:

doc1: {
    "tags":["tag1", "tag5", "tag6", "tag4"],
    "organization" : "A"
}

doc2: {
    "tags":["tag1", "tag2", "tag4"],
    "organization" : "B"
} 

所需查询结果:
{
   "tag": "tag1",
   "organization" : ["A", "B"]
},

{
   "tag": "tag4",
   "organization" : ["A", "B"]
}

我到目前为止得到的是

通过以下建议,我得到了一个结果列表,其中包含一个组织使用的关键字以及不同组织使用的关键字。

为了澄清,这是结果的一部分:
     {
      "key": "someKeyWord",
      "doc_count": 66,
      "organization_list": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "Organization A",
            "doc_count": 62
          },
          {
            "key": "Organization B",
            "doc_count": 4
          }
        ]
      }
    },
    {
      "key": "someOtherKeyword",
      "doc_count": 62,
      "organization_list": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "Organization A",
            "doc_count": 62
          }
        ]
      }
    }

现在,我只想要第一个结果,该结果具有来自organization_list聚合的两个存储桶。因为该关键字由两个不同的组织使用。

我这样尝试过:
    "number_buckets_filter": {
        "bucket_selector": {
            "buckets_path": {
                "my_var": "organization_list"
             },
             "script": "params.my_var > 1"
        }
    }

但这使我感到异常:“buckets_path必须引用数字值或单值数字度量聚合,得到:org.elasticsearch.search.aggregations.bucket.terms.StringTerms”

有什么办法可以过滤结果?在此先感谢您的帮助。

亲切的问候,
奥斯卡伊特德博斯

最佳答案

您可以使用以下查询首先对标签进行存储,然后对组织进行子存储

{
  "size": 0,
  "aggs": {
    "tags_list": {
      "terms": {
        "field": "tags",
        "size": 100
      },"aggs": {
        "organization_list": {
          "terms": {
            "field": "organization",
            "size": 100
          }
        }
      }
    }
  }
}

映射
{
    "mappings": {
        "product": {
            "properties": {
                "tags": {
                    "type": "text",
                    "fielddata": true
                },
                "organization": {
                    "type": "text",
                    "fielddata": true
                }
            }
        }
    }
}

注意-确保未同时分析标记和组织的汇总。还要在映射中设置fielddata=true以避免占用大量内存。

关于elasticsearch - 弹性查询以查找来自不同组织的内容中的相似标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42692242/

相关文章:

elasticsearch - 如何在 Elasticsearch 中按多个字段创建渗透索引

elasticsearch - elasticsearch-聚合结果中的其他字段

elasticsearch - GeoShape 不存储纬度/经度

elasticsearch - elasticsearch null_value替换不适用于整数

elasticsearch - elasticsearch-head在浏览器中看不到

elasticsearch - Logstash doc_as_upsert在Elasticsearch中交叉索引消除重复

elasticsearch - 如何在Elasticsearch的术语内使用OR?

elasticsearch - Elasticsearch中带有正则表达式的聚合

python - KeyError访问elasticsearch_dsl Python客户端中的inner_hits

elasticsearch - 如何在Elasticsearch中解决unassigned_shards?