地理空間API
Distributed Lucene は、地理空間座標を使用したドキュメントのインデックス付けと、後で位置ベースの検索のためにこれらのインデックス付けされたドキュメントのクエリをサポートします。 この目的のために、分散 Lucene は Spatial4n
としょうかん。
前提条件
- すべてを使用するために必要な標準的な前提条件について学習するには NCache クライアント側の機能については、次のページを参照してください。 クライアント側 API の前提条件.
- Luceneキャッシュを作成して開始したことを確認してください。 NCache 管理センター or コマンドラインインターフェイス.
- アプリケーションがネイティブのLuceneDLL/リファレンスを使用していないことを確認してください。
- APIの詳細については、以下を参照してください。 空間ドキュメント, 空間戦略, GeohashPrefixTree, RecursivePrefixTree戦略, NCacheディレクトリ, IndexWriterConfig, Luceneバージョン, スタンダードアナライザー, インデックスライター, 空間コンテキスト, ReadShapeFromWkt, インデックスサーチャー, 索引リーダー, ディレクトリリーダー, クラスSortField, 並べ替えフィールドの種類, 空間引数, 空間操作, 距離利用率, メイクサークル, 距離 2 度, メイクフィルター, トップドキュメント, MatchAllDocsQuery, 処分.
空間ドキュメント
空間ドキュメントは、 SpatialDocument
クラス。 これらのドキュメントは、後でインデックス作成の目的で使用されます。 このクラスは分散 Lucene に固有です。
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の例
SpatialStrategy
クラスは、形状に基づいて文書を索引付けおよび検索するアプローチをカプセル化します。 分散型 Lucene は、Lucene でサポートされているすべての空間戦略をサポートしています。 空間 API の動作は、分散型 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();
Note
処分しないのは、 NCacheDirectory
, IndexReader
, IndexWriter
, IndexSearcher
, DirectoryTaxonomyReader
サーバー側で余分なメモリ消費が発生します。
その他のリソース
NCache また、上の分散 Lucene で Geo-Spatial API のサンプル アプリケーションを提供します。 GitHubの
も参照してください
。ネット: DistributedLucene.Net.Spatial 名前空間