2016-06-21 38 views
1

我在弹性搜索中有不同类型的索引。 但是,如果我想提高一些选定类型的结果,那么我该怎么做? 我可以在提升查询中使用类型过滤器,但类型过滤器只允许我在过滤器中使用一种类型。我需要在多种类型的基础上提升结果。根据elasticsearch中的选定类型提升结果

例如: 我有弹性搜索索引的Person,Event和Location数据,其中Person,Location和Event是我的类型。

我在所有类型中搜索关键字'伦敦',但我希望人和事件类型记录比位置提升。

我怎么能达到相同的?

回答

0

一个获得所期望的功能的方式是通过包装一个布尔查询中查询,然后利用应节的,为了提高某些文件

小例子:

POST test/person 
{ 
    "title": "london elise moore" 
} 

POST test/event 
{ 
    "title" : "london is a great city" 
} 

没有提升:

GET test/_search 
{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "match": { 
      "title": "london" 
      } 
     } 
     ] 
    } 
    } 
} 

有以下回应:

"hits": { 
    "total": 2, 
    "max_score": 0.2972674, 
    "hits": [ 
     { 
     "_index": "test", 
     "_type": "person", 
     "_id": "AVVx621GYvUb9aQn6r5X", 
     "_score": 0.2972674, 
     "_source": { 
      "title": "london elise moore" 
     } 
     }, 
     { 
     "_index": "test", 
     "_type": "event", 
     "_id": "AVVx63LrYvUb9aQn6r5Y", 
     "_score": 0.26010898, 
     "_source": { 
      "title": "london is a great city" 
     } 
     } 
    ] 
    } 

现在随着加应该子句:

GET test/_search 
{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "match": { 
      "title": "london" 
      } 
     } 
     ], 
     "should": [ 
     { 
      "term": { 
      "_type": { 
       "value": "event", 
       "boost": 2 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

这还给了如下回应:

"hits": { 
    "total": 2, 
    "max_score": 1.0326607, 
    "hits": [ 
     { 
     "_index": "test", 
     "_type": "event", 
     "_id": "AVVx63LrYvUb9aQn6r5Y", 
     "_score": 1.0326607, 
     "_source": { 
      "title": "london is a great city" 
     } 
     }, 
     { 
     "_index": "test", 
     "_type": "person", 
     "_id": "AVVx621GYvUb9aQn6r5X", 
     "_score": 0.04235228, 
     "_source": { 
      "title": "london elise moore" 
     } 
     } 
    ] 
    } 

你甚至可以离开了额外的动力在宜条款,导致如果应该条款匹配它会提高结果:)

希望这有助于!

+0

谢谢拜伦。是的,应该子句可以提高结果..但是,我想在过滤器中使用相同的,因为我不想修改现有的查询,而是我会添加一个过滤器,这将取决于选定的ES类型提升结果。可能吗?如果是的话,那么如何? – user3340357

+0

我没有看到任何通过使用过滤器来获得该功能的方法。过滤器缩小了您的结果集,它们不是用来增强某些文档的。不确定其他人是否有这方面的想法。 也许你可以发布你的查询,以便我们能够拿出更好的选择? –

0

我看到这样做的两种方式使用,但两者的使用使用脚本 1.分拣

POST c1_1/_search 
{ 
    "from": 0, 
    "size": 10, 
    "sort": [ 
    { 
     "_script": { 
     "order": "desc", 
     "type": "number", 
     "script": "double boost = 1; if(doc['_type'].value == 'Person') { boost *= 2 }; if(doc['_type'].value == 'Event') { boost *= 3}; return _score * boost; ", 
     "params": {} 
     } 
    }, 
    { 
     "_score": {} 
    } 
    ], 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "query_string": { 
      "query": "*", 
      "default_operator": "and" 
      } 
     } 
     ], 
     "minimum_should_match": "1" 
    } 
    } 
} 

第二个选项使用功能评分。

POST c1_1/_search 
{ 
    "from": 0, 
    "size": 10, 
    "query": { 
    "function_score": { 
     "query": { 
     "bool": { 
      "should": [ 
      { 
       "query_string": { 
       "query": "*", 
       "default_operator": "and" 
       } 
      } 
      ], 
      "minimum_should_match": "1" 
     } 
     }, 
     "script_score": { 
     "script": "_score * (doc['_type'].value == 'Person' || doc['_type'].value == 'Event'? 2 : 1)" 
     } 
    } 
    } 
}