查找文本等于且在请求范围内的产品 [英] Find products which have text equals and is between a range of the request

查看:53
本文介绍了查找文本等于且在请求范围内的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用以下内容将我的产品编入索引

I have my product indexed with the following

product_id | key | text   | number
1          | A1  | pc     | <null>
1          | A1  | mac    | <null>
1          | A2  | <null> | 23
1          | A2  | <null> | 30
2          | A1  | pc     | <null>
3          | A2  | <null> | 25
4          | A2  | <null> | 32
4          | A1  | linux  | <null>

现在我想在哪里找到产品

Now I want to find the products where

  • key = A1 text pc mac
  • key = A2 number 22 28
  • key = A1 and text is either pc or mac
  • key = A2 and number is between 22 and 28

这应该给我 product_id 1 2 3 ,但不能给product_id 4,因为它不是在范围内,也没有在 A1

This should give me product_id 1, 2 and 3, but not product_id 4, because its not inside the range nor have A1 the selected key in A1

我的索引是

text: { type: keyword, index: not_analyzed }
number: { type: float, index: not_analyzed }
product_id: { type: integer, index: not_analyzed }
key: { type: keyword, index: not_analyzed }

如果仅选择 text ,以下内容将很完美

The following works perfect, if only text are selected

{
   "query": {
      "bool": {
         "must": [
            {
               "term": {
                  "key": "A1"
               }
            },
            {
               "terms": {
                  "text": [
                     "mac",
                     "pc"
                  ]
               }
            }
         ]
      }
   },
   "aggs": {
      "BUCKET_NAME": {
         "terms": {
            "field": "text",
            "min_doc_count":2
         }
      }
   }
}

但是,如果我将范围放进去,它将不再起作用

But if I put my range in, it does not work any more

推荐答案

您可以在下面的should子句中同时使用这两个条件,然后简单地应用聚合.

You can use your both conditions in a should clause like below and then simply apply aggregation.

{
   "size": 0,
   "query": {
      "bool": {
         "should": [
            {
               "bool": {
                  "must": [
                     {
                        "term": {
                           "key": "A1"
                        }
                     },
                     {
                        "terms": {
                           "text": [
                              "pc",
                              "mac"
                           ]
                        }
                     }
                  ]
               }
            },
            {
               "bool": {
                  "must": [
                     {
                        "term": {
                           "key": "A2"
                        }
                     },
                     {
                        "range": {
                           "number": {
                              "gte": 22,
                              "lte": 28
                           }
                        }
                     }
                  ]
               }
            }
         ]
      }
   },
   "aggs": {
      "PRODUCT_IDs": {
         "terms": {
            "field": "product_id"
         }
      }
   }
}

这篇关于查找文本等于且在请求范围内的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆