2013-06-28 56 views
4

通过搜索的字符串i(Experience:[1 TO 5])它像搜索15所有号码,25,21,51,等我需要的数字1和5之间进行搜索,为什么Lucene数字范围搜索不起作用?

using Lucene.Net.Store; 

var results = new List<SearchResults>(); 

// Specify the location where the index files are stored 
string indexFileLocation = @"G:\Lucene.Net\Data\Document"; 
var dir = Lucene.Net.Store.FSDirectory.GetDirectory(indexFileLocation); 
var reader = IndexReader.Open(dir); 
var searcher = new IndexSearcher(reader); 
var analyzer = new StandardAnalyzer(); 
var queryParser = new QueryParser("Prof_ID", analyzer); 

// <default field> is the field that QueryParser will search if you don't 

string special = ""; 
if (!txtkeyword.Text.Equals("")) 
{ 
    special = special + "(Experience:[1 TO 5])"; 
} 

var hits = searcher.Search(queryParser.Parse(special)); 

// Getting result to the list 
for (int i = 0; i < hits.Length(); i++) 
{ 
    SearchResults result = new SearchResults(); 

    result.Skillsummarry = hits.Doc(i).GetField("JS_Skill_Summary").StringValue(); 
    result.Experience = hits.Doc(i).GetField("Experience").StringValue(); 
    result.Profile_Id = hits.Doc(i).GetField("Prof_ID").StringValue(); 

    results.Add(result); 
} 

GridView1.DataSource = results; 
GridView1.DataBind(); 
+0

对不起,你的问题不是很清楚。请编辑你的问题,并试图弄清楚什么可行,你想达到什么目的。 – wonko79

+0

您需要显示如何为数据编制索引,以及数据的外观。此代码只是显示您如何查询现有索引。 –

回答

6

做一个范围 - 类型的查询,你应该做的,

var query = new TermRangeQuery(
    "Experience", 
    "1", 
    "5", 
    includeLower: true, 
    includeUpper: true); 

但是,如果存储在您的数字作为string因为它字符串比较,而不是一个数字比较可能返回错误的范围那是;因此"5" > "15"true,而不是其他方式。

要你做一个数字范围类型查询,

var query = 
    NumericRangeQuery.NewDoubleRange(
     "Experience", 
     1, 
     5, 
     includeLower: true, 
     includeUpper: true); 

但是,你需要确保当您索引您的文档,您存储Experience字段作为数字字段,而不是一个标准,

var field = 
    new NumericField("Experience", Field.Store.YES, true) 
     .SetDoubleValue(15, 25, 21, 51, etc.); 

将其添加到您的Lucene文档之前。

+1

谢谢@ rae1n.You指导我正确的方式来实现我的需求。 –