1. 程式人生 > >Solr針對空間搜尋的支援

Solr針對空間搜尋的支援

Spatial Search

Solr支援在地理空間搜尋中使用位置資料,使用空間搜尋,你可以:

  • 索引點或者其他形狀
  • 通過矩形框,圓或者其他形狀來過濾搜尋結果
  • 通過點之間的距離 或者兩個區域之間的矩形之間的距離,排序或者增強分數
  • 產生一個二維的網格來生成熱力圖或者點繪製

有四個欄位型別來進行空間搜尋:

  • LatLonPointSpatialField
  • LatLonType (已經過期)
  • SpatialRecursivePrefixTreeFieldType (簡稱RPT ),包括RptWithGeometrySpatialField
  • BBoxField

LatLonPointSpatialField 是一般點位搜尋的最常用的型別。它替代了 LatLonType ,為了相容LatLonType 還會存在。RPT提供了具有一些高階的特點,例如使用多邊形和熱力圖。
RptWithGeometrySpatialField 用來索引和檢索非點位資料,儘管它也可以辦到。它不可以進行排序和增強。
BBoxField是進行盒子索引,通過盒子查詢 ,制定具體的操作(Intersects,Within,Contains,Disjoint,Equals),或者相關的排序、增強比如overlapRatio或者區域。
一些深奧的詳細情況可以在下面連結中找到

http://wiki.apache.org/solr/SpatialSearch

LatLonPointSpatialField

下面是LatLonPointSpatialField 在Schema中如何配置:

<fieldType name="location" class="solr.LatLonPointSpatialField" docValues="true"/>

LLPSF支援在 indexed, stored, docValues, 和multiValued的切換。 LLPSF 內部使用 Lucene “Points” 進行索引. 當啟用”docValues” 的時候, 經度和緯度位元交織為64位,存入Lucene DocValues中。docValues 資料精度達到釐米級。

Indexing Points

為了索引大地點(經度和緯度),需要以這樣的格式”lat,lon”提供。
所以非大地點位,需要看情況。RPT使用”x y”(空格分隔),PointType 然而使用”x,y” (逗號分隔)。
如果你寧願使用工業標準的格式, Solr 支援 WKT 和GeoJSON。然而對於簡單的資料,它們過於笨重。 (過期的型別 LatLonType 或者PointType並不支援)。

Searching with Query Parsers

Solr中存在兩個空間搜尋查詢解析器: geofilt 和bbox。 它們接收以下引數:

引數 描述
sfield 空間索引欄位
score (高階選項 LatLonType 或者 PointType並不支援),如果這一查詢在評分的環境中使用(如同主查詢中的q),這一本地引數決定分數如何產生,它的有效值包括:none 、kilometers 、miles 、degrees 、distance 、recipDistance 。(Don’t use this for indexed non-point shapes (e.g. polygons). The results will be erroneous. And with RPT, it’s only recommended for multi-valued point data, as the implementation doesn’t scale very well and for single-valued fields, you should instead use a separate non-RPT field purely for distance sorting.)當使用BBoxField的時候,支援一些額外的引數:overlapRatio 、area 和area2D
pt 中心點座標 “lat,lon” 。 Otherwise, “x,y” for PointType or “x y” for RPT field types.
filter (Advanced option; not supported by LatLonType (deprecated) or PointType). If you only want the query to score (with the above score local parameter), not filter, then set this local parameter to false.
d the radial distance, usually in kilometers. (RPT & BBoxField can set other units via the setting distanceUnits)

geofilt

The geofilt filter allows you to retrieve results based on the geospatial distance (AKA the “great circle distance”) from a given point. Another way of looking at it is that it creates a circular shape filter. For example, to find all documents within five kilometers of a given lat/lon point, you could enter &q=:&fq={!geofilt sfield=store}&pt=45.15,-93.85&d=5. This filter returns all results within a circle of the given radius around the initial point:

bbox

The bbox filter is very similar to geofilt except it uses the bounding box of the calculated circle. See the blue box in the diagram below. It takes the same parameters as geofilt. Here’s a sample query: &q=:&fq={!bbox sfield=store}&pt=45.15,-93.85&d=5. The rectangular shape is faster to compute and so it’s sometimes used as an alternative to geofilt when it’s acceptable to return points outside of the radius. However, if the ideal goal is a circle but you want it to run faster, then instead consider using the RPT field and try a large “distErrPct” value like 0.1 (10% radius). This will return results outside the radius but it will do so somewhat uniformly around the shape.

Filtering by an arbitrary rectangle

Sometimes the spatial search requirement calls for finding everything in a rectangular area, such as the area covered by a map the user is looking at. For this case, geofilt and bbox won’t cut it. This is somewhat of a trick, but you can use Solr’s range query syntax for this by supplying the lower-left corner as the start of the range and the upper-right corner as the end of the range. Here’s an example: &q=:&fq=store:[45,-94 TO 46,-93]. LatLonType (deprecated) does not support rectangles that cross the dateline. For RPT and BBoxField, if you are non-geospatial coordinates (geo=”false”) then you must quote the points due to the space, e.g. “x y”.

Optimizing: Cache or Not

It’s most common to put a spatial query into an “fq” parameter – a filter query. By default, Solr will cache the query in the filter cache. If you know the filter query (be it spatial or not) is fairly unique and not likely to get a cache hit then specify cache=”false” as a local-param as seen in the following example. The only spatial types which stand to benefit from this technique are LatLonPointSpatialField and LatLonType (deprecated). Enable docValues on the field (if it isn’t already). LatLonType (deprecated) additionally requires a cost=”100” (or more) local-param.
&q=…mykeywords…&fq=…someotherfilters…&fq={!geofilt cache=false}&sfield=store&pt=45.15,-93.85&d=5
LLPSF does not support Solr’s “PostFilter”.

Distance Sorting or Boosting (Function Queries)

There are four distance function queries: geodist, see below, usually the most appropriate; dist , to calculate the p-norm distance between multi-dimensional vectors; hsin , to calculate the distance between two points on a sphere; and sqedist , to calculate the squared Euclidean distance between two points. For more information about these function queries, see the section on Function Queries.

geodist

geodist is a distance function that takes three optional parameters: (sfield,latitude,longitude). You can use the geodist function to sort results by distance or score return results.
For example, to sort your results by ascending distance, enter …&q=:&fq={!geofilt}&sfield=store&pt=45.15,-93.85&d=50&sort=geodist() asc.
To return the distance as the document score, enter …&q={!func}geodist()&sfield=store&pt=45.15,-93.85&sort=score+asc.

More Examples

Here are a few more useful examples of what you can do with spatial search in Solr.

Use as a Sub-Query to Expand Search Results

Here we will query for results in Jacksonville, Florida, or within 50 kilometers of 45.15,-93.85 (near Buffalo, Minnesota):
&q=:&fq=(state:”FL” AND city:”Jacksonville”) OR {!geofilt}&sfield=store&pt=45.15,-93.85&d=50&sort=geodist()+asc

Facet by Distance

To facet by distance, you can use the Frange query parser:
&q=:&sfield=store&pt=45.15,-93.85&facet.query={!frange l=0 u=5}geodist()&facet.query={!frange l=5.001 u=3000}geodist()
There are other ways to do it too, like using a {!geofilt} in each facet.query.

Boost Nearest Results

Using the DisMax or Extended DisMax, you can combine spatial search with the boost function to boost the nearest results:
&q.alt=:&fq={!geofilt}&sfield=store&pt=45.15,-93.85&d=50&bf=recip(geodist(),2,200,20)&sort=score desc

相關推薦

Solr針對空間搜尋支援

Spatial Search Solr支援在地理空間搜尋中使用位置資料,使用空間搜尋,你可以: 索引點或者其他形狀 通過矩形框,圓或者其他形狀來過濾搜尋結果 通過點之間的距離 或者兩個區域之間的矩形之間的距離,排序或者增強分數 產生一個二維的網格來生成熱力

基於Solr空間搜尋

概括:     最近一個專案需要基於LBS查詢附近的商鋪資訊,看了一下網上都是基於Solr和ELS方式來實現, 本來想使用ELS來實現的,但是由於專案以前用的是Solr, 所以就去調研了一下基於Solr來實現地理位置的搜尋,並且在實現的時候整理了一下實現的筆記。       

MySql5.7InnoDB全文索引(針對中文搜尋

來源:https://www.2cto.com/database/201704/622275.html   1、ngram and MeCab full-text parser plugins 全文檢索在MySQL裡面很早就支援了,只不過一直以來只支援英文。緣由是他從來都使用空

《商城專案05》--用Solr實現商品搜尋功能

一, Solr的安裝配置 1, 下載資源  (solr-4.10.3.tgz.tgz) 貼個連結參考:  連結:https://pan.baidu.com/s/1rMkFTdoSALB8Q-7jCcb4YA  提取碼:6ken   

solr站內搜尋之suggest和詞頻統計

solr站內搜尋與詞頻統計 一、suggest關鍵字搜尋(帶聯想建議提示功能) 二、autocompleter外掛的使用 三、詞頻統計   package com.product.pojo; import java.io.Serializable; import java.util

Solr實現商城搜尋高亮顯示

package com.pinyougou.search.service.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; im

solr極速搜尋

一 。solr簡介 solr是以lucene為核心開發的企業級搜尋應用 應用程式可以通過http請求方式來提交索引,查詢索引,提供了比lucene更豐富的查詢語言,是 一個高效能,高可用環境全文搜尋引擎 二 。solr安裝配置 1》下載solr安裝包 solr所有版本

SSM+solr 通過商品搜尋學習solr的簡單使用

  學習了一下https://github.com/TyCoding/ssm-redis-solr這個github上的solr搜尋功能,現在來記錄一下。 我的理解就是solr有點類似於資料庫,但它是有索引的資料庫,按很多欄位建立索引,可能是b+樹或者雜湊索引,然後就能夠實現海量資料的查詢。sol

solr synonyms.txt不支援輸入中文

為了實現實際使用中有些簡稱的準確匹配,這個時候我們就需要定義一些同義詞,具體做法就是在solr自帶的synonyms.txt檔案中填寫我們想要的縮寫與全稱對應關係: 配置完成後需要重啟solr 對應core,如下圖所示: reload solr core時提示 org.a

如何在thinkPHP5中使用mongoDB中空間搜尋進行位置範圍查詢

在很多場景我們都會使用位置範圍服務,如查詢附近的單車、紅包數量等。網上已有很多關於mongoDB空間搜尋的文章,由於thinkPHP的使用人還是比較多的,但還沒有關於thinkPHP5中如何使用的相關文章。thinkPHP5中的查詢條件已經預設擁有了near查詢處理,但結果

.NET Core採用的全新配置系統[9]: 為什麼針對XML的支援不夠好?如何改進?

物理檔案是我們最常用到的原始配置的載體,最佳的配置檔案格式主要由三種,它們分別是JSON、XML和INI,對應的配置源型別分別是JsonConfigurationSource、XmlConfigurationSource和IniConfigurationSource。但是對於.NET Core的配置系統來說,

android針對c++ stl支援的配置

Android NDK從r5b版本開始有官方支援的STL了,有一個crystax版本早已經支援。官方的支援有兩個版本一個是gnu的,一個是stlport。如果你需要在你的NDK程式中使用STL,那麼需要在Applica

狀態空間搜尋——八數碼問題 Java實現

狀態空間搜尋——八數碼問題  實驗報告 【注】原始碼將以附件的形式上傳,其中EightPuzzle.java為vo類,EightPuzzleOperator.java為util類,EightPuzzleAlgorithm.java為演算法實現類。Main函式在Eight

Solr(全文搜尋功能)的介紹,安裝及配置

Solr(全文搜尋功能) Solr是什麼? Solr 是Apache下的一個頂級開源專案,採用Java開發,它是基於Lucene的全文搜尋伺服器。Solr提供了比Lucene更為豐富的查詢語言,同時實現了可配置、可擴充套件,並對索引、搜尋效能進行了優

ios搜尋(可實現模糊搜尋 支援拼音檢索 首字母等)

一、搜框中輸入關鍵字的事件響應 -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ NSLog(@"輸入的關鍵字是---%@---%lu",s

Solr分面搜尋(Faceting)

分面搜尋(faceting)是基於索引詞,將搜尋結果組織到不同的分類(categories)中。表示為索引詞(terms),以及一個數值代表各個詞匹配的文件數。 *分面的欄位屬性:indexed或docValues之一必須為true, 但不是都必須為true. docVal

Solr空間索引

一、Solr空間搜尋的目的 (1)索引空間點資料和其他形狀的資料 (2)通過圓形、正方形或者其他形狀進行過濾搜尋結果 (3)通過兩個點之間的距離或者是兩個多邊形的形狀進行排序或者評分 二、Solr空間搜尋的域型別(FieldType) 1 、LatLonType與POINT

Solr分詞搜尋結果不準確

Solr的schema.xml預設配置分詞後條件取 OR 例如:大眾1.6T  系統會自動分詞為  【大眾】 【1.6T】(ps:不同分詞器分詞效果不同)   會搜尋出包含 【大眾 OR  1.6T】 的結果。 想要讓Solr搜尋預設為    【大眾 AND 1.6T

狀態空間搜尋——八數碼問題 Ja…

實驗一 狀態空間搜尋——八數碼問題 實驗報告 【注】原始碼將以附件的形式上傳,其中EightPuzzle.java為vo類,EightPuzzleOperator.java為util類,EightPuzzleAlgorithm.java為演算法實現類。Main函式在EightPuzzleA

MySql5.7 InnoDB全文索引(針對中文搜尋

MySql5.7 建立全文索引 1、ngram and MeCab full-text parser plugins 全文檢索在MySQL裡面很早就支援了,只不過一直以來只支援英文。緣由是他從來都使用空格來作為分詞的分隔符,而對於中文來講,顯然用空格就不合適