地理空间 API
分布式 Lucene 支持使用地理空间坐标对文档进行索引,然后查询这些索引文档以进行基于位置的搜索。 为此,分布式 Lucene 使用 Spatial4n
图书馆。
先决条件
- 了解与所有人员合作所需的标准先决条件 NCache 客户端功能请参阅给定页面 客户端 API 先决条件.
- 确保您已经通过 NCache 管理中心 or 命令行界面.
- 确保您的应用程序没有使用任何本机 Lucene DLL/Reference。
- 有关 API 详细信息,请参阅: 空间文档, 空间策略, Geohash前缀树, 递归前缀树策略, NCache目录, 索引写入器配置, Lucene版本, 标准分析仪, 索引写入器, 空间上下文, 从 Wkt 读取形状, 索引搜索器, 索引阅读器, 目录阅读器, 类排序字段, 排序字段类型, 空间参数, 空间操作, 距离实用程序, 画圈, 距离2度, 制作过滤器, 顶级文档, 匹配所有文档查询, 部署.
空间文件
空间文档是使用 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
类封装了一种根据形状对文档进行索引和搜索的方法。 Distributed Lucene 支持 Lucene 支持的所有空间策略。 空间 API 的工作在分布式 Lucene 和 Lucene 中是相同的,除了一个概念 SpatialDocument
仅存在于分布式 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上
参见
.NET: 分布式Lucene.Net.Spatial 命名空间。