27

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

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 实现这一点,那就太好了,但是如果你能让我知道如何直接在弹性搜索中实现这一点,我将不胜感激,这样我就可以自己尝试实现该行为并为如果需要,捆绑。

谢谢。

4

3 回答 3

47

唷,经过大量的实验和数小时的互联网拖网,我终于设法获得了所需的行为!(全部归功于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);

对于那些对原始 elasticsearch 查询更感兴趣的人(为简洁起见,仅提供 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>"
            }
          }
        }
      ]
    }
  }
}'
于 2012-09-06T12:09:51.970 回答
34

您可以使用衰减评分函数来降低评分与时间的关系:

{
 "query": {
 "function_score": {
    "functions": [
     {
      "linear": {
        "pubdate": {
          "origin": 1398673886,
          "scale": "1h",
          "offset": 0,
          "decay": 0.1
        }
      }
    }
    ]
   }
  }
 }
于 2014-04-28T09:30:44.413 回答
14

基于function_score的完整elasticsearch 5示例。有关更多信息,请参阅此博客文章function_score 文档

允许在没有“硬截止”的高斯曲线上基于多个日期范围以不同的强度提升最近的条目。

{
    "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 >" }
            }
        }
    }
}
于 2017-05-04T09:49:13.240 回答