有可能使用弹性搜索来提升“最新”的项目吗? (FOQElasticaBundle) [英] Is it possible to boost 'newest' items using elasticsearch? (FOQElasticaBundle)

查看:133
本文介绍了有可能使用弹性搜索来提升“最新”的项目吗? (FOQElasticaBundle)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 FOQElasticaBundle 在Symfony2应用程序中执行弹性搜索,到目前为止,它一直在很好基于对故事实体的各个领域的应用。这是配置:

  foq_elastica:
客户端:
默认值:{host:localhost,port: 9200}

索引:
网站:
客户端:默认
类型:
故事:
映射:
标题:{ boost:8}
摘要:{boost:5}
文本:{boost:3}
作者:
持久性:
驱动程序:orm#orm,mongodb, propel可用
模型:Acme\Bundle\StoryBundle\Entity\Story
provider:
query_builder_method:createIsActiveQueryBuilder
listener:
service:acme_story.search_index_listener
finder:

H不过,我也想根据故事的published_at日期进行推广,所以昨天发布的一个故事会在6个月前发表的一个故事之前出现在结果中 - 即使这个老故事的得分略高一些(显然这将需要一点调整)。这是可能的吗?



如果有人可以让我知道如何使用FOQElasticaBundle来实现这一点,那将是伟大的,但否则我会感激它,如果你可以让我知道如何要在弹性搜索中直接实现这一点,所以我可以自己尝试和实现自己的行为,如果需要,可以帮助捆绑。



谢谢。

解决方案

呃,经过很多实验和拖网之后的Interweb,我终于找到了所需的行为! (完整信用额度为克林顿·戈麦利 。)



映射配置:

 映射:
标题:{boost:8}
摘要:{boost:5}
文本:{boost:3}
作者:
publishedAt:{type:date}

以下是使用PHP客户端Elastica的代码,动态构建查询以使用原始映射进行升级和发布日期:

  $ query = new \Elastica_Query_Bool(); 
$ query-> addMust(new \Elastica_Query_QueryString($ queryString));

$ ranges = array(); ($ 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'
{
查询:{
bool:{
必须:{
query_string:{
query:&搜索词>
}
},
should:[
{
range:{
publishedAt:{
boost :5,
gte:< 1个月前>
}
}
},
{
range:{
publishedAt:{
boost:4,
gte:< 2个月前>
}
}
},
{
range:{
publishedAt:{
boost:3,
gte:< 3个月前>
}
}
}
]
}
}
}'


I'm currently implementing elasticsearch in my Symfony2 application via the FOQElasticaBundle and so far it's been working great based on boosts applied to various fields of my "Story" entity. Here is the config:

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:

However I'd like to also apply a boost based on the "published_at" date of the story, so that a story published yesterday would appear in the results before a story published 6 months ago - even if the older story had a slightly better score (obviously this will need a bit of tweaking). Is this possible?

If anyone could let me know how to achieve this using FOQElasticaBundle that would be great, but otherwise I'd appreciate it if you could let me know how to achieve this directly in elasticsearch so I can try and implement the behaviour myself and contribute to the bundle if needs be.

Thanks.

解决方案

Whew, after much experimentation and hours of trawling the Interweb I finally managed to get the desired behavior! (Full credit goes to Clinton Gormley.)

Mapping configuration:

mappings:
    title: { boost: 8 }
    summary: { boost: 5 }
    text: { boost: 3 }
    author:
    publishedAt: { type: date }

Here is the code using the PHP client, Elastica, to dynamically build the query to boost using the original mapping AND the published date:

$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);

And for those of you more interested in the raw elasticsearch query (only with 3 date ranges for brevity)...

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>"
            }
          }
        }
      ]
    }
  }
}'

这篇关于有可能使用弹性搜索来提升“最新”的项目吗? (FOQElasticaBundle)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆