php - elasticsearch中 "union"的子查询

标签 php elasticsearch elastica

我目前正忙于一个项目,我们选择使用 Elasticsearch 作为分类网站的搜索引擎。

目前,我有以下业务规则:

List 25 adverts per page. Of these 25, 10 of the displayed adverts must be "Paid Adverts", and the other 15 must be "Free". All 25 must be relevant to the search performed (i.e. Keywords, Region, Price, Category, etc.)

我知道我可以使用两个单独的查询来做到这一点,但这似乎是对资源的巨大浪费。是否可以执行“子查询”(如果可以这样调用它们?)并将这些结果合并到一个结果集中?不知何故,在一次查询中只从 elasticsearch 获取 10 个“付费”广告和 15 个“免费”广告?当然,假设有足够多的广告来满足这个要求。

感谢您的帮助!

编辑 - 只是添加我的映射信息以更加清晰。

"properties": {
       "advertText": {
          "type": "string",
          "boost": 2,
          "store": true,
          "analyzer": "snowball"
       },
       "canonical": {
          "type": "string",
          "store": true
       },
       "category": {
          "properties": {
             "id": {
                "type": "string",
                "store": true
             },
             "name": {
                "type": "string",
                "store": true
             },
             "parentCategory": {
                "type": "string",
                "store": true
             }
          }
       },
       "contactNumber": {
          "type": "string",
          "index": "not_analyzed",
          "store": true
       },
       "emailAddress": {
          "type": "string",
          "store": true,
          "analyzer": "url_email_analyzer"
       },
       "advertType": {
          "type": "string",
          "index": "not_analyzed"
       },
       ...
}

然后我想要的是能够查询这个并获得 10 个结果,其中“advertType”:“付费” 15 个,其中“advertType”:“免费”...

最佳答案

您可以采用几种方法。

首先,您可以尝试使用多搜索 API:

Multi Search API

The multi search API allows to execute several search requests within the same API. The endpoint for it is _msearch.

The format of the request is similar to the bulk API format

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html

一个基本的例子:

curl -XGET 'http://127.0.0.1:9200/advertising_index/_msearch?pretty=1'  -d '
{}
{"query" : {"match" : {"Paid_Ads" : "search terms"}}, "size" : 10}
{}
{"query" : {"match" : {"Free" : "search terms"}}, "size" : 15}
'

我已经编写了字段和查询,但总的来说您应该明白了 - 您点击了 _msearch 端点并将一系列以空括号 {} 开头的查询传递给它。对于付费,我将大小设置为 10,对于免费,我将大小设置为 15。

根据您自己的实现细节,您应该能够使用类似这样的东西。

如果出于某种原因这不起作用,您也可以尝试使用限制过滤器:

Limit Filter

A limit filter limits the number of documents (per shard) to execute on. For example:

{
    "filtered" : {
        "filter" : {
             "limit" : {"value" : 100}
         },
         "query" : {
            "term" : { "name.first" : "shay" }
        }
    }
}

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-limit-filter.html

请注意,限制是针对每个分片的,而不是针对每个索引的。给定每个索引 5 个主分片的默认值,要获得 10 的总响应,您可以将限制设置为 2 (2X5 == 10)。另请注意,如果您在一个分片上有多个匹配项但在另一个分片上没有匹配项,这可能会产生不完整的结果。

然后您可以将两个过滤器与一个 bool 过滤器结合起来:

Bool Filter

A filter that matches documents matching boolean combinations of other queries. Similar in concept to Boolean query, except that the clauses are other filters. Can be placed within queries that accept a filter.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-filter.html

我没有详细说明这一点,因为它需要有关您的特定索引、映射、数据和查询的更多信息。

关于php - elasticsearch中 "union"的子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24409001/

相关文章:

javascript - 如何让php输入后运行js?

PHP - 无法加载动态库 '/usr/lib/php/20151012/php_curl.dll' -/usr/lib/php/20151012/php_curl.dll

php - 使用数据库(最好是mysql)在php和nodejs之间共享 session

scala - elastic4s中找不到HttpClient?

php - elasticsearch排序根本不起作用

elasticsearch - 弹性过滤器不工作

php - 在 MySQL 中连接 3 个表

pagination - 对 Elasticsearch 聚合结果进行分页

elasticsearch - 添加日期字段和 bool 值?以现有Elasticsearch文档的名义

arrays - fos elasticabundle 映射类型 "array"