ElasticSearch,返回结果,重复数据删除以及自定义排序冒泡到顶部

我有一个列表索引.每个都有与文档相关的权重.我需要能够搜索“工程师”,并根据与文档相关的相关性和任意权重,为“标题”的每个匹配获得最高结果.

样本索引:

Doc 1 {"title": "Java Engineer", "content": "A long description", "weighted_importance": 10}
Doc 2 {"title": "Search Engineer", "content": "A long description", "weighted_importance": 10}
Doc 3 {"title": "Ruby Engineer", "content": "A long description", "weighted_importance": 10}
Doc 4 {"title": "PHP Engineer", "content": "A long description", "weighted_importance": 10}
Doc 5 {"title": "Java Engineer", "content": "A long description", "weighted_importance": 10}
Doc 6 {"title": "Search Engineer", "content": "A long description", "weighted_importance": 100}
Doc 7 {"title": "Java Engineer", "content": "A long description", "weighted_importance": 100}
Doc 8 {"title": "MySQL Engineer", "content": "A long description", "weighted_importance": 10}

如果我搜索“工程师”,那么期望的结果是它会使用相同的标题对项目进行重复数据删除,并通过增加weighted_importance字段返回结果集中的最佳结果,例如:

Doc 6 {"title": "Search Engineer", "content": "A long description", "weighted_importance": 100}
Doc 7 {"title": "Java Engineer", "content": "A long description", "weighted_importance": 100}
Doc 3 {"title": "Ruby Engineer", "content": "A long description", "weighted_importance": 10}
Doc 4 {"title": "PHP Engineer", "content": "A long description", "weighted_importance": 10}
Doc 8 {"title": "MySQL Engineer", "content": "A long description", "weighted_importance": 10}

最后三个结果会被排序但是它们会下降,但前两个结果需要在它们自己的桶内冒泡到表面.

你可以告诉我,我是ElasticSearch的新手.任何帮助将不胜感激.

最佳答案 试试这种方法:

>您的映射还应该存储标题的not_analyzed版本,以便根据完整标题构建存储桶,而不是基于形成标题的单个术语:

{
  "mappings": {
    "engineers": {
      "properties": {
        "title": {
          "type": "string",
          "fields":{
            "raw": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        },
        "content": {
          "type": "string"
        },
        "weighted_importance": {
          "type": "integer"
        }
      }
    }
  }
}

>将结果分组到基于上面定义的title.raw构建的存储桶上
>定义top_hits子聚合以返回每个桶的“最佳”文档
>在与top_hits相同的级别上定义另一个子聚合,该聚合应该是一个最大聚合,它将计算最大weighted_importance
>在主聚合中使用上面的max来对生成的桶进行排序

GET /my_index/engineers/_search?search_type=count
{
  "query": {
    "match": {
      "title": "Engineer"
    }
  },
  "aggs": {
    "title": {
      "terms": {
        "field": "title.raw",
        "order": {"best_hit":"desc"}
      },
      "aggs": {
        "first_match": {
          "top_hits": {
            "sort": [{"weighted_importance": {"order": "desc"}}],
            "size": 1
          }
        },
        "best_hit": {
          "max": {
            "lang": "groovy", 
            "script": "doc['weighted_importance'].value"
          }
        }
      }
    }
  }
}
点赞