经过大量的实验和数小时的拖曳Interweb的努力,我终于设法达到了期望的行为!(值得称赞的是克林顿·戈姆利。)
映射配置:
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);$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>" } } } ] } }}'


