我一直致力于尝试让SOLR的SpatialSearch适配器来搜索二维和三维对象。我尝试使用solr.LatLonType但我无法成功地将任何数据添加到使用该类型的字段。然后我想可能有关于solr.SpatialRecursivePrefixTreeFieldType的更多信息,但我发现使用该类型的帮助更少。 这是我到目前为止所得到的:
// SOLR schema.xml
<field name="sheet" type="location" indexed="true" stored="true"/>
<field name="sheet_0_edge" type="double" indexed="true" stored="true" />
<field name="sheet_1_edge" type="double" indexed="true" stored="true" />
// SolrNet Document
using System;
using System.Drawing;
using SolrNet.Attributes;
public class SheetIndexDocument
{
public class SheetDocument
{
//Return identifiers
[SolrField("id")]
public string Id { get; set; }
[SolrField("name")]
public string Name { get; set; }
[SolrField("sheet_0_edge")]
public decimal? Width { get; set; }
[SolrField("sheet_1_edge")]
public decimal? Length { get; set; }
}
}
// SolrNet WebService Index
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Configuration;
using ApplicationModel.Context;
public class SheetIndex : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string sCommand = context.Request.Params["Command"];
if (sCommand == "Index")
{
context.Response.ContentType = "text/plain";
this.IndexSheets(context);
}
}
public bool IsReusable
{
get
{
return false;
}
}
public void IndexSheets(HttpContext context)
{
// Connects the SOLR schema to our SOLR Doc in the App_Code section.
SolrNet.ISolrOperations<SheetIndexDocument.SheetDocument> solr = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<SolrNet.ISolrOperations<SheetIndexDocument.SheetDocument>>();
SheetIndexDocument.SheetDocument oDoc = new SheetIndexDocument.SheetDocument();
string sQuery = @"
SELECT Id, Name, Width, Length
FROM [Sheet]
";
try
{
using (SqlConnection cn = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
cn.Open();
SqlTransaction tr = cn.BeginTransaction(IsolationLevel.ReadUncommitted);
SqlCommand oCmd = new SqlCommand(sQuery, cn);
oCmd.CommandTimeout = 240;
oCmd.Transaction = tr;
DataTable dt = new DataTable();
using (SqlDataReader reader = oCmd.ExecuteReader())
{
dt.Load(reader);
}
oCmd.Transaction.Dispose();
oCmd.Connection.Close();
context.Response.Write("Number of Sheets to index: " + dt.Rows.Count + "\r");
context.Response.Write("Indexing... \r\n");
foreach (DataRow row in dt.Rows)
{
//Sheet identifiers
int id = ToFieldValueOrNull<int>(row, "Id");
oDoc.Id = id.ToString();
oDoc.Name = ToFieldValueOrNull<string>(row, "Caption");
context.Response.Write("Added Item: " + oDoc.Name);
decimal? dWidth = 0.0m;
if (ToFieldValueOrNull<decimal?>(row, "Width").HasValue) dWidth = ToFieldValueOrNull<decimal?>(row, "Width");
context.Response.Write(" Loc(" + oDoc.Width);
decimal? dLength = 0.0m;
if (ToFieldValueOrNull<decimal?>(row, "Length").HasValue) dLength = ToFieldValueOrNull<decimal?>(row, "Length");
context.Response.Write("," + oDoc.Length + ")\r");
oDoc.Width = dWidth;
oDoc.Longitude = dLength;
solr.Add(oDoc);
context.Response.Flush();
}
context.Response.Write("Done\r\n");
context.Response.Flush();
}
}
finally
{
solr.Commit();
context.Response.Write("Committed");
context.Response.Flush();
}
}
public T ToFieldValueOrNull<T>(DataRow row, string columnName)
{
if (row[columnName] == DBNull.Value) return default(T);
T val = row.Field<T>(columnName);
return val;
}
public List<string> ToFieldValuesOrNull(DataRow row, string columnName)
{
object val = row[columnName];
if (row[columnName] == null) return null;
return new List<string>(
val.ToString().Split(new string[] { "|", ";", "," }, StringSplitOptions.RemoveEmptyEntries)
);
}
}
非常感谢有关如何处理LatLonType或SpatialRecursivePrefixTreeFieldType的任何指示!
修改:使用此FieldType:
<fieldType name="location" class="solr.LatLonType" subFieldSuffix="_edge"/>