지리 공간 API
분산 Lucene은 지리 공간 좌표를 사용하여 문서를 인덱싱하고 나중에 위치 기반 검색을 위해 이러한 인덱싱된 문서를 쿼리하는 것을 지원합니다. 이를 위해 분산 루씬은 다음을 사용합니다. Spatial4n
도서관.
사전 조건
- 모든 작업에 필요한 표준 전제 조건에 대해 알아보려면 NCache 클라이언트측 기능은 다음 페이지를 참조하세요. 클라이언트 측 API 전제 조건.
- 다음을 통해 Lucene 캐시를 만들고 시작했는지 확인하십시오. NCache 관리 센터 or 명령 행 인터페이스.
- 애플리케이션이 기본 Lucene DLL/참조를 사용하고 있지 않은지 확인하십시오.
- API 세부 정보는 다음을 참조하세요. 공간문서, 공간 전략, GeohashPrefix트리, RecursivePrefixTree전략, NCache명부 담당, 색인 작성기 구성, Lucene 버전, 표준 분석기, 인덱스 라이터, 공간 컨텍스트, ReadShapeFromWkt, 인덱스 검색자, 인덱스 리더, 디렉토리 리더, 클래스 SortField, 정렬필드유형, SpatialArgs, 공간 조작, 거리 유틸리티, 메이크서클, 거리2도, MakeFilter, 탑닥스, MatchAllDocs쿼리, 이.
공간 문서
공간 문서는 SpatialDocument
수업. 이러한 문서는 나중에 색인화 목적으로 사용됩니다. 이 클래스는 분산 Lucene에만 적용됩니다.
XNUMXD덴탈의 SpatialDocument
의 모양으로 구성되어 있습니다. IShape
함께하는 수업 Document
. 그러면 이러한 모양은 다음을 통해 서버 측에서 색인 생성 가능한 필드로 변환됩니다. Strategy
. 인덱싱 가능한 필드가 Document
를 통해 IndexWriter
.
다음 예제에서는 반환 유형이 SpatialDocument
클래스에서 이 메서드는 다음 예제에서 사용됩니다.
public static SpatialDocument CreateSpatialDocument(int id, SpatialStrategy strategy, params IShape[] shapes)
{
Document doc = new Document();
doc.Add(new Int32Field("id", id, Field.Store.YES));
foreach (IShape shape in shapes)
{
IPoint pt = (IPoint)shape;
doc.Add(new StoredField(strategy.FieldName,
pt.X.ToString(CultureInfo.InvariantCulture) + " " +
pt.Y.ToString(CultureInfo.InvariantCulture)));
}
return new SpatialDocument()
{
Document = doc,
Shapes = shapes
};
}
공간 API 예제
XNUMXD덴탈의 SpatialStrategy
클래스는 모양을 기반으로 문서를 인덱싱하고 검색하는 접근 방식을 캡슐화합니다. 분산 Lucene은 Lucene에서 지원하는 모든 공간 전략을 지원합니다. 공간 API의 작업은 Distributed Lucene과 Lucene 모두에서 동일합니다. SpatialDocument
Distributed Lucene에만 있는 클래스입니다.
다음 예에서 공간 문서는 다음을 사용하여 정의됩니다. CreateSpatialDocument
이전 예제에서 생성한 다음 추가된 메서드 IndexWriter
와 더불어 SpatialStrategy
한정된. 문서는 나중에 다음을 사용하여 검색됩니다. IndexSearcher
, 그 결과는 나중에 확인됩니다.
// Precondition: Lucene cache is already connected
string cache = "luceneCache";
string index = "normalIndex";
IndexReader indexReader = null;
IndexSearcher indexSearcher = null;
// Initialize prefix tree with depth level
var maxLevels = 11;
var grid = new GeohashPrefixTree(SpatialContext.GEO,
maxLevels);
// Define strategy
var strategy = new RecursivePrefixTreeStrategy(grid,
"myGeoField");
// Open NCache directory on index
var directory = NCacheDirectory.Open(cache, index);
// Configure and initalize index writer
var indexWriterConfig = new IndexWriterConfig
(LuceneVersion.LUCENE_48, new StandardAnalyzer
(LuceneVersion.LUCENE_48));
IndexWriter writer = new IndexWriter(directory,
indexWriterConfig);
// Add spatial documents to document with index writer
writer.AddDocument(CreateSpatialDocument(2, strategy,
SpatialContext.GEO.MakePoint(-80.93, 33.77)), strategy);
writer.AddDocument(CreateSpatialDocument(4, strategy,
SpatialContext.GEO.ReadShapeFromWkt("POINT(60.9289094
-50.7693246)")), strategy);
writer.AddDocument(CreateSpatialDocument(20,
strategy, SpatialContext.GEO.MakePoint(0.1, 0.1),
SpatialContext.GEO.MakePoint(0, 0)), strategy);
// Open index reader on directory
indexReader = DirectoryReader.Open(directory);
// Open index searcher on index reader
indexSearcher = new IndexSearcher(indexReader);
// Define sorting strategy
Sort idSort = new Sort(new SortField("id",
SortFieldType.INT32));
// Define spatial arguments
SpatialArgs args = new SpatialArgs(SpatialOperation.
Intersects, SpatialContext.GEO.MakeCircle(-80.0, 33.
0, DistanceUtils.Dist2Degrees(200, DistanceUtils.
EARTH_MEAN_RADIUS_KM)));
// Define the filter with spatial arguments
Filter filter = strategy.MakeFilter(args);
// Search top docs
TopDocs docs = indexSearcher.Search(new
MatchAllDocsQuery(), filter, 10, idSort);
if (indexSearcher != null) indexSearcher.Dispose();
if (indexReader != null) indexReader.Dispose();
주의 사항
처분하지 않음 NCacheDirectory
, IndexReader
, IndexWriter
, IndexSearcher
및 DirectoryTaxonomyReader
서버 측에서 추가 메모리 소비가 발생합니다.
추가 자료
NCache 또한 의 Distributed Lucene에서 Geo-Spatial API용 샘플 애플리케이션을 제공합니다. GitHub의
도 참조
.그물: DistributedLucene.Net.Spatial 네임 스페이스.