如何使用Lucene在许多领域内进行搜索?
我可以根据字段或所有字段进行搜索。但是在几个领域内不可能。
我的代码:
public class Users
{
public string Name { get; set; }
public string Family { get; set; }
public string Code { get; set; }
}
搜索字段:
List<Users> searchResults = GoLucene<Users>.Search(input, "Family").ToList();
搜索所有字段:
List<Users> searchResults = GoLucene<Users>.Search(input).ToList();
public static IEnumerable<T> Search(string input, string fieldName = "")
{
if (string.IsNullOrEmpty(input)) return new List<T>();
return _search(input, fieldName);
}
// main search method
private static IEnumerable<T> _search(string searchQuery, string searchField = "")
{
// validation
if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", ""))) return new List<T>();
// set up lucene searcher
using (var searcher = new IndexSearcher(Directory, true))
{
const int hitsLimit = 15000;
var analyzer = new StandardAnalyzer(Version.LUCENE_30);
// search by single field
if (!string.IsNullOrEmpty(searchField))
{
var parser = new QueryParser(Version.LUCENE_30, searchField, analyzer);
var query = ParseQuery(searchQuery, parser);
var hits = searcher.Search(query, hitsLimit).ScoreDocs;
var results = _mapLuceneToDataList(hits, searcher);
analyzer.Close();
searcher.Dispose();
return results;
}
// search by multiple fields (ordered by RELEVANCE)
else
{
var strings = ReadProperties().Select(a => a.Name).ToArray();
var parser = new MultiFieldQueryParser(Version.LUCENE_30, strings, analyzer);
var query = ParseQuery(searchQuery, parser);
var hits = searcher.Search(query, null, hitsLimit, Sort.INDEXORDER).ScoreDocs;
var results = _mapLuceneToDataList(hits, searcher);
analyzer.Close();
searcher.Dispose();
return results;
}
}
}
如何仅在以下字段中进行搜索? 家庭,编码
我尝试了以下但没有奏效:
List<Users> searchResults = GoLucene<Users>.Search(input, "Family,Code").ToList();
或
List<Users> searchResults = GoLucene<Users>.Search(input, "Family||Code").ToList();
或
List<Users> searchResults = GoLucene<Users>.Search(input, "Family Code").ToList();
更新 我尝试过以下但没有奏效:
var nameParser = new QueryParser(Version.LUCENE_30, "Family", analyzer);
var hobbiesParser = new QueryParser(Version.LUCENE_30, "Code", analyzer);
var q = new BooleanQuery
{
{nameParser.Parse(searchQuery), Occur.SHOULD},
{hobbiesParser.Parse(searchQuery), Occur.SHOULD}
};
var hits = searcher.Search(q, null, hitsLimit, Sort.INDEXORDER).ScoreDocs;
var results = _mapLuceneToDataList(hits, searcher);
analyzer.Close();
searcher.Dispose();
return results;
更新2: 我尝试过以下但没有奏效:
var queryParser = new MultiFieldQueryParser(
Version.LUCENE_30,
new[] {"Family", "Code"},analyzer);
Query query = queryParser.Parse(searchQuery);
var hits = searcher.Search(query, hitsLimit).ScoreDocs;
var results = _mapLuceneToDataList(hits, searcher);
analyzer.Close();
searcher.Dispose();
return results;
答案 0 :(得分:0)
我找到了:
var searchField="Family,Code";
var parser = new MultiFieldQueryParser(Version.LUCENE_30, searchField.Split(','), analyzer);
var query = ParseQuery(searchQuery, parser);
var hits = searcher.Search(query, null, hitsLimit, Sort.INDEXORDER).ScoreDocs;
var results = _mapLuceneToDataList(hits, searcher);
analyzer.Close();
searcher.Dispose();
return results;