简体   繁体   English

ElasticSearch-如何限制每个组合查询的大小?

[英]ElasticSearch- How to limit size of the each combined query?

Here is my Mapping 这是我的地图

{
   "state":"open",
   "settings":{
      "index":{
         "creation_date":"1453816191454",
         "number_of_shards":"5",
         "number_of_replicas":"1",
         "version":{
            "created":"1070199"
         },
         "uuid":"TfMJ4M0wQDedYSQuBz5BjQ"
      }
   },
   "mappings":{
      "Product":{
         "properties":{
            "index":"not_analyzed",
            "store":true,
            "type":"string"
         },
         "ProductName":{
            "type":"nested",
            "properties":{
               "Name":{
                  "store":true,
                  "type":"string"
               }
            }
         },
         "ProductCode":{
            "type":"string"
         },
         "Number":{
            "index":"not_analyzed",
            "store":true,
            "type":"string"
         },
         "id":{
            "index":"no",
            "store":true,
            "type":"integer"
         },
         "ShortDescription":{
            "store":true,
            "type":"string"
         },
         "Printer":{
            "_routing":{
               "required":true
            },
            "_parent":{
               "type":"Product"
            },
            "properties":{
               "properties":{
                  "RelativeUrl":{
                     "index":"no",
                     "store":true,
                     "type":"string"
                  }
               }
            },
            "PrinterId":{
               "index":"no",
               "store":true,
               "type":"integer"
            },
            "Name":{
               "store":true,
               "type":"string"
            }
         }
      },
      "aliases":[]
   }
}

I would like to query mainly Products and if there products have 20 results, then return 20 products but if Products dont have any matching return printers+products having matching printers(childs) 我想主要查询产品,如果有产品有20个结果,则返回20个产品,但如果产品没有任何匹配的返回打印机+具有匹配的打印机(子代)的产品

When I execute this query, for key=tn-200, it returns 20 products and for key=hl-2230 returns me only printers. 当我执行此查询时,对于key = tn-200,它将返回20种产品,对于key = hl-2230,将仅返回打印机。 It works as expected. 它按预期工作。 because hl-2230 doesnt have any products matching. 因为hl-2230没有任何匹配的产品。

{
    "query": {
        "bool": {
            "should": [{
                "query_string": {
                    "default_field": "_all",
                    "query": "key"
                }
            }],
            "must_not": [],
            "must": []
        }
    },
    "from": 0,
    "size": 20,
    "sort": [],
    "aggs": {}
}

when I execute this query for hl-2230, it will return me products of matching hl-2230 printer. 当我对hl-2230执行此查询时,它将返回与hl-2230打印机匹配的产品。 Also works as expected. 也按预期工作。

{ 
    "query": {
        "has_child": {
            "type": "Printer",
            "query": { 
                "match": {
                    "Name": "HL-2230"
                }
            }
        }
    },
    "from": 0,
    "size": 20,
    "sort": [],
    "aggs": {}
}

Now my questions is how to combine those? 现在我的问题是如何将这些结合起来? I tried to use combined bool query with limit but when I search hl-2230, it only returns products and never returns any printers. 我尝试将合并的布尔查询与限制一起使用,但是当我搜索hl-2230时,它仅返回产品,而从不返回任何打印机。 As if "should" part is inactive and only must part is executed. 好像“应该”部分处于非活动状态,仅必须执行部分。 because If I set "value" : 1 for the must query, I get 5 results (5 shards), "value" : 2, I get 10 results. 因为如果我为must查询设置“ value”:1,则会得到5个结果(5个分片),“ value”:2,则会得到10个结果。 I am not sure if the limit query is the way to go also? 我不确定极限查询是否也是可行的方式? Please advise me. 请建议我。 thanks. 谢谢。

{
    "query": {
        "bool": {
            "should": [{
                "filtered" : {
                    "filter" : {
                        "limit" : {
                            "value" : 20
                        }
                    },
                    "query": {
                        "multi_match": {
                            "type": "best_fields",
                            "query": "hl-2230",
                            "fields": [
                                "ManufactureNumber^5",
                                "Number^4",
                                "Name^3"
                            ]
                        }
                    }
                }
            }],
            "must": [{
                "filtered" : {
                    "filter" : {
                        "limit" : {
                            "value" : 1
                        }
                    },
                    "query": {
                        "has_child": {
                            "type": "Printer",
                            "query": {
                                "match": {
                                    "Name": "HL-2230"
                                }
                            }
                        }
                    }
                }
            }]
        }  
    },
    "from": 0,
    "size": 20,
    "sort": [],
    "aggs": {}
}

PLease try this: 请尝试以下方法:

{
 "query": {
  "bool": {
     "should": [
        {
           "multi_match": {
              "type": "best_fields",
              "query": "hl-2230",
              "fields": [
                 "ManufactureNumber^5",
                 "Number^4",
                 "Name^3"
              ]
           }
        },
        {
           "has_child": {
              "type": "Printer",
              "query": {
                 "match": {
                    "Name": "HL-2230"
                 }
              }
           }
        }
     ]
    }
 },
 "size": 20,
 "sort": [],
 "aggs": {}
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM