下面的是搜索的代码:
QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "geoname", new ChineseAnalyzer());
//parser.SetPhraseSlop(10);
Query query = parser.Parse(queryStr);
// QueryParser parserRid = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "rid", new ChineseAnalyzer());
// parserRid.SetAllowLeadingWildcard(true);
//rid = "南宁";
// WildcardQuery queryRid = new Lucene.Net.Search.WildcardQuery(new Lucene.Net.Index.Term("geoname", rid + "*"));//parserRid.Parse(rid + "*"); //
PrefixQuery queryx = new PrefixQuery(new Term("rid", rid));
BooleanQuery booleanQuery = new Lucene.Net.Search.BooleanQuery();
//BooleanQuery.SetMaxClauseCount(10000);
booleanQuery.Add(query, BooleanClause.Occur.MUST);
booleanQuery.Add(queryx, BooleanClause.Occur.MUST);
ScoreDoc[] hits = Searcher.Search(booleanQuery, null, 150).scoreDocs;
//1000021
//10000213io3i2oi
//1000021*
if (hits.Length >= 1)
下面是创建索引的代码:
IndexWriter writer = new IndexWriter(FSDirectory.Open(tempDir), new ChineseAnalyzer(), enableCreate, IndexWriter.MaxFieldLength.UNLIMITED);
if (worker != null && worker.CancellationPending)
{
return true;
}
writer.SetUseCompoundFile(true);
Document doc;
foreach (DataRow dr in dt.Rows)
{
if (worker != null && worker.CancellationPending)
{
return true;
}
doc = new Document();
string geoname = dr["geoname"].ToString();
string lnt = dr["lnt"].ToString();
string lat = dr["lat"].ToString();
string rid = dr["rid"].ToString();
doc.Add(new Field("geoname", geoname, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("lnt", lnt, Field.Store.YES, Field.Index.NO));
doc.Add(new Field("lat", lat, Field.Store.YES, Field.Index.NO));
doc.Add(new Field("rid", rid, Field.Store.YES, Field.Index.ANALYZED));
writer.AddDocument(doc);
}
writer.Optimize();
if (worker != null && worker.CancellationPending)
{
return true;
}
writer.Close();
找不到原因啊, 就是搜索不出数据
需要是说明:
查询的数据是完全正确的,并且包含 通配符 的数据 rid:'2121212*'
rid的ANALYZED改为NOT_ANALYZED.没准可以