/// <summary> /// Search the index entries given a term /// </summary> /// <param name="term">Search Term</param> public IndexEntry[] Search(string term) { IndexSearcher searcher = new IndexSearcher(luceneIndexDirectory); MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new string[] { "Name", "Mobile" }, analyzerWrapper); Query query = parser.Parse(term); var hits = searcher.Search(query, MAX_HITS).ScoreDocs; var results = hits.Select(hit => MapDocument(hit, searcher.Doc(hit.Doc))).ToArray(); return(results); }
private Query MakeSearchQuery(string key, string[] fields, Analyzer analyzer) { if (fields == null || !fields.Any()) { fields = new string[] { "Title", "Body" }; } BooleanQuery query = new BooleanQuery(); QueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, fields, analyzer); query.Add(parser.Parse(key), Occur.MUST); return(query); }
private static IEnumerable <PostModel> SearchByMultipleFields(string searchQuery, StandardAnalyzer analyzer, int hits_limit, IndexSearcher searcher) { var parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new[] { "Id", "Title", "Discription" }, analyzer); var query = ParseQuery(searchQuery, parser); var hits = searcher.Search (query, null, hits_limit, Sort.RELEVANCE).ScoreDocs; var results = MapLuceneToDataList(hits, searcher); analyzer.Close(); searcher.Dispose(); return(results); }
public IActionResult Get(string searchTerm) { if (string.IsNullOrEmpty(searchTerm)) { return(new JsonResult(null)); } if (searchTerm.StartsWith("*") || searchTerm.StartsWith("?")) { return(new JsonResult(null)); } CloudStorageAccount cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount; CloudStorageAccount.TryParse(_options.ConnectionString, out cloudStorageAccount); var azureDirectory = new AzureDirectory(cloudStorageAccount, "TestCatalog"); var searcher = new IndexSearcher(azureDirectory); var analyzer = new NuGet.Indexing.IdentifierAnalyzer(); var queryParser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new[] { "Type", "ReturnType" }, analyzer); Query query = queryParser.Parse(searchTerm); //execute the query var hits = searcher.Search(query, 50); var packages = new List <SearchResult>(); foreach (var hit in hits.ScoreDocs) { var doc = searcher.Doc(hit.Doc); var result = new SearchResult { FullTypeName = doc.GetField("Type").StringValue, PackageName = doc.GetField("Package").StringValue }; if (doc.GetField("Signature") != null) { result.Signature = doc.GetField("Signature").StringValue; } if (doc.GetField("ReturnType") != null) { result.ReturnType = doc.GetField("ReturnType").StringValue; } packages.Add(result); } return(new JsonResult(packages)); }
// See UnitIndexer.cs for details on how fields were originally indexed. public IList <LuceneSearchResult> Search(string term, int numResults) { using (SearcherManager manager = new SearcherManager(UnitIndexWriterSingleton.Instance)) { this.searcher = manager.Acquire().Searcher; QueryParser parser; if (!string.IsNullOrEmpty(term) && term.Trim().StartsWith("Id")) { // Single and ranged numeric value search on Id field parser = new NumericQueryParser(Lucene.Net.Util.Version.LUCENE_30, "Id", new PersonAnalyzer()); } else { // General search across text fields parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new string[] { "Name", "ParentNameChange", "ChildNameChange", "BackgroundInformation", "Organization" }, new PersonAnalyzer()); // We maintain OR as default for maximum results parser.DefaultOperator = QueryParser.Operator.OR; if (!string.IsNullOrEmpty(term)) { if (!term.Contains(':')) { // Edit user's search string and add wildcards. term = string.Join(" ", term.Split(new string[] { " " }, System.StringSplitOptions.RemoveEmptyEntries) .Select(x => "*" + x + "*") .ToArray() ); } } } parser.AllowLeadingWildcard = true; try { Query query = parser.Parse(term); log.Debug("Search query: " + query.ToString()); this.topDocs = this.searcher.Search(query, numResults); return(TransformTopDocs()); } catch (ParseException e) { log.Error("Encountered problem parsing the search term: " + term, e); return(new List <LuceneSearchResult>()); } } }
public LuceneSearchEngine(IIndexDefinition <TEntity> indexDefinition) { IndexDefinition = indexDefinition; if (IndexDefinition == null) { IndexDefinition = new EntityIndexDefinition <TEntity>(); } MapDirectory = new MMapDirectory(IndexDefinition.IndexDirectory); Analyzer = new StandardAnalyzer(Core.LuceneVersion); Parser = new MultiFieldQueryParser(Core.LuceneVersion, IndexDefinition.GetSearchableFields().ToArray(), Analyzer); _writerConfiguration = new IndexWriterConfig(Core.LuceneVersion, Analyzer); }
public OfferQuery WithKeywords(string keywords) { if (!string.IsNullOrEmpty(keywords)) { string[] fields = { "Name", "Description", "Tags" }; var parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, fields, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)); Query multiQuery = parser.Parse(keywords); this.AddQuery(multiQuery); } return(this); }
//Build film queryable based on search string todo needs additions to also search cast and genres private IQueryable <Film> GetFilms() { var query = txtb_Search.Text; if (string.IsNullOrWhiteSpace(query) || !char.IsLetterOrDigit(query[0])) { return(_nhSession.Query <Film>()); } var parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, new[] { "Title", "Synopsis", "Keywords", "DirectorIndexing", "CharIndexing", "GenreIndexing", "PersonIndexing" }, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)); var searchSession = NHibernate.Search.Search.CreateFullTextSession(_nhSession); return(searchSession.CreateFullTextQuery(parser.Parse(query + "*"), new[] { typeof(Film) }).List <Film>().AsQueryable()); }
public void Code() { Analyzer _keywordanalyzer = new KeywordAnalyzer(); Analyzer _simpleanalyzer = new Lucene.Net.Analysis.SimpleAnalyzer(); Analyzer _stopanalyzer = new Lucene.Net.Analysis.StopAnalyzer(Lucene.Net.Util.Version.LUCENE_30); Analyzer _whitespaceanalyzer = new Lucene.Net.Analysis.WhitespaceAnalyzer(); Analyzer _standardanalyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); var _perfieldanalyzer = new Lucene.Net.Analysis.PerFieldAnalyzerWrapper(_standardanalyzer); _perfieldanalyzer.AddAnalyzer("firstname", _keywordanalyzer); _perfieldanalyzer.AddAnalyzer("lastname", _keywordanalyzer); IndexWriter _writer = new IndexWriter(_directory, _perfieldanalyzer, IndexWriter.MaxFieldLength.UNLIMITED); IndexReader _reader = _writer.GetReader(); IndexSearcher _searcher = new IndexSearcher(_reader); //QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "title", _standardanalyzer); string[] fields = new[] { "text", "title", "author" }; var boosts = new Dictionary <string, float>(); boosts.Add("text", 2.0f); boosts.Add("title", 1.5f); QueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, fields, _standardanalyzer, boosts); Query query = parser.Parse("lucene is great"); TopDocs hits = _searcher.Search(query, 1000); IEnumerable <Document> docs = hits.ScoreDocs.Select(hit => _searcher.Doc(hit.Doc)); var books = docs.Select(doc => new Book() { Text = doc.Get("text"), Title = doc.Get("title"), Author = doc.Get("author"), Length = Int32.Parse(doc.Get("length")) }); _writer.Optimize(); _writer.Commit(); _writer.DeleteAll(); }
public ActionResult Search(string txtSearch, int id = 1) { int pageNum = 1; int currentPageNo = id; IndexSearcher search = new IndexSearcher(directory, true); BooleanQuery bQuery = new BooleanQuery(); //总的结果条数 List <Article> list = new List <Article>(); int recCount = 0; //处理搜索关键词 txtSearch = LuceneHelper.GetKeyWordsSplitBySpace(txtSearch); //多个字段查询 标题和内容title, content MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new string[] { "title", "Content" }, new PanGuAnalyzer()); Query query = parser.Parse(txtSearch); //Occur.Should 表示 Or , Occur.MUST 表示 and bQuery.Add(query, Occur.MUST); if (bQuery != null && bQuery.GetClauses().Length > 0) { //盛放查询结果的容器 TopScoreDocCollector collector = TopScoreDocCollector.Create(1000, true); //使用query这个查询条件进行搜索,搜索结果放入collector search.Search(bQuery, null, collector); recCount = collector.TotalHits; //从查询结果中取出第m条到第n条的数据 ScoreDoc[] docs = collector.TopDocs((currentPageNo - 1) * pageNum, pageNum).ScoreDocs; //遍历查询结果 for (int i = 0; i < docs.Length; i++) { //只有 Field.Store.YES的字段才能用Get查出来 Document doc = search.Doc(docs[i].Doc); list.Add(new Article() { Id = doc.Get("id"), Title = LuceneHelper.CreateHightLight(txtSearch, doc.Get("title")), //高亮显示 Content = LuceneHelper.CreateHightLight(txtSearch, doc.Get("Content")) //高亮显示 }); } } //分页 PagedList <Article> plist = new PagedList <Article>(list, currentPageNo, pageNum, recCount); plist.TotalItemCount = recCount; plist.CurrentPageIndex = currentPageNo; return(View("Index", plist)); }
public SearchResult Search(string queryString, string[] searchFields, string[] metaData, int resultOffset, int resultLength) { if(resultOffset< 0) { resultOffset = 0; } if(resultLength < 1) { resultLength = 1; } var parser = new MultiFieldQueryParser(Version.LUCENE_30, searchFields, _analyzer) {DefaultOperator = QueryParser.Operator.AND}; var query = ParseQuery(queryString, parser); return ExecuteQuery(metaData, resultOffset, resultLength, query); }
public BooleanQuery MakeQuery() { //Fill BooleanQeury with anonymous terms. term.Field=critList.Key term.Term=critList.Value foreach (var pair in _searchCriterion) { string searchString = pair.Value.ToString(); if (!String.IsNullOrEmpty(searchString)) { switch (pair.Key) { case "FindAll": string[] searchFields = { "CardKind", "CardName", "CardEdition" }; var multiFieldQueryParser = new MultiFieldQueryParser(Version.LUCENE_30, searchFields, _analyzer); _finalQuery.Add(ParseQuery(searchString, multiFieldQueryParser), searchString.Contains("+") ? Occur.MUST : Occur.SHOULD); break; case "CardAdoptionDateMin": //must be compile time constant! string upperDate = DateTools.DateToString((DateTime)_kvpMax.Value, DateTools.Resolution.DAY); var lowerRange = new TermRangeQuery("CardAdoptionDate", null, upperDate, true, true); //{CardAdoptionDate:[* TO 20140718]} _finalQuery.Add(new BooleanClause(lowerRange, Occur.MUST)); break; case "CardAdoptionDateMax": string lowerDate = DateTools.DateToString((DateTime)_kvpMin.Value, DateTools.Resolution.DAY); var upperRange = new TermRangeQuery("CardAdoptionDate", lowerDate, null, true, true); //{CardAdoptionDate:[20140718 TO *]} _finalQuery.Add(new BooleanClause(upperRange, Occur.MUST)); break; default: _finalQuery.Add(new TermQuery(new Term(pair.Key, (string)pair.Value)), Occur.MUST); break; } #region Source and links //http://stackoverflow.com/questions/17503119/how-to-index-search-the-datetime-searchField-in-lucene-net //http://stackoverflow.com/questions/3294772/storing-datetime-searchField-in-lucene-document #endregion } } return(_finalQuery); }
private static BooleanClause GetFilterByTerm(string term) { StandardAnalyzer analyser = new StandardAnalyzer(Version.LUCENE_30); MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_30, new[] { UniversalSearchFieldNames.PrimarySearchTerms, UniversalSearchFieldNames.SecondarySearchTerms, UniversalSearchFieldNames.DisplayName, UniversalSearchFieldNames.Id, UniversalSearchFieldNames.EntityType }, analyser); BooleanClause booleanClause = new BooleanClause(term.SafeGetSearchQuery(parser, analyser), Occur.MUST); return(booleanClause); }
public static Query CreateMultiFieldQuery(string[] sField, string sSearchCriteria, bool bEscapeCharecters = false) { Analyzer oAnalyzer; Query oLuceneQuery; MultiFieldQueryParser oMultiFieldQueryParser; string sSearchCriteriaEscaped; oAnalyzer = new StandardAnalyzer(LuceneVersion); oMultiFieldQueryParser = new MultiFieldQueryParser(LuceneVersion, sField, oAnalyzer); oMultiFieldQueryParser.DefaultOperator = QueryParser.Operator.OR; sSearchCriteriaEscaped = bEscapeCharecters ? QueryParser.Escape(sSearchCriteria) : sSearchCriteria; oLuceneQuery = oMultiFieldQueryParser.Parse(sSearchCriteriaEscaped); return(oLuceneQuery); }
/// <summary> /// Search the Lucene Index to find document that meet the search terms specified by the user /// </summary> protected void search() { DateTime startTime = DateTime.Now; string indexDirectory = Server.MapPath(LUCENSE_INDEX_DIR); IndexSearcher searcher = new IndexSearcher(indexDirectory); // by default we search name and description MultiFieldQueryParser parser = new MultiFieldQueryParser(new string[] { NAME_FIELD_NAME, DESCRIPTION_FIELD_NAME }, new StandardAnalyzer()); // parse the query, "text" is the default field to search Query query = parser.Parse(SearchTerms); SearchResults.Columns.Add(ID_COLUMN, typeof(string)); SearchResults.Columns.Add(NAME_COLUMN, typeof(string)); SearchResults.Columns.Add(URL_COLUMN, typeof(string)); SearchResults.Columns.Add(IS_CLOSED_COLUMN, typeof(string)); // Perform Lucense Search Hits hits = searcher.Search(query); totalHits = hits.Length(); // index where we'll start processing results startIndex = CalculateStartIndex(); // max number of documents we'll process int maxResultsThisPage = (totalHits < (MAX_ITEMS_PER_PAGE + startIndex)) ? totalHits : (MAX_ITEMS_PER_PAGE + startIndex); // Create a DataRow from the Lucene document and update the results for (int i = startIndex; i < maxResultsThisPage; i++) { Document doc = hits.Doc(i); DataRow row = SearchResults.NewRow(); row[ID_COLUMN] = doc.Get(ID_FIELD_NAME); row[NAME_COLUMN] = doc.Get(NAME_FIELD_NAME); row[URL_COLUMN] = doc.Get(URL_FIELD_NAME); if ("True".Equals(doc.Get(IS_CLOSED_FIELD_NAME))) { row[IS_CLOSED_COLUMN] = "closed"; } SearchResults.Rows.Add(row); } searcher.Close(); searchTime = DateTime.Now - startTime; firstItemOnPage = startIndex + 1; lastItemOnPage = ((startIndex + maxResultsThisPage) < totalHits) ? (startIndex + maxResultsThisPage) : totalHits; }
/// <summary> /// Searches the index. /// </summary> /// <param name="queryText"></param> /// <param name="keywordFilter">A Hashtable where the key is the fieldname of the keyword and /// the value the keyword itself.</param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> public SearchResultCollection Find(string queryText, Hashtable keywordFilter, int pageIndex, int pageSize) { long startTicks = DateTime.Now.Ticks; Query query = MultiFieldQueryParser.Parse(queryText, new string[] { "title", "contents" }, new StandardAnalyzer()); IndexSearcher searcher = new IndexSearcher(_indexDirectory); Hits hits; if (keywordFilter != null && keywordFilter.Count > 0) { QueryFilter qf = BuildQueryFilterFromKeywordFilter(keywordFilter); hits = searcher.Search(query, qf); } else { hits = searcher.Search(query); } int start = pageIndex * pageSize; int end = (pageIndex + 1) * pageSize; if (hits.Length() <= end) { end = hits.Length(); } SearchResultCollection results = new SearchResultCollection(); results.TotalCount = hits.Length(); results.PageIndex = pageIndex; for (int i = start; i < end; i++) { SearchResult result = new SearchResult(); result.Title = hits.Doc(i).Get("title"); result.Summary = hits.Doc(i).Get("summary"); result.Author = hits.Doc(i).Get("author"); result.ModuleType = hits.Doc(i).Get("moduletype"); result.Path = hits.Doc(i).Get("path"); result.Category = hits.Doc(i).Get("category"); result.DateCreated = DateTime.Parse((hits.Doc(i).Get("datecreated"))); result.Score = hits.Score(i); result.Boost = hits.Doc(i).GetBoost(); result.SectionId = Int32.Parse(hits.Doc(i).Get("sectionid")); results.Add(result); } searcher.Close(); results.ExecutionTime = DateTime.Now.Ticks - startTicks; return(results); }
public void Search(string keyword) { IndexReader reader = null; IndexSearcher searcher = null; try { reader = IndexReader.Open(FSDirectory.Open(new DirectoryInfo(indexDirectory)), true); searcher = new IndexSearcher(reader); //创建查询 PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(analyzer); wrapper.AddAnalyzer("FileName", analyzer); wrapper.AddAnalyzer("Author", analyzer); wrapper.AddAnalyzer("Content", analyzer); string[] fields = { "FileName", "Author", "Content" }; QueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, fields, wrapper); Query query = parser.Parse(keyword); TopScoreDocCollector collector = TopScoreDocCollector.Create(NumberHits, true); searcher.Search(query, collector); var hits = collector.TopDocs().ScoreDocs; int numTotalHits = collector.TotalHits; //以后就可以对获取到的collector数据进行操作 for (int i = 0; i < hits.Count(); i++) { var hit = hits[i]; Document doc = searcher.Doc(hit.Doc); Field fileNameField = doc.GetField("FileName"); Field authorField = doc.GetField("Author"); Field pathField = doc.GetField("Path"); } } finally { if (searcher != null) { searcher.Dispose(); } if (reader != null) { reader.Dispose(); } } }
public List <dynamic> Read(string name) { //name = name.ToUpper(); //var newpy = new StringBuilder(); //for (int i = 0; i < name.Length; i++) //{ // if (_cxDic.ContainsKey(name[i])) // { // newpy.Append(_cxDic[name[i]]); // } // else // { // newpy.Append(name[i]); // } //} //name = newpy.ToString(); var list = new List <dynamic>(); //QueryParser queryParser = new MultiFieldQueryParser(LuceneVersion.LUCENE_48, //new[] { "fname", "fpy" }, new ClassicAnalyzer(LuceneVersion.LUCENE_48)); QueryParser queryParser = new MultiFieldQueryParser(LuceneVersion.LUCENE_48, new[] { "fname", "fpy" }, new MyAnalyzer(LuceneVersion.LUCENE_48)); var query = queryParser.Parse(name); var searcher = new IndexSearcher(DirectoryReader.Open(ramDir)); try { var topDocs = searcher.Search(query, 100); foreach (var result in topDocs.ScoreDocs) { var doc = searcher.Doc(result.Doc); list.Add(new { ID = doc.GetField("fmefeeitemid")?.GetStringValue(), Name = doc.GetField("fname")?.GetStringValue(), Py = doc.GetField("fpy")?.GetStringValue(), result.Score, }); } } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { searcher = null; } return(list); }
/// <summary> /// 搜索指定类型集合的数据 /// </summary> /// <param name="dataTypes"></param> /// <param name="areaID"></param> /// <param name="keyword"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <param name="count"></param> /// <returns></returns> private List <BaseIndexModel> SearchData(IndexDataType[] dataTypes, int areaID, string keyword, int pageIndex, int pageSize, out int count) { count = 0; if (null == dataTypes || dataTypes.Length < 1) { return(null); } string indexPath = IndexConfiguration.GetAreaPath(areaID); List <string> fields = new List <string> { "name" }; //, "desc" BooleanQuery bquery = new BooleanQuery(); //分词器 Analyzer analyzer = new StandardAnalyzer(IndexConfiguration.LuceneMatchVersion); //关键词搜索条件 Query kwdQuery = MultiFieldQueryParser.Parse(IndexConfiguration.LuceneMatchVersion, keyword, fields.ToArray(), new Occur[] { Occur.SHOULD }, analyzer);//, Occur.SHOULD bquery.Add(kwdQuery, Occur.MUST); //区域条件 Query areaQuery = new QueryParser(IndexConfiguration.LuceneMatchVersion, "areaid", analyzer).Parse(areaID.ToString()); bquery.Add(areaQuery, Occur.MUST); //类型 BooleanQuery typeQuery = new BooleanQuery(); foreach (var t in dataTypes) { Query qt = new QueryParser(IndexConfiguration.LuceneMatchVersion, "datatype", analyzer).Parse(t.ToString("d")); typeQuery.Add(qt, Occur.SHOULD); } bquery.Add(typeQuery, Occur.MUST); Sort sort = new Sort(new SortField("updatetime", SortField.STRING, true)); count = 0; var list = SearchHelper.Search <BaseIndexModel>(indexPath, bquery, sort, pageIndex, pageSize, out count); return(list); }
private SearchResult SearchCore(string query, string[] fields, int topResultCount) { // Search var parser = new MultiFieldQueryParser(Config.LuceneVersion, fields, Analyzer); parser.AllowLeadingWildcard = true; parser.DefaultOperator = Operator.AND; parser.Locale = Config.Locale; parser.AnalyzeRangeTerms = true; var q = parser.Parse(query); var results = Searcher.Search(q, topResultCount); var hits = results.ScoreDocs; if (results.TotalHits == 0) { return(SearchResult.Empty); } // Format var items = new List <SearchResultItem>(); var scorer = new QueryScorer(q); var formatter = new SimpleHTMLFormatter("<mark>", "</mark>"); var highlighter = new Highlighter(formatter, scorer) { TextFragmenter = new SimpleFragmenter(Config.FragmentLength) }; // ReSharper disable once ForCanBeConvertedToForeach for (var i = 0; i < hits.Length; i++) { var doc = Searcher.Doc(hits[i].Doc); var url = doc.Get("url"); var title = doc.Get("title"); var content = doc.Get("content"); using (var stream = Analyzer.GetTokenStream(url, new StringReader(content))) { var preview = highlighter.GetBestFragments(stream, content, Config.ResultFragments, Config.FragmentSeparator); var item = new SearchResultItem(url, ToWbrWrapName(title), preview); items.Add(item); } } return(new SearchResult(results.TotalHits, items)); }
public static Query SafeGetSearchQuery(this string term, MultiFieldQueryParser q, Analyzer analyser) { Query query; try { query = q.Parse(term.MakeFuzzy()); } catch { var searchTerm = term.Sanitize(analyser); query = q.Parse(searchTerm); } return(query); }
/// <summary> /// 索引文档查询 /// </summary> /// <param name="luceneSql">查询盘古SQL</param> /// <returns>结果</returns> public List <string> IndexDocQuery(string luceneSql) { List <string> hotelIds = new List <string>(); if (!string.IsNullOrWhiteSpace(luceneSql)) { IndexSearcher searcher = new IndexSearcher(FSDirectory.Open(indexPath)); string[] fields = { "Name", "City", "Themes" }; MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, fields, analyzer); Query query = parser.Parse(luceneSql); TopDocs tds = searcher.Search(query, int.MaxValue); hotelIds.AddRange(tds.ScoreDocs.Select(sd => searcher.Doc(sd.Doc)).Select(doc => doc.Get("Id"))); } return(hotelIds); }
public QueryParser GetQueryParser() { QueryParser parser = new MultiFieldQueryParser( LuceneIndexDefaults.IndexVersion, new[] { Labels.Label.ToString(), Labels.AltLabel.ToString() }, Analyzer ); parser.MultiTermRewriteMethod = new MultiTermQuery.TopTermsScoringBooleanQueryRewrite(int.MaxValue); parser.AllowLeadingWildcard = true; return(parser); }
public IEnumerable <T> Search <T>(string terms, params string[] fields) { //QueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, fields, CreateAnalyzer()); //Query query = parser.Parse(terms); //var query = parser.Parse(string.Format("\"{0}\"", terms)); var query = MultiFieldQueryParser.Parse(Version.LUCENE_30, new[] { terms }, fields, CreateAnalyzer()); var results = _searcher.Search(query, int.MaxValue); foreach (var result in results.ScoreDocs.OrderByDescending(d => d.Score)) { yield return(CastDocument <T>(_searcher.Doc(result.Doc))); } }
public List <String> SearchAndDisplayResults(string querytext) { topicID += 1; List <String> resultList = new List <String>(); txtCont.Clear(); resultList.Add(querytext); querytext = querytext.ToLower(); Dictionary <String, float> boosts = new Dictionary <string, float>(); boosts["passage_text"] = 2; boosts["title"] = 10; parser = new MultiFieldQueryParser(AppLuceneVersion, new String[] { "passage_text", "title" }, analyzer, boosts); Query query = parser.Parse(querytext); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); TopDocs results = searcher.Search(query, 100000); stopwatch.Stop(); int hits = results.TotalHits; resultList.Add(hits.ToString()); resultList.Add(stopwatch.ElapsedMilliseconds.ToString()); int rank = 0; foreach (ScoreDoc scoreDoc in results.ScoreDocs) { rank++; Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc); string url = doc.Get("url"); string passage_text = doc.Get("passage_text"); string title = doc.Get("title"); string id = doc.Get("passage_id"); resultList.Add("\nRank " + rank + "\ntitle: " + title + "\nurl: " + url + "\npassage_text: " + passage_text + "\n"); string txt = topicID.ToString().PadLeft(3, '0') + " Q0 " + id + " " + rank + " " + scoreDoc.Score + " " + "n10057862_" + "n10296255_" + "n10056084_" + "n10153853_" + "HyperGloryTeam" + "\n"; txtCont.Add(txt); } return(resultList); }
public CardDescription[] Search(string queryString, SortField[] sortFields) { try { if (queryString != null) { queryString = queryString.Trim(); } Query query = null; if (queryString == null || queryString == "") { query = new MatchAllDocsQuery(); } else { QueryParser parser = new MultiFieldQueryParser(MyLucene.GetLuceneVersion(), new string[] { "name", "oldName", "shortName", "japName", "enName", "effect", "effectType", "cardType", "tribe", "element", "level", "atk", "def", "aliasList", "package", "infrequence" }, AnalyzerFactory.GetAnalyzer()); query = parser.Parse(queryString); } TopDocs docs = null; if (sortFields == null) { docs = searcher.Search(query, null, searcher.MaxDoc()); } else { docs = searcher.Search(query, null, searcher.MaxDoc(), new Sort(sortFields)); } ScoreDoc[] sdocs = docs.scoreDocs; int length = sdocs.Length; CardDescription[] cards = new CardDescription[length]; for (int i = 0; i < length; i++) { cards[i] = GetCardByIndex(sdocs[i].doc); } return(cards); } catch { return(new CardDescription[0]); } }
public static IEnumerable <BaseItem> Search(string searchQuery, int maxHits) { var results = new List <BaseItem>(); lock (lockOb) { try { if (searcher == null) { searcher = new IndexSearcher(directory, true); } BooleanQuery finalQuery = new BooleanQuery(); MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new string[] { "Name", "Overview" }, analyzer, bonusTerms); string[] terms = searchQuery.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); foreach (string term in terms) { finalQuery.Add(parser.Parse(term.Replace("~", "") + "~0.75"), Occur.SHOULD); } foreach (string term in terms) { finalQuery.Add(parser.Parse(term.Replace("*", "") + "*"), Occur.SHOULD); } logger.Debug("Querying Lucene with query: " + finalQuery.ToString()); long start = DateTime.Now.Ticks; var searchResult = searcher.Search(finalQuery, maxHits); foreach (var searchHit in searchResult.ScoreDocs) { Document hit = searcher.Doc(searchHit.Doc); results.Add(BaseItem.LibraryManager.GetItemById(Guid.Parse(hit.Get("Id")))); } long total = DateTime.Now.Ticks - start; float msTotal = (float)total / TimeSpan.TicksPerMillisecond; logger.Debug(searchResult.ScoreDocs.Length + " result" + (searchResult.ScoreDocs.Length == 1 ? "" : "s") + " in " + msTotal + " ms."); } catch (Exception e) { logger.ErrorException("Error while searching Lucene index", e); } } return(results); }
private static IEnumerable <News> SearchQuery(string searchQuery, string searchField = "") { if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", ""))) { return(new List <News>()); } using (var searcher = new IndexSearcher(Directory, false)) { var hitsLimit = 1000; //var analyzer = new StandardAnalyzer(Version.LUCENE_30); var analyzer = GetAnalyzer(); 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.Dispose(); return(results); } else { var parser = new MultiFieldQueryParser(Version.LUCENE_30, new[] { "Id", "Title", "Content" }, analyzer); var query = ParseQuery(searchQuery, parser); var hits = searcher.Search(query, null, hitsLimit, Sort.RELEVANCE).ScoreDocs; var results = MapLuceneToDataList(hits, searcher); //QueryParser queryParser = new QueryParser(Version.LUCENE_30, "Content", analyzer); //Query query = queryParser.Parse(searchQuery); //QueryParser titleQueryParser = new QueryParser(Version.LUCENE_30, "Title", analyzer); //Query titleQuery = titleQueryParser.Parse(searchQuery); //BooleanQuery bq = new BooleanQuery(); //bq.Add(query, Occur.SHOULD); //bq.Add(titleQuery, Occur.SHOULD); //var hits = searcher.Search(bq, null, hitsLimit, Sort.RELEVANCE); //var results = MapLuceneToDataList(hits.ScoreDocs, searcher); analyzer.Close(); return(results); } } }
// main search method private static IEnumerable <IndexableEntity> _search(string searchQuery, string searchField = "") { // validation if (string.IsNullOrWhiteSpace(searchQuery.Replace("*", string.Empty).Replace("?", string.Empty))) { return(new List <IndexableEntity>()); } // set up lucene searcher using (var searcher = new IndexSearcher(_directory, false)) { var hits_limit = 1000; var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); // search by single field if (!string.IsNullOrWhiteSpace(searchField)) { var sort = new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField(searchField, SortField.STRING) }); TopFieldCollector topField = TopFieldCollector.Create(sort, hits_limit, true, true, true, true); var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, searchField, analyzer); var query = parseQuery(searchQuery, parser); //var hits = searcher.Search(query, hits_limit).ScoreDocs; searcher.Search(query, topField); var hits = topField.TopDocs().ScoreDocs; var results = _mapLuceneToDataList(hits, searcher); analyzer.Close(); searcher.Dispose(); return(results); } // search by multiple fields (ordered by RELEVANCE) else { var parser = new MultiFieldQueryParser (Lucene.Net.Util.Version.LUCENE_30, new[] { "Id", "TitleName", "SubtitleText" }, 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 void button3_Click(object sender, EventArgs e) { string q = GetKeyWordsSplitBySpace("潮流新配色", new PanGuTokenizer());; // 索引目录 LuceneNet.Store.Directory dir = FSDirectory.Open(new DirectoryInfo(StaticConst.LucenePath)); // 索引搜索器 IndexSearcher searcher = new IndexSearcher(dir, true); // 查询器 // QueryParser qp = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "Title", new PanGuAnalyzer(true)); MultiFieldQueryParser qp = new MultiFieldQueryParser(LuceneNet.Util.Version.LUCENE_29, new string[] { "Title", "Description" }, new PanGuAnalyzer(true)); Query query = qp.Parse(q); TopDocs topDocs = searcher.Search(query, 20); PrintfDocs(searcher, topDocs); }
/// <summary> /// Builds the query. /// </summary> /// <param name="criteria">The criteria.</param> /// <returns></returns> public override object BuildQuery(ISearchCriteria criteria) { var builder = base.BuildQuery(criteria) as QueryBuilder; var query = builder.Query as BooleanQuery; var analyzer = new StandardAnalyzer(u.Version.LUCENE_30); if (criteria is CatalogItemSearchCriteria) { var c = criteria as CatalogItemSearchCriteria; var datesFilterStart = new TermRangeQuery( "startdate", c.StartDateFrom.HasValue ? DateTools.DateToString(c.StartDateFrom.Value, DateTools.Resolution.SECOND) : null, DateTools.DateToString(c.StartDate, DateTools.Resolution.SECOND), false, true); query.Add(datesFilterStart, Occur.MUST); if (c.EndDate.HasValue) { var datesFilterEnd = new TermRangeQuery( "enddate", DateTools.DateToString(c.EndDate.Value, DateTools.Resolution.SECOND), null, true, false); query.Add(datesFilterEnd, Occur.MUST); } if (c.Outlines != null && c.Outlines.Count > 0) { AddQuery("__outline", query, c.Outlines); } query.Add(new TermQuery(new Term("__hidden", "false")), Occur.MUST); if (!String.IsNullOrEmpty(c.Catalog)) { AddQuery("catalog", query, c.Catalog); } // Add search if (!String.IsNullOrEmpty(c.SearchPhrase)) { var searchPhrase = c.SearchPhrase; if (c.IsFuzzySearch) { var keywords = c.SearchPhrase.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); searchPhrase = string.Empty; searchPhrase = keywords.Aggregate( searchPhrase, (current, keyword) => current + String.Format("{0}~{1}", keyword.Replace("~", ""), c.FuzzyMinSimilarity.ToString(CultureInfo.InvariantCulture))); } var fields = new List<string> { "__content" }; if (c.Locale != null) { var contentField = string.Format("__content_{0}", c.Locale.ToLower()); fields.Add(contentField); } var parser = new MultiFieldQueryParser(u.Version.LUCENE_30, fields.ToArray(), analyzer) { DefaultOperator = QueryParser .Operator.OR }; var searchQuery = parser.Parse(searchPhrase); query.Add(searchQuery, Occur.MUST); } } else if (criteria is OrderSearchCriteria) { var c = criteria as OrderSearchCriteria; if (!String.IsNullOrEmpty(c.CustomerId)) { AddQuery("customerid", query, c.CustomerId); } } return builder; }