簡體   English   中英

elasticsearch嵌套范圍過濾器腳本

[英]elasticsearch nested range filter script

我有一個Elasticsearch范圍匯總問題。

我在稱為“產品”的嵌套“對象”中有一個稱為“價格”的嵌套對象。 在此子嵌套對象價格中,我針對不同的國家和貨幣使用不同的價格。 現在我想使用范圍聚合,但是這個遍歷所有價格項目並返回大范圍聚合。 現在,我想使用腳本來過濾貨幣和國家/地區價格。 但是我的if子句從來沒有返回值。

"script": "if(doc['currency']=='GBP') { doc['price']; } else 0"

這是我的查詢代碼

"aggs": {
    "products": {
        "nested": {
            "path": "products"
        },
        "aggs": {
            "prices": {
                "nested": {
                    "path": "products.prices"
                },
                "aggs": {
                    "range": {
                        "range": {
                            "field": "products.prices.price",
                            "script": "if(doc['currency']=='GBP') { doc['price']; } else 0",
                            "params": {
                                "currency": "GBP",
                                "country": "GB"
                            },
                            "ranges": [
                                {
                                    "to": 50
                                },
                                {
                                    "from": 50,
                                    "to": 100
                                },
                                {
                                    "from": 100
                                }
                            ]
                        }
                    }
                }
            }
        }
    }
}

和我的地圖

{
"settings": {
    "index": {
        "number_of_shards": 2,
        "number_of_replicas": 1
    },
    "analysis": {
        "filter": {
            "nGram_filter": {
                "type": "nGram",
                "min_gram": 2,
                "max_gram": 20,
                "token_chars": ["letter", "digit", "punctuation", "symbol"]
            }
        },
        "analyzer": {
            "nGram_analyzer": {
                "type": "custom",
                "tokenizer": "whitespace",
                "filter": ["lowercase", "asciifolding", "nGram_filter"]
            },
            "whitespace_analyzer": {
                "type": "custom",
                "tokenizer": "whitespace",
                "filter": ["lowercase", "asciifolding"]
            }
        }
    }
},
"mappings": {
    "program": {
        "properties": {
            "title": {
                "type": "string",

                "fields": {
                    "raw": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            },
            "products": {
                "type": "nested",
                "store": true,
                "index": "analyzed",
                "fields": {
                    "raw": {
                        "type": "nested",
                        "index": "not_analyzed"
                    }
                },
                "properties": {
                    "sku": {
                        "type": "string",
                        "store": true,
                        "index": "analyzed",
                        "fields": {
                            "raw": {
                                "type": "string",
                                "index": "not_analyzed"
                            }
                        }
                    },
                    "prices": {
                        "type": "nested",
                        "store": true,
                        "index": "analyzed",
                        "fields": {
                            "raw": {
                                "type": "nested",
                                "index": "not_analyzed"
                            }
                        },
                        "properties": {
                            "price": {
                                "type": "float",
                                "store": true,
                                "index": "analyzed",
                                "null_value": 0,
                                "analyzer": "english",
                                "fields": {
                                    "raw": {
                                        "type": "float",
                                        "index": "not_analyzed"
                                    }
                                }
                            },
                            "price2": {
                                "include_in_all": false,
                                "type": "float",
                                "store": true,
                                "index": "analyzed",
                                "null_value": 0,
                                "fields": {
                                    "raw": {
                                        "type": "float",
                                        "index": "not_analyzed"
                                    }
                                }
                            },
                            "vat": {
                                "include_in_all": false,
                                "type": "float",
                                "store": true,
                                "index": "analyzed",
                                "null_value": 0,
                                "fields": {
                                    "raw": {
                                        "type": "float",
                                        "index": "not_analyzed"
                                    }
                                }
                            },
                            "country": {
                                "include_in_all": false,
                                "type": "string",
                                "store": true,
                                "index": "analyzed",
                                "fields": {
                                    "raw": {
                                        "type": "string",
                                        "index": "not_analyzed"
                                    }
                                }
                            },
                            "currency": {
                                "include_in_all": false,
                                "type": "string",
                                "store": true,
                                "index": "analyzed",
                                "fields": {
                                    "raw": {
                                        "type": "string",
                                        "index": "not_analyzed"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

}

你可以試試看嗎?

 { "filtered" : { "query" : { "match_all" : {} }, "filter" : { "nested" : { "path" : "products", "filter" : { "bool" : { "must" : [ { "term" : {"prices.currency" : "GBP"} }, { "range" : {"range.count" : {"gt" : 5}} } ] } }, "_cache" : true } } } } 

此時,ElasticSearch已棄用過濾器,並已將其替換為bool。 其新版本如下:

{
"query" : {
    "nested" : {
        "path" : "products",
        "query" : {
            "bool" : {
                "must" : [
                    {
                        "term" : {"prices.currency" : "GBP"}
                    },
                    {
                        "range" : {"range.count" : {"gt" : 5}}
                    }
                ]}
            }
        }
    }
}

這是對ElasticSearch文檔的參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM