如何搜索搜索词,城市或州等两个字段。我必须像justdial那样进行搜索。我只能通过搜索字词而非术语和地点来搜索搜索结果。我尝试了一些代码如下,但我得到了例外。
public static IEnumerable<SearchModel> Search(string input, string state, string city)
{
var terms = input.Trim().Replace("-", " ").Split(' ')
.Where(x => !string.IsNullOrEmpty(x)).Select(x => x.Trim() + "*");
var stat = state.Trim().Replace("-", " ").Split(' ')
.Where(x => !string.IsNullOrEmpty(x)).Select(x => x.Trim());
input = string.Join(" ", terms,stat); //Joiningstate here
return _search(input);
}
private static IEnumerable<SearchModel> _search(string searchQuery)
{
// validation
if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", ""))) return new List<SearchModel>();
// set up lucene searcher
using (var searcher = new IndexSearcher(_directory, false))
{
var hits_limit = 1000;
var analyzer = new StandardAnalyzer(Version.LUCENE_30);
var parser = new MultiFieldQueryParser
(Version.LUCENE_30, new[] { "Name","StateName", "CityName" }, analyzer);
var query = parseQuery(searchQuery, parser);
var hits = searcher.Search(query, null, hits_limit, Sort.INDEXORDER).ScoreDocs;
var results = _mapLuceneToDataList(hits, searcher);
analyzer.Close();
searcher.Dispose();
return results;
}
}
private static Query parseQuery(string searchQuery, QueryParser parser)
{
Query query;
try
{
query = parser.Parse(searchQuery.Trim());
}
catch (ParseException)
{
query = parser.Parse(QueryParser.Escape(searchQuery.Trim()));
}
return query;
}
但是我在Parse Query中遇到异常,如下所示。
请帮帮我。