是否可以使用弹性搜索提升“最新"项目?(FOQElasticaBundle) [英] Is it possible to boost 'newest' items using elasticsearch? (FOQElasticaBundle)

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

问题描述

我目前正在通过 FOQElasticaBundle 在我的 Symfony2 应用程序中实现 elasticsearch,到目前为止它运行良好基于应用于我的故事"实体各个领域的提升.这是配置:

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: AcmeBundleStoryBundleEntityStory
                        provider:
                            query_builder_method: createIsActiveQueryBuilder
                        listener:
                            service: acme_story.search_index_listener
                        finder:

但是,我还想根据故事的published_at"日期应用提升,以便昨天发布的故事会出现在 6 个月前发布的故事之前的结果中 - 即使较旧的故事有稍微好一点的分数(显然这需要一些调整).这可能吗?

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?

如果有人能让我知道如何使用 FOQElasticaBundle 实现这一点,那就太好了,否则,如果您能让我知道如何直接在 elasticsearch 中实现这一点,我将不胜感激,这样我就可以尝试自己实现该行为,并且如果需要,请为捆绑包做出贡献.

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.

谢谢.

推荐答案

哇,经过大量的实验和数小时的浏览互联网后,我终于获得了想要的行为!(完全归功于 Clinton Gormley.)

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

映射配置:

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

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

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

对于那些对原始 elasticsearch 查询更感兴趣的人(为简洁起见,只有 3 个日期范围)...

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天全站免登陆