【问题标题】:ElasticSearch - terms aggregation split by whitespaceElasticSearch - 术语聚合按空格拆分
【发布时间】:2018-03-21 23:53:15
【问题描述】:

我有一堆包含招聘广告信息的弹性搜索文档。我正在尝试聚合 attributes.Title 字段以从职位发布中提取“经验”实例的数量。例如Junior,Senior,Lead等。相反,我得到的是与标题整体匹配的存储桶,而不是标题字段中的每个单词。例如“初级 Java 开发人员”、“高级 .NET 分析师”等

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

稍后我想扩展查询以提取“技能级别”和“角色”,但如果桶包含字段中的所有单词,只要它们被分成单独的桶,也应该没问题。

当前查询:

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
        },
        ...
      ]
    }
  }
}

【问题讨论】:

    标签: elasticsearch aggregate aggregation


    【解决方案1】:

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

    您希望将标题字段的映射更新为type: "text"。我不会称它为title.keyword,而是类似title.analyzed——如果您不指定分析器,Elasticsearch 将应用standard analyzer,这应该足以让您入门。如果您只希望标题被空格(而不是词干和其他一些东西)分解,您也可以使用whitespace analyzer。你在你的聚合中得到很多其他的词,但我假设你正在寻找这些共享体验修饰符标记,并且根据频率,它们会上升到顶部。

    如果您使用的是 5.x,请确保设置 'fielddata: true' since text fields aren't available for aggregation by default

    映射:

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

    【讨论】:

      猜你喜欢
      • 2014-07-09
      • 2021-06-06
      • 1970-01-01
      • 2018-05-21
      • 2021-06-06
      • 2015-11-06
      • 2015-01-21
      • 2019-12-06
      • 2020-10-30
      相关资源
      最近更新 更多