加入多个字段的查询 - 在MVC C中进行lucence.net搜索#

时间:2013-12-11 13:13:03

标签: c# asp.net-mvc-4 lucene lucene.net

如何搜索搜索词,城市或州等两个字段。我必须像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);
        }

_search方法:

 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中遇到异常,如下所示。 enter image description here

请帮帮我。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用edial.in搜索脚本。我觉得很高级