【问题标题】:Combining 3 elasticsearch queries into one将 3 个 elasticsearch 查询合二为一
【发布时间】:2017-06-21 18:56:33
【问题描述】:

我的基本问题是我有三个单独的查询来执行空间、时间和关键字搜索。我想按照以下用例描述的方式将它们组合成一个查询:

用户输入关键字以搜索文档。查询返回某些文档。然后,用户通过空间搜索来缩小搜索范围,其中存在空间查询,然后通过时间搜索进一步缩小结果范围。

关键字查询

"query": {
    "match" : { "metadata.o2r.title" : "geosciences" } 
  }

空间查询

   {
                            "query": {
                                    "bool": {
                                            "must": {
                                                    "match_all": {}
                                            },
                                            "filter": {
                                                    "geo_shape": {
                                                            "metadata.o2r.spatial.geometry": {
                                                                    "shape": {
                                                                            "type": "polygon",
                                                                            "coordinates":
                                                                                coords


                                                                    },
                                                                    "relation": "within"
                                                            }
                                                    }
                                            }
                                    }
                            }
                    }

时间查询

{
                "query": {
                    "bool": {
                        "must": [{
                                "range": {
                                    "metadata.o2r.temporal.begin": {
                                        "from": lower
                                    }
                                }
                            },
                            {
                                "range": {
                                    "metadata.o2r.temporal.end": {
                                        "to": upper
                                    }
                                }
                            }
                        ]
                    }
                }
            }

基本思想是通过单个查询为给定位置在特定时间段内提供具有特定关键字的文档

组合查询

"query": {
                    "bool": {
                        "must": [  {
                            "match" : { "metadata.o2r.title" : "geosciences"
                          }
                        },
                        {
                        "filter": {
                            "geo_shape": {
                                "metadata.o2r.spatial.geometry": {
                                    "shape": {
                                        "type": "polygon",
                                        "coordinates": coords


                                    },
                                    "relation": "within"
                                }
                            }
                        }
                      },
                          {
                                "range": {
                                    "metadata.o2r.temporal.begin": {
                                        "from": lower
                                    }
                                }
                            },
                            {
                                "range": {
                                    "metadata.o2r.temporal.end": {
                                        "to": upper
                                    }
                                }
                            }


                        ]
                    }
                }

【问题讨论】:

  • 将它们全部放在bool/must 中,然后就可以了 :-)
  • @Val 将它们放在一起将限制用户一次输入所有三个要求。虽然我希望他只是在开始时输入关键字,然后他可以根据时间或空间查询缩小这些结果(具有该关键字)。也许是某种 OR 运算符。?
  • 听起来您需要逐位构造查询...所以按照建议使用bool/must,并在用户指定后立即将新约束包含在其中

标签: elasticsearch


【解决方案1】:

我使用 bool/match 运算符来组合所有查询

{
                "query": {
                    "bool": {
                        "must": [
                          {
                                "range": {
                                    "metadata.o2r.temporal.begin": {
                                        "from": from
                                    }
                                }
                            },
                            {
                                "range": {
                                    "metadata.o2r.temporal.end": {
                                        "to": to
                                    }
                                }
                            },
                            {
                                "bool": {
                                    "must": {
                                        "match_all": {}
                                    },
                                    "filter": {
                                        "geo_shape": {
                                            "metadata.o2r.spatial.geometry": {
                                                "shape": {
                                                    "type": "polygon",
                                                    "coordinates": coords


                                                },
                                                "relation": "within"
                                            }
                                        }
                                    }
                                }
                            },
                            {
    "match" : { "metadata.o2r.title" : "geosciences" }
  }
                        ]
                    }
                }
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    相关资源
    最近更新 更多