【问题标题】:elasticsearch nested range filter scriptelasticsearch 嵌套范围过滤器脚本
【发布时间】:2015-02-27 08:21:48
【问题描述】:

我有一个弹性搜索范围聚合问题。

我在一个名为“products”的嵌套“对象”中有一个名为“prices”的嵌套对象。 在这个子嵌套对象价格中,我对不同的国家和货币有不同的价格。现在我想使用范围聚合,但是这个循环遍历所有价格项目并返回一个大范围聚合。 现在我想使用脚本来过滤货币和国家价格。但我的 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"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    你可以试试这个?

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

    【讨论】:

      【解决方案2】:

      此时 ElasticSearch 已弃用 Filtered,并已将其替换为 bool。新版本如下:

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

      这是对ElasticSearch documentation的引用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-07
        • 2017-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多