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

时间:2016-02-12 16:53:40

标签: json elasticsearch

这是我的映射

{
   "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":[]
   }
}

我想查询主要产品,如果有产品有20个结果,那么返回20个产品,但如果产品没有任何匹配的退货打印机+具有匹配打印机(儿童)的产品

当我执行此查询时,对于key = tn-200,它返回20个产品,而对于key = hl-2230,仅返回打印机。它按预期工作。因为hl-2230没有任何产品匹配。

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

当我为hl-2230执行此查询时,它将返回匹配hl-2230打印机的产品。也按预期工作。

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

现在我的问题是如何将这些结合起来?我尝试使用组合bool查询和限制但是当我搜索hl-2230时,它只返回产品并且永远不会返回任何打印机。好像"应该"部分处于非活动状态,只能执行部分。因为如果我设置"值" :1表示必须查询,我得到5个结果(5个分片),"值" :2,我得到10个结果。 我不确定限制查询是否也是要走的路?请建议我。 感谢。

{
    "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": {}
}

1 个答案:

答案 0 :(得分:1)

请试试这个:

{
 "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": {}
}

希望这有帮助。