2017-05-30 108 views
0

我的基本問題是我有三個單獨的查詢執行空間,時間和關鍵字搜索。我想按照以下用例描述的方式將它們組合成一個查詢:將3個彈性搜索查詢合併爲一個

用戶輸入用於搜索文檔的關鍵字。該查詢返回某些文檔。然後用戶通過空間查詢來縮小搜索範圍,其中存在空間查詢,然後通過時間搜索進一步縮小結果範圍。

關鍵字查詢

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


         ] 
        } 
       } 
+2

把他們都在一個'布爾/ must'並有亞去:-) – Val

+0

@val把它們放在一起會限制用戶輸入所有T他立即要求三項要求。而我希望他只是在開始時輸入關鍵字,然後他可以基於時間或空間查詢縮小那些結果(具有該關鍵字)。也許某種OR運算符。? – Rehan

+0

聽起來好像你需要一點一點構建查詢......所以按照建議使用'bool/must',只要用戶指定它就在其中包含新的約束 – Val

回答

0

我用一個布爾值/匹配運營商合併單個查詢爲用戶提供針對一定的時間特定地點特定關鍵字的文檔所有查詢

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