6

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

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

每页列出 25 个广告。在这 25 个中,显示的广告中有 10 个必须是“付费广告”,另外 15 个必须是“免费”。所有 25 项必须与执行的搜索相关(即关键字、地区、价格、类别等)

我知道我可以使用两个单独的查询来做到这一点,但这似乎是对资源的巨大浪费。是否可以进行“子查询”(如果您可以这样称呼它们?)并将这些结果合并到一个结果集中?不知何故,在一个查询中只能从弹性搜索中获取 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”:“Paid”15 个“advertType”:“Free”...

4

3 回答 3

7

您可以采取几种方法。

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

多搜索 API

多搜索 API 允许在同一个 API 中执行多个搜索请求。它的端点是_msearch。

请求的格式类似于批量 API 格式

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。

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

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

限制过滤器

限制过滤器限制要执行的文档数量(每个分片)。例如:

{
    "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 过滤器结合起来:

布尔过滤器

匹配与其他查询的布尔组合匹配的文档的过滤器。在概念上类似于布尔查询,除了子句是其他过滤器。可以放置在接受过滤器的查询中。

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

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

于 2014-06-25T20:12:56.057 回答
0

尝试使用限制返回的文档数量的限制过滤器

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

将值更改为 2 以获取 10 个结果,将值更改为 3 以获取 15

于 2014-06-25T13:03:14.980 回答
-4

你是问询吗?

(select * from tablename where advert = "Paid Advert" limit 10) union (select * from tablename where advert = "Free" limit 15);

产生每页限制的逻辑?

于 2014-06-25T13:08:02.280 回答