2016-02-15 117 views
1

我有一個elasticsearch問題:如何結合使用Elasticsearch搜索距離的過濾器?

這裏是我下面的映射:

{ 
    "9849asdasprofiles" : { 
    "mappings" : { 
     "profile" : { 
    "properties" : { 
     "activity" : { 
     "type" : "long" 
     }, 
     "address" : { 
     "type" : "string" 
     }, 
     "cPerson" : { 
     "type" : "string" 
     }, 
     "category_id" : { 
     "type" : "long" 
     }, 
     "city" : { 
     "type" : "string" 
     }, 
     "country" : { 
     "type" : "string" 
     }, 
     "created_at" : { 
     "properties" : { 
      "date" : { 
      "type" : "string" 
      }, 
      "timezone" : { 
      "type" : "string" 
      }, 
      "timezone_type" : { 
      "type" : "long" 
      } 
     } 
     }, 
     "editors" : { 
     "properties" : { 
      "__cloner__" : { 
      "type" : "object" 
      }, 
      "__initializer__" : { 
      "type" : "object" 
      }, 
      "__isInitialized__" : { 
      "type" : "boolean" 
      } 
     } 
     }, 
     "fax" : { 
     "type" : "string" 
     }, 
     "foto_url" : { 
     "type" : "string" 
     }, 
     "has_siegel" : { 
     "type" : "long" 
     }, 
     "has_upgrade" : { 
     "type" : "long" 
     }, 
     "hsnr" : { 
     "type" : "string" 
     }, 
     "id" : { 
     "type" : "long" 
     }, 
     "info_text" : { 
     "type" : "string" 
     }, 
     "location" : { 
     "type" : "geo_point", 
     "fielddata" : { 
      "format" : "compressed", 
      "precision" : "3m" 
     } 
     }, 
     "logo_url" : { 
     "type" : "string" 
     }, 
     "name" : { 
     "type" : "string" 
     }, 
     "phone" : { 
     "type" : "string" 
     }, 
     "published" : { 
     "type" : "boolean" 
     }, 
     "role_limit_article" : { 
     "type" : "boolean" 
     }, 
     "role_limit_clicktracking" : { 
     "type" : "boolean" 
     }, 
     "role_limit_contact_fax" : { 
     "type" : "boolean" 
     }, 
     "role_limit_contact_mail" : { 
     "type" : "boolean" 
     }, 
     "role_limit_contact_phone" : { 
     "type" : "boolean" 
     }, 
     "role_limit_contact_website" : { 
     "type" : "boolean" 
     }, 
     "role_limit_create_profile" : { 
     "type" : "boolean" 
     }, 
     "role_limit_events" : { 
     "type" : "boolean" 
     }, 
     "role_limit_following" : { 
     "type" : "boolean" 
     }, 
     "role_limit_gallery" : { 
     "type" : "boolean" 
     }, 
     "role_limit_images" : { 
     "type" : "boolean" 
     }, 
     "role_limit_info_fields" : { 
     "type" : "boolean" 
     }, 
     "role_limit_logo" : { 
     "type" : "boolean" 
     }, 
     "role_limit_messages" : { 
     "type" : "boolean" 
     }, 
     "role_limit_products" : { 
     "type" : "boolean" 
     }, 
     "role_limit_view_follower" : { 
     "type" : "boolean" 
     }, 
     "role_limit_visitors" : { 
     "type" : "boolean" 
     }, 
     "role_limit_visittracking" : { 
     "type" : "boolean" 
     }, 
     "siegel_url" : { 
     "type" : "string" 
     }, 
     "slug" : { 
     "type" : "string" 
     }, 
     "street" : { 
     "type" : "string" 
     }, 
     "upgrade_level" : { 
     "type" : "long" 
     }, 
     "upgrade_sort" : { 
     "type" : "integer" 
     }, 
     "visible" : { 
     "type" : "boolean" 
     }, 
     "website" : { 
     "type" : "string" 
     }, 
     "zipcode" : { 
     "type" : "string" 
     } 
    } 
     } 
    } 
    } 
} 

對於映射數據庫條目上面我想只定義ID的條目。這些ID在一個數組中。過濾這些項目後,我想運行一個距離搜索。於是我就在下面這個查詢:

{ 
    "index": "9849asdasprofiles", 
    "type": "profile", 
    "size": 30, 
    "from": 0, 
    "body": { 
    "query": { 
     "function_score": { 
      "functions": [ 
       { 
        "linear": { 
         "location": { 
          "origin": "48.136833417046,11.570900696682", 
          "offset": "2km", 
          "scale": "1km" 
         } 
        } 
       } 
      ], 
      "score_mode": "avg", 
      "boost_mode": "replace", 
      "query": { 
       "bool": { 
        "must": [ 
         { 
          "term": { 
           "published": "1" 
          } 
         }, 
         { 
          "match": { 
           "country": "DE" 
          } 
         } 
        ], 
        "filter": { 
         "terms": { 
          "id": [ 
           5336, 
           4955, 
           5488 
          ] 
         } 
        } 
       } 
      } 
     } 
    }, 
    "aggs": { 
     "rings": { 
      "geo_distance": { 
       "field": "location", 
       "origin": "48.136833417046,11.570900696682", 
       "distance_type": "arc", 
       "unit": "km", 
       "ranges": [ 
        { 
         "to": 25 
        } 
       ] 
      } 
     } 
    }, 
    "script_fields": { 
     "distance": { 
      "lang": "groovy", 
      "params": { 
       "lat": 48.136833417046, 
       "lon": 11.570900696682 
      }, 
      "script": "doc['location'].arcDistanceInKm(lat,lon)" 
     } 
    }, 
    "sort": [ 
     { 
      "upgrade_sort": { 
       "order": "desc" 
      } 
     }, 
     { 
      "has_siegel": { 
       "order": "desc" 
      } 
     }, 
     { 
      "_geo_distance": { 
       "location": { 
        "lat": 48.136833417046, 
        "lon": 11.570900696682 
       }, 
       "order": "asc", 
       "unit": "km" 
      } 
     } 
    ] 
    }, 
    "fields": [ 
    "_source", 
    "distance" 
    ] 
} 

問題是:結果包含具有指定ID的條目,但距離過濾器不影響使用效果。

任何想法如何做到這一點?

回答

1

好吧,我明白了:解決方案非常簡單。我只需要(在PHP)過濾器部分從

$params['body']['query']['function_score']['query']['bool']['filter'] 

改變

$params['body']['query']['function_score']['filter'] 

但我找不到elastisearch文檔中的一部分... :(

相關問題