2016-02-12 48 views
0

這裏是我的映射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時,它只返回產品並且從不返回任何打印機。好像「應該」部分是不活動的並且只有部分被執行。因爲如果我爲必須查詢設置「value」:1,我得到5個結果(5個分片),「value」: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

請試試這個:

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

希望這有助於。