我目前正在通过FOQElasticaBundle在我的Symfony2应用程序中实现elasticsearch,到目前为止,它基于应用于我的“Story”实体的各个字段的提升而工作得很好。这是配置:
foq_elastica:
clients:
default: { host: localhost, port: 9200 }
indexes:
website:
client: default
types:
story:
mappings:
title: { boost: 8 }
summary: { boost: 5 }
text: { boost: 3 }
author:
persistence:
driver: orm # orm, mongodb, propel are available
model: Acme\Bundle\StoryBundle\Entity\Story
provider:
query_builder_method: createIsActiveQueryBuilder
listener:
service: acme_story.search_index_listener
finder:
然而,我还希望根据故事的“published_at”日期应用提升,以便昨天发布的故事会出现在6个月前发布的故事之前的结果中 - 即使旧故事有一个得分略高(显然这需要一些调整)。这可能吗?
如果有人能让我知道如何使用FOQElasticaBundle来实现这一点,那将是很好的,但是如果你能让我知道如何在elasticsearch中直接实现这一点我会很感激,所以我可以尝试自己实现这个行为。如果需要,可以为捆绑做出贡献。
感谢。
答案 0 :(得分:41)
哇,经过大量的实验和数小时的拖网工作后,我终于设法得到了理想的行为! (完全归功于Clinton Gormley。)
映射配置:
mappings:
title: { boost: 8 }
summary: { boost: 5 }
text: { boost: 3 }
author:
publishedAt: { type: date }
以下是使用PHP客户端Elastica动态构建查询以使用原始映射和已发布日期进行提升的代码:
$query = new \Elastica_Query_Bool();
$query->addMust(new \Elastica_Query_QueryString($queryString));
$ranges = array();
for ($i = 1; $i <= 5; $i++) {
$date = new \DateTime("-$i month");
$currentRange = new \Elastica_Query_Range();
$currentRange->addField('publishedAt', array(
'boost' => (6 - $i),
'gte' => $date->getTimestamp()
));
$ranges[] = $currentRange->toArray();
}
$query->addShould($ranges);
/** @var $pagerfanta Pagerfanta */
$pagerfanta = $this->getFinder()->findPaginated($query);
对于那些对原始弹性搜索查询更感兴趣的人(为简洁起见只有3个日期范围)......
curl -XPOST 'http://localhost:9200/website/story/_search?pretty=true' -d '
{
"query" : {
"bool" : {
"must" : {
query_string: {
query: "<search term(s)>"
}
},
"should" : [
{
"range" : {
"publishedAt" : {
"boost" : 5,
"gte" : "<1 month ago>"
}
}
},
{
"range" : {
"publishedAt" : {
"boost" : 4,
"gte" : "<2 months ago>"
}
}
},
{
"range" : {
"publishedAt" : {
"boost" : 3,
"gte" : "<3 months ago>"
}
}
}
]
}
}
}'
答案 1 :(得分:28)
您可以使用衰减评分函数来降低得分与时间的关系:
{
"query": {
"function_score": {
"functions": [
{
"linear": {
"pubdate": {
"origin": 1398673886,
"scale": "1h",
"offset": 0,
"decay": 0.1
}
}
}
]
}
}
}
答案 2 :(得分:9)
基于 function_score 的完整 elasticsearch 5 示例。有关详细信息,请参阅this blogpost和function_score docs。
允许基于具有不同强度的多个日期范围,在没有“硬截止”的高斯曲线上提升更近期的条目。
{
"query": {
"function_score": {
"score_mode": "sum", // All functions outputs get summed
"boost_mode": "multiply", // The documents relevance is multiplied with the sum
"functions": [
{
// The relevancy of old posts is multiplied by at least one.
// Remove if you want to exclude old posts
"weight": 1
},
{
// Published this month get a big boost
"weight": 5,
"gauss": {
"date": { // <- Change to your date field name
"origin": "2017-04-07", // Change to current date
"scale": "31d",
"decay": 0.5
}
}
},
{
// Published this year get a boost
"weight": 2,
"gauss": {
"date": { // <- Change to your date field name
"origin": "2017-04-07", // Change to current date
"scale": "356d",
"decay": 0.5
}
}
}
],
"query": {
// The rest of your search here, change to something relevant
"match": { "title": "< your search string >" }
}
}
}
}