ElasticSearch - 按空格分割的术语聚合

ElasticSearch - terms aggregation split by whitespace

我有一堆弹性搜索文档,其中包含有关招聘广告的信息。我正在尝试聚合 attributes.Title 字段以从职位发布中提取 "experience" 实例的数量。例如Junior、Senior、Lead 等。相反,我得到的是与整个标题匹配的桶,而不是标题字段中的每个单词。例如"Junior Java Developer"、"Senior .NET Analyst" 等

我如何告诉弹性搜索根据标题中的每个词拆分聚合,而不是匹配整个字段的值。

稍后我想扩展查询以提取 "skill level" 和 "role",但如果存储桶包含字段中的所有单词,只要它们是分成不同的桶。

当前查询:

GET /jobs/_search
{
  "query": {
    "simple_query_string" : {
        "query": "Java",
        "fields": ["attributes.Title"]
    }
  },
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "attributes.Title.keyword"
      }
    }
  }
}

不需要的输出:

{
  ...
  "hits": {
    "total": 63,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_state": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 14,
      "buckets": [{
          "key": "Junior Java Tester",
          "doc_count": 6
        },{
          "key": "Senior Java Lead",
          "doc_count": 6
        },{
          "key": "Intern Java Tester",
          "doc_count": 5
        },
        ...
      ]
    }
  }
}

期望输出:

{
  ...
  "hits": {
    "total": 63,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_state": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 14,
      "buckets": [{
          "key": "Junior",
          "doc_count": 12
        },{
          "key": "Senior",
          "doc_count": 8
        },{
          "key": "Tester",
          "doc_count": 5
        },{
          "key": "Intern",
          "doc_count": 5
        },{
          "key": "Analyst",
          "doc_count": 5
        },
        ...
      ]
    }
  }
}

我推断您的映射类型是 keyword,因为您在一个名为 "attributes.Title.keyword" 的字段上进行了聚合。 keyword 映射不会标记您的字符串,因此在聚合期间,它将整个字符串视为唯一键。

您想将标题字段的映射更新为 type: "text"。我不会称它为 title.keyword,而是类似于 title.analyzed——如果你没有指定分析器,如果你只希望你的标题被分解,Elasticsearch 将应用 standard analyzer which should be enough to get you started. You can also use the whitespace analyzer空格(而不是词干和其他一些东西)。你 在你的聚合中得到很多其他词,但我假设你正在寻找这些共享经验修饰符标记,并且根据频率,它们将上升到顶部。

如果您使用 5.x,请务必设置 'fielddata: true',因为 text fields aren't available for aggregation by default

映射:

"properties" : {
    "attributes" : {
        "properties" : {
            "title" : {
                "properties" : {
                    "keyword" : { "type" : "keyword" },
                    "analyzed" : { "type" : "text", "analyzer" : "whitespace", "fielddata" : true }
                }
            }
        }
    }
 }