使用Lucene索引和檢索POI資料

來源:互聯網
上載者:User

標籤:xtree   lis   catch   提高   should   grid   rbo   gre   sum   

使用Lucene索引和檢索POI資料 

摘要: 1、簡介 關於空間資料搜尋,以前寫過《使用Solr進行空間搜尋》這篇文章,是基於Solr的GIS資料的索引和檢索。 Solr和Elasticsearch這兩者都是基於Lucene實現的,兩者都可以進行空間搜尋(Spatial Search),在有些情境,我們需要把Lucene嵌入到已有的系統提供...

1、簡介

關於空間資料搜尋,以前寫過《使用Solr進行空間搜尋》這篇文章,是基於Solr的GIS資料的索引和檢索。

Solr和Elasticsearch這兩者都是基於Lucene實現的,兩者都可以進行空間搜尋(Spatial Search),在有些情境,我們需要把Lucene嵌入到已有的系統提供資料索引和檢索的功能,這篇文章介紹下用Lucene如何索引帶有經緯度的POI資訊並進行檢索。

2、環境資料

Lucene版本:5.3.1

POI資料庫:Base_Station測試資料,每條資料主要是ID,經緯度和地址。

3、實現

基本變數定義,這裡對“地址”資訊進行了分詞,分詞使用了Lucene內建的smartcnSmartChineseAnalyzer。

    private String indexPath = "D:/IndexPoiData";    private IndexWriter indexWriter = null;    private SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer(true);    private IndexSearcher indexSearcher = null;    // Field Name    private static final String IDFieldName = "id";    private static final String AddressFieldName = "address";    private static final String LatFieldName = "lat";    private static final String LngFieldName = "lng";    private static final String GeoFieldName = "geoField";        // Spatial index and search    private SpatialContext ctx;    private SpatialStrategy strategy;    public PoiIndexService() throws IOException {        init();    }    public PoiIndexService(String indexPath) throws IOException {        this.indexPath = indexPath;        init();    }        protected void init() throws IOException {        Directory directory = new SimpleFSDirectory(Paths.get(indexPath));        IndexWriterConfig config = new IndexWriterConfig(analyzer);        indexWriter = new IndexWriter(directory, config);        DirectoryReader ireader = DirectoryReader.open(directory);        indexSearcher = new IndexSearcher(ireader);        // Typical geospatial context        // These can also be constructed from SpatialContextFactory        ctx = SpatialContext.GEO;        int maxLevels = 11; // results in sub-meter precision for geohash        // This can also be constructed from SpatialPrefixTreeFactory        SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);        strategy = new RecursivePrefixTreeStrategy(grid, GeoFieldName);    }

索引資料

    public boolean indexPoiDataList(List<PoiData> dataList) {        try {            if (dataList != null && dataList.size() > 0) {                List<Document> docs = new ArrayList<>();                for (PoiData data : dataList) {                    Document doc = new Document();                    doc.add(new LongField(IDFieldName, data.getId(), Field.Store.YES));                    doc.add(new DoubleField(LatFieldName, data.getLat(), Field.Store.YES));                    doc.add(new DoubleField(LngFieldName, data.getLng(), Field.Store.YES));                    doc.add(new TextField(AddressFieldName, data.getAddress(), Field.Store.YES));                    Point point = ctx.makePoint(data.getLng(),data.getLat());                    for (Field f : strategy.createIndexableFields(point)) {                        doc.add(f);                    }                    docs.add(doc);                }                indexWriter.addDocuments(docs);                indexWriter.commit();                return true;            }            return false;        } catch (Exception e) {            log.error(e.toString());            return false;        }    }

這裡的PoiData是個普通的POJO。

檢索圓形範圍內的資料,按距離從近到遠排序:

    public List<PoiData> searchPoiInCircle(double lng, double lat, double radius){        List<PoiData> results= new ArrayList<>();        Shape circle = ctx.makeCircle(lng, lat, DistanceUtils.dist2Degrees(radius, DistanceUtils.EARTH_MEAN_RADIUS_KM));        SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, circle);        Query query = strategy.makeQuery(args);        Point pt = ctx.makePoint(lng, lat);        ValueSource valueSource = strategy.makeDistanceValueSource(pt, DistanceUtils.DEG_TO_KM);//the distance (in km)        Sort distSort = null;        TopDocs docs = null;        try {            //false = asc dist            distSort = new Sort(valueSource.getSortField(false)).rewrite(indexSearcher);            docs = indexSearcher.search(query, 10, distSort);        } catch (IOException e) {            log.error(e.toString());        }                if(docs!=null){            ScoreDoc[] scoreDocs = docs.scoreDocs;            printDocs(scoreDocs);            results = getPoiDatasFromDoc(scoreDocs);        }                return results;    }    private List<PoiData> getPoiDatasFromDoc(ScoreDoc[] scoreDocs){        List<PoiData> datas = new ArrayList<>();        if (scoreDocs != null) {            //System.out.println("總數:" + scoreDocs.length);            for (int i = 0; i < scoreDocs.length; i++) {                try {                    Document hitDoc = indexSearcher.doc(scoreDocs[i].doc);                    PoiData data = new PoiData();                    data.setId(Long.parseLong((hitDoc.get(IDFieldName))));                    data.setLng(Double.parseDouble(hitDoc.get(LngFieldName)));                    data.setLat(Double.parseDouble(hitDoc.get(LatFieldName)));                    data.setAddress(hitDoc.get(AddressFieldName));                    datas.add(data);                } catch (IOException e) {                    log.error(e.toString());                }            }        }                return datas;    }

搜尋矩形範圍內的資料:

    public List<PoiData> searchPoiInRectangle(double minLng, double minLat, double maxLng, double maxLat) {        List<PoiData> results= new ArrayList<>();        Point lowerLeftPoint = ctx.makePoint(minLng, minLat);        Point upperRightPoint = ctx.makePoint(maxLng, maxLat);        Shape rect = ctx.makeRectangle(lowerLeftPoint, upperRightPoint);        SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, rect);        Query query = strategy.makeQuery(args);        TopDocs docs = null;        try {            docs = indexSearcher.search(query, 10);        } catch (IOException e) {            log.error(e.toString());        }                if(docs!=null){            ScoreDoc[] scoreDocs = docs.scoreDocs;            printDocs(scoreDocs);            results = getPoiDatasFromDoc(scoreDocs);        }                return results;    } 

搜尋某個範圍內並根據地址關鍵字資訊來檢索POI:

public List<PoiData>searchPoByRangeAndAddress(doublelng, doublelat, double range, String address){        List<PoiData> results= newArrayList<>();        SpatialArgsargs = newSpatialArgs(SpatialOperation.Intersects,        ctx.makeCircle(lng, lat, DistanceUtils.dist2Degrees(range, DistanceUtils.EARTH_MEAN_RADIUS_KM)));        Query geoQuery = strategy.makeQuery(args);                QueryBuilder builder = newQueryBuilder(analyzer);        Query addQuery = builder.createPhraseQuery(AddressFieldName, address);                BooleanQuery.BuilderboolBuilder = newBooleanQuery.Builder();        boolBuilder.add(addQuery, Occur.SHOULD);        boolBuilder.add(geoQuery,Occur.MUST);                Query query = boolBuilder.build();                TopDocs docs = null;        try {            docs = indexSearcher.search(query, 10);        } catch (IOException e) {            log.error(e.toString());        }                if(docs!=null){            ScoreDoc[] scoreDocs = docs.scoreDocs;            printDocs(scoreDocs);            results = getPoiDatasFromDoc(scoreDocs);        }                return results;    }

4、關於分詞

POI的地址屬性和描述屬性都需要做分詞才能更好的進行檢索和搜尋。

簡單對比了幾種分詞效果:

原文:

這是一個lucene中文分詞的例子,你可以直接運行它!Chinese Analyer can analysis english text too.中國農業銀行(農行)和建設銀行(建行),江蘇南京江寧上元大街12號。東南大學是一所985高校。

分詞結果:

smartcn SmartChineseAnalyzer這\是\一個\lucen\中文\分\詞\的\例子\你\可以\直接\運行\它\chines\analy\can\analysi\english\text\too\中國\農業\銀行\農行\和\建設\銀行\建行\江蘇\南京\江\寧\上\元\大街\12\號\東南\大學\是\一\所\985\高校MMSegAnalyzer ComplexAnalyzer這是\一個\lucene\中文\分詞\的\例子\你\可以\直接\運行\它\chinese\analyer\can\analysis\english\text\too\中國農業\銀行\農行\和\建設銀行\建\行\江蘇南京\江\寧\上\元\大街\12\號\東南大學\是一\所\985\高校IKAnalyzer這是\一個\lucene\中文\分詞\的\例子\你\可以\直接\運行\它\chinese\analyer\can\analysis\english\text\too.\中國農業銀行\農行\和\建設銀行\建行\江蘇\南京\江寧\上元\大街\12號\東南大學\是\一所\985\高校\

分詞效果對比:

1)Smartcn不能正確的分出有些英文單詞,有些中文單詞也被分成單個字。

2)MMSegAnalyzer能正確的分出英文和中文,但對於類似“江寧”這樣的地名和“建行”等資訊不是很準確。MMSegAnalyzer支援自訂詞庫,詞庫可以大大提高分詞的準確性。

3)IKAnalyzer能正確的分出英文和中文,中文分詞比較不錯,但也有些小問題,比如單詞too和最後的點號分在了一起。IKAnalyzer也支援自訂詞庫,但是要擴充一些源碼。

 

總結:使用Lucene強大的資料索引和檢索能力可以為一些帶有經緯度和需要分詞檢索的資料提供搜尋功能。

 

使用Lucene索引和檢索POI資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.