public void CanGenerateQuery(string query) { var parser = new QueryParser(); var result = parser.Parse(query); Debug.WriteLine(DebugQueryVisitor.Run(result)); var generatedQuery = GenerateQueryVisitor.Run(result); Assert.Equal(query, generatedQuery); }
private SubQueryFindingExpressionTreeVisitor (INodeTypeProvider nodeTypeProvider) { ArgumentUtility.CheckNotNull ("nodeTypeProvider", nodeTypeProvider); _nodeTypeProvider = nodeTypeProvider; _expressionTreeParser = new ExpressionTreeParser (_nodeTypeProvider, new NullExpressionTreeProcessor()); _queryParser = new QueryParser (_expressionTreeParser); }
public void CanParseQuery() { var parser = new QueryParser(); var result = parser.Parse("criteria"); Assert.NotNull(result); Assert.NotNull(result.Left); Assert.IsType<TermNode>(result.Left); Assert.Equal("criteria", ((TermNode)result.Left).Term); }
public void ValidateQuery(String query) { ANTLRStringStream string_stream = new ANTLRStringStream(query); QueryLexer lexer = new QueryLexer(string_stream); CommonTokenStream tokens = new CommonTokenStream(lexer); QueryParser parser = new QueryParser(tokens); QueryParser.statement_return obj = parser.statement(); Statement StatementObject = obj.ret; if (StatementObject is CreateTable) { CreateTable mod = (CreateTable)StatementObject; } }
public static void search(String indexDir, String q) { Directory dir = FSDriecotry.Open(new System.IO.FileInfo(indexDir)); IndexSearcher searcher = new IndexSearcher(dir, true); QueryParser parser = new QueryParser("contents", new StandardAnalyzer(Version.LUCENE_CURRENT)); Query query = parser.Parser(q); Lucene.Net.Saerch.TopDocs hits = searher.Search(query, 10); System.Console.WriteLine("Found " + hits.totalHits + " document(s) that matched query '" + q + "':"); for (int i = 0; i < hits.scoreDocs.Length; i++) { ScoreDoc scoreDoc = hits.ScoreDoc[i]; Document doc = searcher.Doc(scoreDoc.doc); System.Console.WriteLine(doc.Get("filename")); } searcher.Close(); }
static NhRelinqQueryParser() { var nodeTypeProvider = new NHibernateNodeTypeProvider(); var transformerRegistry = ExpressionTransformerRegistry.CreateDefault(); // Register custom expression transformers here: // transformerRegistry.Register (new MyExpressionTransformer()); var processor = ExpressionTreeParser.CreateDefaultProcessor(transformerRegistry); // Add custom processors here: // processor.InnerProcessors.Add (new MyExpressionTreeProcessor()); var expressionTreeParser = new ExpressionTreeParser(nodeTypeProvider, processor); _queryParser = new QueryParser(expressionTreeParser); }
public static QueryProcessResult Validate(string query) { if (String.IsNullOrEmpty(query)) return new QueryProcessResult { IsValid = true }; GroupNode result; try { var parser = new QueryParser(); result = parser.Parse(query); } catch (Exception ex) { return new QueryProcessResult { Message = ex.Message }; } var validator = new QueryProcessorVisitor(new HashSet<string> { "hidden", "fixed", "type" }); result.Accept(validator); return new QueryProcessResult { IsValid = true, UsesPremiumFeatures = validator.UsesPremiumFeatures }; }
public Searcher(string mainFolder, string taxonomyFolder, string thumbsFolder) { MaxResultCount = 50; mainSearchManager = new SearchManager(FSDirectory.open(new java.io.File(mainFolder))); taxonomyReader = new DirectoryTaxonomyReader(FSDirectory.open(new java.io.File(taxonomyFolder))); analyzer = FindAPhotoAnalyzers.ForSearching(); parser = new MultiFieldQueryParser(FieldName.DefaultSearchFields, analyzer); parser.setAllowLeadingWildcard(true); thumbSearchManager = new SearchManager(FSDirectory.open(new java.io.File(thumbsFolder))); using (var wrapper = mainSearchManager.Wrapper()) { indexPreferences = new IndexPreferences(wrapper.IndexSearcher); } }
public static QueryProcessResult Process(string query) { if (String.IsNullOrEmpty(query)) return new QueryProcessResult { IsValid = true }; GroupNode result; try { var parser = new QueryParser(); result = parser.Parse(query); } catch (Exception ex) { return new QueryProcessResult { Message = ex.Message }; } var validator = new QueryProcessorVisitor(new HashSet<string> { "hidden", "fixed", "type", "organization", "project" }); result.Accept(validator); string expandedQuery = validator.UsesDataFields ? GenerateQueryVisitor.Run(result) : query; return new QueryProcessResult { IsValid = true, UsesPremiumFeatures = validator.UsesPremiumFeatures, ExpandedQuery = expandedQuery }; }
public static QueryProcessResult Process(string query) { if (String.IsNullOrWhiteSpace(query)) return new QueryProcessResult { IsValid = true }; GroupNode result; try { var parser = new QueryParser(); result = parser.Parse(query); } catch (Exception ex) { return new QueryProcessResult { Message = ex.Message }; } var validator = new QueryProcessorVisitor(_freeFields); result.Accept(validator); var expandedQuery = validator.UsesDataFields ? GenerateQueryVisitor.Run(result) : query; return new QueryProcessResult { IsValid = true, UsesPremiumFeatures = validator.UsesPremiumFeatures, ExpandedQuery = expandedQuery }; }
public static IQueryParser CreateQueryParser() { //Create Custom node registry var customNodeTypeRegistry = new MethodInfoBasedNodeTypeRegistry(); //Register new clause type customNodeTypeRegistry.Register(FilterExpressionNode.SupportedMethods, typeof(FilterExpressionNode)); customNodeTypeRegistry.Register(LetSelectExpressionNode.SupportedMethods, typeof(LetSelectExpressionNode)); customNodeTypeRegistry.Register(LetLambdaExpressionNode.SupportedMethods, typeof(LetLambdaExpressionNode)); customNodeTypeRegistry.Register(TakeExpressionNode.SupportedMethods, typeof(TakeExpressionNode)); customNodeTypeRegistry.Register(SkipExpressionNode.SupportedMethods, typeof(SkipExpressionNode)); customNodeTypeRegistry.Register(GroupByExpressionNode.GroupBySupportedMethods, typeof(GroupByExpressionNode)); customNodeTypeRegistry.Register(LimitExpressionNode.SupportedMethods, typeof(LimitExpressionNode)); customNodeTypeRegistry.Register(UpdateAndReturnExpressionNode.SupportedMethods, typeof(UpdateAndReturnExpressionNode)); customNodeTypeRegistry.Register(RemoveAndReturnExpressionNode.SupportedMethods, typeof(RemoveAndReturnExpressionNode)); customNodeTypeRegistry.Register(InsertAndReturnExpressionNode.SupportedMethods, typeof(InsertAndReturnExpressionNode)); customNodeTypeRegistry.Register(UpsertAndReturnExpressionNode.SupportedMethods, typeof(UpsertAndReturnExpressionNode)); customNodeTypeRegistry.Register(InModifyExpressionNode.SupportedMethods, typeof(InModifyExpressionNode)); customNodeTypeRegistry.Register(ReturnResultModifyExpressionNode.SupportedMethods, typeof(ReturnResultModifyExpressionNode)); customNodeTypeRegistry.Register(QueryableExtensions.OrderBySupportedMethods, typeof(ArangoDB.Client.Common.Remotion.Linq.Parsing.Structure.IntermediateModel.OrderByExpressionNode)); customNodeTypeRegistry.Register(QueryableExtensions.OrderByDescendingSupportedMethods, typeof(ArangoDB.Client.Common.Remotion.Linq.Parsing.Structure.IntermediateModel.OrderByDescendingExpressionNode)); customNodeTypeRegistry.Register(QueryableExtensions.SelectManySupportedMethods, typeof(ArangoDB.Client.Common.Remotion.Linq.Parsing.Structure.IntermediateModel.SelectManyExpressionNode)); //This creates all the default node types var nodeTypeProvider = ExpressionTreeParser.CreateDefaultNodeTypeProvider(); //add custom node provider to the providers //nodeTypeProvider.InnerProviders.Add(customNodeTypeRegistry); nodeTypeProvider.InnerProviders.Insert(0, customNodeTypeRegistry); var transformerRegistry = ExpressionTransformerRegistry.CreateDefault(); var processor = ExpressionTreeParser.CreateDefaultProcessor(transformerRegistry); var expressionTreeParser = new ExpressionTreeParser(nodeTypeProvider, processor); var queryParser = new QueryParser(expressionTreeParser); return queryParser; }
public void AnotherExampleUsingParseQuery() { EmployeeDataContext db = new EmployeeDataContext(); IDictionary<string, Field<Employee>> fieldDefinitions = new Dictionary<string, Field<Employee>>() { { "id", new IntegerField<Employee>(e => e.id) }, { "lastname", new TextField<Employee>(e => e.LastName) }, { "firstname", new TextField<Employee>(e => e.FirstName) }, { "sex", new TextField<Employee>(e => e.Sex.ToString()) }, { "birthdate", new DateField<Employee>(e => e.BirthDate) }, { "hiredate", new DateField<Employee>(e => e.HireDate) } }; QueryParser<Employee> parser = new QueryParser<Employee>(e => fieldDefinitions.ContainsKey(e) ? fieldDefinitions[e] : null); Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < 100; ++i) { var x = parser.parse("(LastName eq 'Smith' | firstname = 'Mary') and sex = 'F' and hireDate < 1970-1-1"); } sw.Stop(); Debug.WriteLine(sw.ElapsedMilliseconds); var expr = parser.parse("(LastName eq 'Smith' | firstname = 'Mary') and sex = 'F' and hireDate < 1970-1-1"); Stopwatch sw2 = Stopwatch.StartNew(); for (int i = 0; i < 10; ++i) { var result = db.Employees.Where(expr).Select(e => e.id).Count(); Assert.AreEqual(837, result); } sw2.Stop(); Debug.WriteLine(sw2.ElapsedMilliseconds); }
private static string Escape(string term) { return(QueryParser.Escape(term).to_lower_invariant()); }
/// <summary> /// Searches the index. /// </summary> /// <param name="totalHits">The total hits.</param> /// <param name="forumId">The forum identifier.</param> /// <param name="userId">The user identifier.</param> /// <param name="searchQuery">The search query.</param> /// <param name="searchField">The search field.</param> /// <param name="pageIndex">Index of the page.</param> /// <param name="pageSize">Size of the page.</param> /// <returns> /// Returns the Search results /// </returns> private List <SearchMessage> SearchIndex( out int totalHits, int forumId, int userId, string searchQuery, string searchField = "", int pageIndex = 1, int pageSize = 1000) { if (string.IsNullOrEmpty(searchQuery.Replace("*", string.Empty).Replace("?", string.Empty))) { totalHits = 0; return(new List <SearchMessage>()); } // Insert forum access here var userAccessList = this.GetRepository <vaccess>().Get(v => v.UserID == userId); // filter forum if (forumId > 0) { userAccessList = userAccessList.FindAll(v => v.ForumID == forumId); } using (var searcher = new IndexSearcher(Directory, true)) { var hitsLimit = this.Get <YafBoardSettings>().ReturnSearchMax; var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); var formatter = new SimpleHTMLFormatter( "<mark>", "</mark>"); var fragmenter = new SimpleFragmenter(hitsLimit); QueryScorer scorer; // search by single field if (!string.IsNullOrEmpty(searchField)) { var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, searchField, analyzer); var query = this.ParseQuery(searchQuery, parser); scorer = new QueryScorer(query); var hits = searcher.Search(query, hitsLimit).ScoreDocs; totalHits = hits.Length; var highlighter = new Highlighter(formatter, scorer) { TextFragmenter = fragmenter }; var results = this.MapSearchToDataList( highlighter, analyzer, searcher, hits, pageIndex, pageSize, userAccessList); analyzer.Close(); searcher.Dispose(); return(results); } else { var parser = new MultiFieldQueryParser( Lucene.Net.Util.Version.LUCENE_30, new[] { "Message", "Topic", "Author" }, analyzer); var query = this.ParseQuery(searchQuery, parser); scorer = new QueryScorer(query); var hits = searcher.Search(query, null, hitsLimit, Sort.INDEXORDER).ScoreDocs; totalHits = hits.Length; var highlighter = new Highlighter(formatter, scorer) { TextFragmenter = fragmenter }; var results = this.MapSearchToDataList( highlighter, analyzer, searcher, hits, pageIndex, pageSize, userAccessList); analyzer.Close(); searcher.Dispose(); return(results); } } }
public void Can_parse_phrase() { var q = new QueryParser(new Analyzer()).Parse("+title:rambo first blood"); Assert.AreEqual("+title:rambo title:first title:blood", q.ToString()); }
private void ParseUri(MyUri uri, ref MyWebRequest request) { string str = ""; if ((request != null) && request.response.KeepAlive) { str = str + "连接转至: " + uri.Host + "\r\n\r\n"; } else { str = str + "连接: " + uri.Host + "\r\n\r\n"; } ListViewItem item = null; Monitor.Enter(this.listViewThreads); try { item = this.listViewThreads.Items[int.Parse(Thread.CurrentThread.Name)]; item.SubItems[1].Text = uri.Depth.ToString(); item.ImageIndex = 1; item.BackColor = System.Drawing.Color.WhiteSmoke; item.SubItems[2].Text = "正在连接"; item.ForeColor = System.Drawing.Color.Red; item.SubItems[3].Text = uri.AbsoluteUri; item.SubItems[4].Text = ""; item.SubItems[5].Text = ""; } catch (Exception) { } Monitor.Exit(this.listViewThreads); try { object obj2; request = MyWebRequest.Create(uri, request, this.KeepAlive); request.Timeout = this.RequestTimeout * 0x3e8; MyWebResponse response = request.GetResponse(); str = str + request.Header + response.Header; if (!response.ResponseUri.Equals(uri)) { this.EnqueueUri(new MyUri(response.ResponseUri.AbsoluteUri), true); obj2 = str; str = string.Concat(new object[] { obj2, "重定向到: ", response.ResponseUri, "\r\n" }); request = null; } else { if ((!this.AllMIMETypes && (response.ContentType != null)) && (this.MIMETypes.Length > 0)) { string str2 = response.ContentType.ToLower(); int index = str2.IndexOf(';'); if (index != -1) { str2 = str2.Substring(0, index); } if ((str2.IndexOf('*') == -1) && ((index = this.MIMETypes.IndexOf(str2)) == -1)) { this.LogError(uri.AbsoluteUri, str + "\r\nUnlisted Content-Type (" + str2 + "), check settings."); request = null; return; } Match match = new Regex(@"\d+").Match(this.MIMETypes, index); int num3 = int.Parse(match.Value) * 0x400; int num4 = int.Parse(match.NextMatch().Value) * 0x400; if ((num3 < num4) && ((response.ContentLength < num3) || (response.ContentLength > num4))) { this.LogError(uri.AbsoluteUri, string.Concat(new object[] { str, "\r\nContentLength limit error (", response.ContentLength, ")" })); request = null; return; } } string[] strArray = new string[] { ".gif", ".jpg", ".css", ".zip", ".exe" }; bool flag = true; foreach (string str3 in strArray) { if (uri.AbsoluteUri.ToLower().EndsWith(str3)) { flag = false; break; } } foreach (string str3 in this.ExcludeFiles) { if ((str3.Trim().Length > 0) && uri.AbsoluteUri.ToLower().EndsWith(str3)) { flag = false; break; } } string strBody = uri.ToString(); if (this.Compared(uri.LocalPath.Substring(uri.LocalPath.LastIndexOf('.') + 1).ToLower()) && (uri.ToString().Substring(uri.ToString().Length - 1, 1) != "/")) { this.LogError("丢弃--非网页文件", strBody); } else { int num5; UriKind absolute = UriKind.Absolute; if (!string.IsNullOrEmpty(strBody) && Uri.IsWellFormedUriString(strBody, absolute)) { string page = GetPage(strBody); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Html html = new Html { Web = page, Url = strBody }; CommonAnalyze analyze = new CommonAnalyze(); analyze.LoadHtml(html); Net.LikeShow.ContentAnalyze.Document result = analyze.GetResult(); stopwatch.Stop(); string bt = result.Title.Replace("[(title)]", ""); switch (bt) { case null: case "": bt = result.Doc.Substring(20).ToString(); break; } if ((result.Doc == null) || (result.Doc == "")) { this.LogError("丢弃--空内容或非内空页", strBody); } else { Lucene.Net.Documents.Document document3; string str7 = result.Doc + bt; if (this.cgcount >= 10) { string keywords = this.MD5string(result.Doc.ToString()); string keyWordsSplitBySpace = ""; IndexSearcher searcher = new IndexSearcher(this.path); keyWordsSplitBySpace = GetKeyWordsSplitBySpace(keywords, new KTDictSegTokenizer()); Query query = new QueryParser("J_md5_bai", new KTDictSegAnalyzer(true)).Parse(keyWordsSplitBySpace); if (searcher.Search(query).Doc(0).Get("J_md5_bai") == keywords) { this.LogError("排除--重复", strBody); } else { this.cgcount++; this.LogUri(bt, "引索完成"); document3 = new Lucene.Net.Documents.Document(); document3.Add(new Field("分类", this.page_py, Field.Store.YES, Field.Index.TOKENIZED)); document3.Add(new Field("J_title_bai", bt, Field.Store.YES, Field.Index.TOKENIZED)); document3.Add(new Field("J_msgContent_bai", str7, Field.Store.YES, Field.Index.TOKENIZED)); document3.Add(new Field("J_SiteType_bai", result.SiteType.ToString(), Field.Store.YES, Field.Index.NO)); document3.Add(new Field("J_URL_bai", strBody, Field.Store.YES, Field.Index.NO)); document3.Add(new Field("J_addtime_bai", DateTime.Now.ToShortDateString(), Field.Store.YES, Field.Index.NO)); document3.Add(new Field("J_md5_bai", this.MD5string(result.Doc.ToString()), Field.Store.YES, Field.Index.TOKENIZED)); this.writer.AddDocument(document3); } } else { this.cgcount++; this.LogUri(bt, "引索完成"); document3 = new Lucene.Net.Documents.Document(); document3.Add(new Field("分类", this.page_py, Field.Store.YES, Field.Index.TOKENIZED)); document3.Add(new Field("J_title_bai", bt, Field.Store.YES, Field.Index.TOKENIZED)); document3.Add(new Field("J_msgContent_bai", str7, Field.Store.YES, Field.Index.TOKENIZED)); document3.Add(new Field("J_SiteType_bai", result.SiteType.ToString(), Field.Store.YES, Field.Index.NO)); document3.Add(new Field("J_URL_bai", strBody, Field.Store.YES, Field.Index.NO)); document3.Add(new Field("J_addtime_bai", DateTime.Now.ToShortDateString(), Field.Store.YES, Field.Index.NO)); document3.Add(new Field("J_md5_bai", this.MD5string(result.Doc.ToString()), Field.Store.YES, Field.Index.TOKENIZED)); this.writer.AddDocument(document3); } } } item.SubItems[2].Text = "正在下载"; item.ForeColor = System.Drawing.Color.Black; string input = ""; byte[] buffer = new byte[0x2800]; int nNum = 0; while ((num5 = response.socket.Receive(buffer, 0, 0x2800, SocketFlags.None)) > 0) { nNum += num5; if (flag) { input = input + Encoding.ASCII.GetString(buffer, 0, num5); } item.SubItems[4].Text = this.Commas(nNum); if (response.ContentLength > 0) { item.SubItems[5].Text = '%' + ((100 - (((response.ContentLength - nNum) * 100) / response.ContentLength))).ToString(); } if ((response.KeepAlive && (nNum >= response.ContentLength)) && (response.ContentLength > 0)) { break; } } if (response.KeepAlive) { str = str + "Connection kept alive to be used in subpages.\r\n"; } else { response.Close(); str = str + "Connection closed.\r\n"; } this.FileCount++; this.ByteCount += nNum; if ((this.ThreadsRunning && flag) && (uri.Depth < this.WebDepth)) { str = str + "\r\nParsing page ...\r\n"; string pattern = "(href|HREF|src|SRC)[ ]*=[ ]*[\"'][^\"'#>]+[\"']"; MatchCollection matchs = new Regex(pattern).Matches(input); obj2 = str; str = string.Concat(new object[] { obj2, "Found: ", matchs.Count, " ref(s)\r\n" }); this.URLCount += matchs.Count; foreach (Match match in matchs) { pattern = match.Value.Substring(match.Value.IndexOf('=') + 1).Trim(new char[] { '"', '\'', '#', ' ', '>' }); try { if (!(((pattern.IndexOf("..") == -1) && !pattern.StartsWith("/")) && pattern.StartsWith("http://"))) { pattern = new Uri(uri, pattern).AbsoluteUri; } this.Normalize(ref pattern); MyUri uri2 = new MyUri(pattern); if ((((uri2.Scheme != Uri.UriSchemeHttp) && (uri2.Scheme != Uri.UriSchemeHttps)) || ((uri2.Host.Split(new char[] { '.' })[1] != this.urllhost[1]) && this.KeepSameServer)) || !this.Compared_jpg(uri2.LocalPath.Substring(uri2.LocalPath.LastIndexOf('.') + 1).ToLower())) { continue; } Global.URL = uri2.ToString(); if ((Global.BXBH != "") && (Redspider_link.bxbh() == 2)) { continue; } uri2.Depth = uri.Depth + 1; if (this.EnqueueUri(uri2, true)) { str = str + uri2.AbsoluteUri + "\r\n"; } } catch (Exception) { } } } } } } catch (Exception exception) { this.LogError(uri.AbsoluteUri, str + exception.Message); request = null; } finally { this.EraseItem(item); } }
public List <SearchEntry> Search(IBaseWindow baseWin, string searchText) { if (baseWin == null) { throw new ArgumentNullException("baseWin"); } const uint flags = (uint)(QueryParser.feature_flag.FLAG_PARTIAL | QueryParser.feature_flag.FLAG_WILDCARD | QueryParser.feature_flag.FLAG_PHRASE | QueryParser.feature_flag.FLAG_BOOLEAN | QueryParser.feature_flag.FLAG_LOVEHATE); List <SearchEntry> res = new List <SearchEntry>(); try { lock (fLock) { using (Database database = new Database(GetXDBFolder())) using (Enquire enquire = new Enquire(database)) using (Stem stemmer = new Stem("russian")) using (QueryParser qp = new QueryParser()) { qp.SetStemmer(stemmer); qp.SetDatabase(database); qp.SetDefaultOp(Query.op.OP_AND); qp.SetStemmingStrategy(QueryParser.stem_strategy.STEM_SOME); string qs = searchText + " ged:" + GetSign(baseWin); qp.AddBooleanPrefix("ged", "GDB"); using (Query query = qp.ParseQuery(qs, flags)) { enquire.SetQuery(query); using (MSet matches = enquire.GetMSet(0, 100)) { MSetIterator m = matches.Begin(); while (m != matches.End()) { try { using (Document mDoc = m.GetDocument()) { SearchEntry entry = new SearchEntry(); entry.XRef = mDoc.GetData(); entry.Rank = m.GetRank() + 1; entry.Percent = m.GetPercent(); res.Add(entry); } } catch (Exception ex) { Logger.LogWrite("SearchManager.Search(): " + ex.Message); } m = m.Next(); } } } } } } catch (Exception ex) { Logger.LogWrite("SearchManager.Search(): " + ex.Message); } return(res); }
protected internal Query GetFieldInternalQuery(string fieldName, IExamineValue fieldValue, bool useQueryParser) { Query queryToAdd; switch (fieldValue.Examineness) { case Examineness.Fuzzy: if (useQueryParser) { queryToAdd = this.QueryParser.GetFuzzyQuery(fieldName, fieldValue.Value, fieldValue.Level); } else { //REFERENCE: http://lucene.apache.org/java/2_4_0/queryparsersyntax.html#Fuzzy%20Searches var proxQuery = fieldName + ":" + fieldValue.Value + "~" + Convert.ToInt32(fieldValue.Level).ToString(); queryToAdd = ParseRawQuery(proxQuery); } break; case Examineness.SimpleWildcard: case Examineness.ComplexWildcard: if (useQueryParser) { queryToAdd = this.QueryParser.GetWildcardQuery(fieldName, fieldValue.Value); } else { //this will already have a * or a . suffixed based on the extension methods //REFERENCE: http://lucene.apache.org/java/2_4_0/queryparsersyntax.html#Wildcard%20Searches var proxQuery = fieldName + ":" + fieldValue.Value; queryToAdd = ParseRawQuery(proxQuery); } break; case Examineness.Boosted: if (useQueryParser) { queryToAdd = this.QueryParser.GetFieldQuery(fieldName, fieldValue.Value); queryToAdd.SetBoost(fieldValue.Level); } else { //REFERENCE: http://lucene.apache.org/java/2_4_0/queryparsersyntax.html#Boosting%20a%20Term var proxQuery = fieldName + ":\"" + fieldValue.Value + "\"^" + Convert.ToInt32(fieldValue.Level).ToString(); queryToAdd = ParseRawQuery(proxQuery); } break; case Examineness.Proximity: //This is how you are supposed to do this based on this doc here: //http://lucene.apache.org/java/2_4_1/api/org/apache/lucene/search/spans/package-summary.html#package_description //but i think that lucene.net has an issue with it's internal parser since it parses to a very strange query //we'll just manually make it instead below //var spans = new List<SpanQuery>(); //foreach (var s in fieldValue.Value.Split(' ')) //{ // spans.Add(new SpanTermQuery(new Term(fieldName, s))); //} //queryToAdd = new SpanNearQuery(spans.ToArray(), Convert.ToInt32(fieldValue.Level), true); var qry = fieldName + ":\"" + fieldValue.Value + "\"~" + Convert.ToInt32(fieldValue.Level).ToString(); if (useQueryParser) { queryToAdd = QueryParser.Parse(qry); } else { queryToAdd = ParseRawQuery(qry); } break; case Examineness.Escaped: //when an item is escaped it should be an exact match // http://examine.codeplex.com/workitem/10359 //standard query ... no query parser var stdQuery = fieldName + ":" + fieldValue.Value; queryToAdd = ParseRawQuery(stdQuery); break; case Examineness.Explicit: default: if (useQueryParser) { queryToAdd = this.QueryParser.GetFieldQuery(fieldName, fieldValue.Value); } else { //standard query var proxQuery = fieldName + ":" + fieldValue.Value; queryToAdd = ParseRawQuery(proxQuery); } break; } return(queryToAdd); }
public void Initialize() { scriptParser = new QueryParser(); }
/// <summary>Creates executor and get command.</summary> /// <returns>The new executor and get command.</returns> public virtual IRelationalCommand CreateExecutorAndGetCommand(out RelationalQueryContext queryContext) { bool isEFCore2x = false; bool EFCore_2_1 = false; var context = Query.GetDbContext(); // REFLECTION: Query._context.StateManager #if NETSTANDARD2_0 var stateManager = context.ChangeTracker.GetStateManager(); #else var stateManagerProperty = typeof(DbContext).GetProperty("StateManager", BindingFlags.NonPublic | BindingFlags.Instance); var stateManager = (StateManager)stateManagerProperty.GetValue(context); #endif // REFLECTION: Query._context.StateManager._concurrencyDetector var concurrencyDetectorField = typeof(StateManager).GetField("_concurrencyDetector", BindingFlags.NonPublic | BindingFlags.Instance); var concurrencyDetector = (IConcurrencyDetector)concurrencyDetectorField.GetValue(stateManager); // REFLECTION: Query.Provider._queryCompiler var queryCompilerField = typeof(EntityQueryProvider).GetField("_queryCompiler", BindingFlags.NonPublic | BindingFlags.Instance); var queryCompiler = queryCompilerField.GetValue(Query.Provider); // REFLECTION: Query.Provider.NodeTypeProvider (Use property for nullable logic) var nodeTypeProviderProperty = queryCompiler.GetType().GetProperty("NodeTypeProvider", BindingFlags.NonPublic | BindingFlags.Instance); object nodeTypeProvider; object QueryModelGenerator = null; if (nodeTypeProviderProperty == null) { EFCore_2_1 = true; var QueryModelGeneratorField = queryCompiler.GetType().GetField("_queryModelGenerator", BindingFlags.NonPublic | BindingFlags.Instance); QueryModelGenerator = QueryModelGeneratorField.GetValue(queryCompiler); var nodeTypeProviderField = QueryModelGenerator.GetType().GetField("_nodeTypeProvider", BindingFlags.NonPublic | BindingFlags.Instance); nodeTypeProvider = nodeTypeProviderField.GetValue(QueryModelGenerator); } else { nodeTypeProvider = nodeTypeProviderProperty.GetValue(queryCompiler); } // REFLECTION: Query.Provider._queryCompiler.CreateQueryParser(); #if NETSTANDARD2_0 QueryParser createQueryParser = null; if (EFCore_2_1) { var queryParserMethod = QueryModelGenerator.GetType().GetMethod("CreateQueryParser", BindingFlags.NonPublic | BindingFlags.Instance); createQueryParser = (QueryParser)queryParserMethod.Invoke(QueryModelGenerator, new[] { nodeTypeProvider }); } else { var queryParserMethod = queryCompiler.GetType().GetMethod("CreateQueryParser", BindingFlags.NonPublic | BindingFlags.Instance); createQueryParser = (QueryParser)queryParserMethod.Invoke(queryCompiler, new[] { nodeTypeProvider }); } #else var createQueryParserMethod = queryCompiler.GetType().GetMethod("CreateQueryParser", BindingFlags.NonPublic | BindingFlags.Static); var createQueryParser = (QueryParser)createQueryParserMethod.Invoke(null, new[] { nodeTypeProvider }); #endif // REFLECTION: Query.Provider._queryCompiler._database var databaseField = queryCompiler.GetType().GetField("_database", BindingFlags.NonPublic | BindingFlags.Instance); var database = (IDatabase)databaseField.GetValue(queryCompiler); // REFLECTION: Query.Provider._queryCompiler._evaluatableExpressionFilter #if NETSTANDARD2_0 IEvaluatableExpressionFilter evaluatableExpressionFilter = null; if (EFCore_2_1) { evaluatableExpressionFilter = (IEvaluatableExpressionFilter)QueryModelGenerator.GetType().GetField("_evaluatableExpressionFilter", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(QueryModelGenerator); } else { evaluatableExpressionFilter = (IEvaluatableExpressionFilter)queryCompiler.GetType().GetField("_evaluatableExpressionFilter", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(queryCompiler); } #else var evaluatableExpressionFilterField = queryCompiler.GetType().GetField("_evaluatableExpressionFilter", BindingFlags.NonPublic | BindingFlags.Static); var evaluatableExpressionFilter = (IEvaluatableExpressionFilter)evaluatableExpressionFilterField.GetValue(null); #endif // REFLECTION: Query.Provider._queryCompiler._queryContextFactory var queryContextFactoryField = queryCompiler.GetType().GetField("_queryContextFactory", BindingFlags.NonPublic | BindingFlags.Instance); var queryContextFactory = (IQueryContextFactory)queryContextFactoryField.GetValue(queryCompiler); // REFLECTION: Query.Provider._queryCompiler._queryContextFactory.CreateQueryBuffer var createQueryBufferDelegateMethod = (typeof(QueryContextFactory)).GetMethod("CreateQueryBuffer", BindingFlags.NonPublic | BindingFlags.Instance); var createQueryBufferDelegate = (Func <IQueryBuffer>)createQueryBufferDelegateMethod.CreateDelegate(typeof(Func <IQueryBuffer>), queryContextFactory); // REFLECTION: Query.Provider._queryCompiler._queryContextFactory._connection var connectionField = queryContextFactory.GetType().GetField("_connection", BindingFlags.NonPublic | BindingFlags.Instance); var connection = (IRelationalConnection)connectionField.GetValue(queryContextFactory); IQueryCompilationContextFactory queryCompilationContextFactory; object logger; var dependenciesProperty = typeof(Database).GetProperty("Dependencies", BindingFlags.NonPublic | BindingFlags.Instance); if (dependenciesProperty != null) { // EFCore 2.x isEFCore2x = true; var dependencies = dependenciesProperty.GetValue(database); var queryCompilationContextFactoryField = typeof(DbContext).GetTypeFromAssembly_Core("Microsoft.EntityFrameworkCore.Storage.DatabaseDependencies") .GetProperty("QueryCompilationContextFactory", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); queryCompilationContextFactory = (IQueryCompilationContextFactory)queryCompilationContextFactoryField.GetValue(dependencies); var dependenciesProperty2 = typeof(QueryCompilationContextFactory).GetProperty("Dependencies", BindingFlags.NonPublic | BindingFlags.Instance); var dependencies2 = dependenciesProperty2.GetValue(queryCompilationContextFactory); // REFLECTION: Query.Provider._queryCompiler._database._queryCompilationContextFactory.Logger var loggerField = typeof(DbContext).GetTypeFromAssembly_Core("Microsoft.EntityFrameworkCore.Query.Internal.QueryCompilationContextDependencies") .GetProperty("Logger", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); // (IInterceptingLogger<LoggerCategory.Query>) logger = loggerField.GetValue(dependencies2); } else { // EFCore 1.x // REFLECTION: Query.Provider._queryCompiler._database._queryCompilationContextFactory var queryCompilationContextFactoryField = typeof(Database).GetField("_queryCompilationContextFactory", BindingFlags.NonPublic | BindingFlags.Instance); queryCompilationContextFactory = (IQueryCompilationContextFactory)queryCompilationContextFactoryField.GetValue(database); // REFLECTION: Query.Provider._queryCompiler._database._queryCompilationContextFactory.Logger var loggerField = queryCompilationContextFactory.GetType().GetProperty("Logger", BindingFlags.NonPublic | BindingFlags.Instance); logger = loggerField.GetValue(queryCompilationContextFactory); } // CREATE connection { QueryConnection = context.Database.GetService <IRelationalConnection>(); var innerConnection = new CreateEntityConnection(QueryConnection.DbConnection, null); var innerConnectionField = typeof(RelationalConnection).GetField("_connection", BindingFlags.NonPublic | BindingFlags.Instance); var initalConnection = innerConnectionField.GetValue(QueryConnection); innerConnectionField.SetValue(QueryConnection, new Microsoft.EntityFrameworkCore.Internal.LazyRef <DbConnection>(() => innerConnection)); RestoreConnection = () => innerConnectionField.SetValue(QueryConnection, initalConnection); } // CREATE query context { var relationalQueryContextType = typeof(RelationalQueryContext); var relationalQueryContextConstructor = relationalQueryContextType.GetConstructors()[0]; // EF Core 1.1 preview if (relationalQueryContextConstructor.GetParameters().Length == 5) { // REFLECTION: Query.Provider._queryCompiler._queryContextFactory.ExecutionStrategyFactory var executionStrategyFactoryField = queryContextFactory.GetType().GetProperty("ExecutionStrategyFactory", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy); var executionStrategyFactory = executionStrategyFactoryField.GetValue(queryContextFactory); var lazyRefStateManager = new LazyRef <IStateManager>(() => stateManager); queryContext = (RelationalQueryContext)relationalQueryContextConstructor.Invoke(new object[] { createQueryBufferDelegate, QueryConnection, lazyRefStateManager, concurrencyDetector, executionStrategyFactory }); } else if (isEFCore2x) { // REFLECTION: Query.Provider._queryCompiler._queryContextFactory.ExecutionStrategyFactory var executionStrategyFactoryField = queryContextFactory.GetType().GetProperty("ExecutionStrategyFactory", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy); var executionStrategyFactory = executionStrategyFactoryField.GetValue(queryContextFactory); var lazyRefStateManager = new LazyRef <IStateManager>(() => stateManager); var dependenciesProperty3 = typeof(RelationalQueryContextFactory).GetProperty("Dependencies", BindingFlags.NonPublic | BindingFlags.Instance); var dependencies3 = dependenciesProperty3.GetValue(queryContextFactory); queryContext = (RelationalQueryContext)relationalQueryContextConstructor.Invoke(new object[] { dependencies3, createQueryBufferDelegate, QueryConnection, executionStrategyFactory }); } else { queryContext = (RelationalQueryContext)relationalQueryContextConstructor.Invoke(new object[] { createQueryBufferDelegate, QueryConnection, stateManager, concurrencyDetector }); } } Expression newQuery; if (isEFCore2x) { var parameterExtractingExpressionVisitorConstructors = typeof(ParameterExtractingExpressionVisitor).GetConstructors(); if (parameterExtractingExpressionVisitorConstructors.Any(x => x.GetParameters().Length == 5)) { // EF Core 2.1 var parameterExtractingExpressionVisitorConstructor = parameterExtractingExpressionVisitorConstructors.First(x => x.GetParameters().Length == 5); var parameterExtractingExpressionVisitor = (ParameterExtractingExpressionVisitor)parameterExtractingExpressionVisitorConstructor.Invoke(new object[] { evaluatableExpressionFilter, queryContext, logger, true, false }); // CREATE new query from query visitor newQuery = parameterExtractingExpressionVisitor.ExtractParameters(Query.Expression); } else { // EF Core 2.1 Preview 2. We pass null for the DbContext, may require something else! var parameterExtractingExpressionVisitorConstructor = parameterExtractingExpressionVisitorConstructors.First(x => x.GetParameters().Length == 6); var parameterExtractingExpressionVisitor = (ParameterExtractingExpressionVisitor)parameterExtractingExpressionVisitorConstructor.Invoke(new object[] { evaluatableExpressionFilter, queryContext, logger, null, true, false }); // CREATE new query from query visitor newQuery = parameterExtractingExpressionVisitor.ExtractParameters(Query.Expression); } } else { // CREATE new query from query visitor var extractParametersMethods = typeof(ParameterExtractingExpressionVisitor).GetMethod("ExtractParameters", BindingFlags.Public | BindingFlags.Static); newQuery = (Expression)extractParametersMethods.Invoke(null, new object[] { Query.Expression, queryContext, evaluatableExpressionFilter, logger }); } // PARSE new query var queryModel = createQueryParser.GetParsedQuery(newQuery); // CREATE query model visitor var queryModelVisitor = (RelationalQueryModelVisitor)queryCompilationContextFactory.Create(false).CreateQueryModelVisitor(); // REFLECTION: Query.Provider._queryCompiler._database._queryCompilationContextFactory.Create(false).CreateQueryModelVisitor().CreateQueryExecutor() var createQueryExecutorMethod = queryModelVisitor.GetType().GetMethod("CreateQueryExecutor"); var createQueryExecutorMethodGeneric = createQueryExecutorMethod.MakeGenericMethod(Query.ElementType); var queryExecutor = createQueryExecutorMethodGeneric.Invoke(queryModelVisitor, new[] { queryModel }); // SET value QueryExecutor = queryExecutor; QueryContext = queryContext; // RETURN the IRealationCommand var sqlQuery = queryModelVisitor.Queries.First(); var relationalCommand = sqlQuery.CreateDefaultQuerySqlGenerator().GenerateSql(queryContext.ParameterValues); return(relationalCommand); }
/// <summary> /// Does the search and stores the information about the results. /// </summary> private void search() { DateTime start = DateTime.Now; // create the searcher // index is placed in "index" subdirectory string indexDirectory = Server.MapPath("~/App_Data/index"); //var analyzer = new StandardAnalyzer(Version.LUCENE_30); var analyzer = new Lucene.Net.Analysis.PanGu.PanGuAnalyzer(); IndexSearcher searcher = new IndexSearcher(FSDirectory.Open(indexDirectory)); // parse the query, "text" is the default field to search var parser = new QueryParser(Version.LUCENE_30, "name", analyzer); parser.AllowLeadingWildcard = true; Query query = parser.Parse(this.Query); // create the result DataTable this.Results.Columns.Add("title", typeof(string)); this.Results.Columns.Add("sample", typeof(string)); this.Results.Columns.Add("path", typeof(string)); this.Results.Columns.Add("url", typeof(string)); // search TopDocs hits = searcher.Search(query, 200); this.total = hits.TotalHits; // create highlighter IFormatter formatter = new SimpleHTMLFormatter("<span style=\"font-weight:bold;\">", "</span>"); SimpleFragmenter fragmenter = new SimpleFragmenter(80); QueryScorer scorer = new QueryScorer(query); Highlighter highlighter = new Highlighter(formatter, scorer); highlighter.TextFragmenter = fragmenter; // initialize startAt this.startAt = InitStartAt(); // how many items we should show - less than defined at the end of the results int resultsCount = Math.Min(total, this.maxResults + this.startAt); for (int i = startAt; i < resultsCount; i++) { // get the document from index Document doc = searcher.Doc(hits.ScoreDocs[i].Doc); TokenStream stream = analyzer.TokenStream("", new StringReader(doc.Get("name"))); String sample = highlighter.GetBestFragments(stream, doc.Get("name"), 2, "..."); String path = doc.Get("path"); // create a new row with the result data DataRow row = this.Results.NewRow(); row["title"] = doc.Get("name"); //row["path"] = "api/" + path; //row["url"] = "www.dotlucene.net/documentation/api/" + path; //row["sample"] = sample; this.Results.Rows.Add(row); } searcher.Dispose(); // result information this.duration = DateTime.Now - start; this.fromItem = startAt + 1; this.toItem = Math.Min(startAt + maxResults, total); }
private void ProcessCommand(string line) { if (string.IsNullOrEmpty(line)) { return; } string[] parsed = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (parsed.Length == 0) { return; } string cmd = parsed[0]; string[] args = new string[parsed.Length - 1]; if (args.Length > 0) { Array.Copy(parsed, 1, args, 0, args.Length); } if ("help".Equals(cmd, StringComparison.OrdinalIgnoreCase) || "?".Equals(cmd, StringComparison.OrdinalIgnoreCase)) { Console.WriteLine(@"help or ? - prints this message"); Console.WriteLine(@"exit or q - quits"); Console.WriteLine(@"query <query string> - sets query text"); Console.WriteLine(@"facetspec <name>:<minHitCount>,<maxCount>,<isExpand>,<orderby> - add facet spec"); Console.WriteLine(@"page <offset>:<count> - set paging parameters"); Console.WriteLine(@"select <name>:<value1>,<value2>... - add selection, with ! in front of value indicates a not"); Console.WriteLine(@"sort <name>:<dir>,... - set sort specs (false for ascending, true for descending)"); Console.WriteLine(@"showReq: shows current request"); Console.WriteLine(@"clear: clears current request"); Console.WriteLine(@"clearSelections: clears all selections"); Console.WriteLine(@"clearSelection <name>: clear selection specified"); Console.WriteLine(@"clearFacetSpecs: clears all facet specs"); Console.WriteLine(@"clearFacetSpec <name>: clears specified facetspec"); Console.WriteLine(@"browse - executes a search"); } else if ("query".Equals(cmd, StringComparison.OrdinalIgnoreCase)) { if (parsed.Length < 2) { Console.WriteLine(@"query not defined."); } else { string queryString = parsed[1]; if (!string.IsNullOrEmpty(queryString)) { var qparser = new QueryParser(Lucene.Net.Util.Version.LUCENE_CURRENT, "contents", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT)); Query q; try { q = qparser.Parse(queryString); _reqBuilder.Request.Query = q; } catch (Exception e) { Console.WriteLine(e.ToString()); } } } } else if ("facetspec".Equals(cmd, StringComparison.OrdinalIgnoreCase)) { if (parsed.Length < 2) { Console.WriteLine("facetspec not defined."); } else { try { string fspecString = parsed[1]; string[] parts = fspecString.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); string name = parts[0]; string fvalue = parts[1]; string[] valParts = fvalue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (valParts.Length != 4) { Console.WriteLine(@"spec must of of the form <minhitcount>,<maxcount>,<isExpand>,<orderby>"); } else { int minHitCount = 1; int maxCount = 5; bool expand = false; FacetSpec.FacetSortSpec sort = FacetSpec.FacetSortSpec.OrderHitsDesc; try { minHitCount = int.Parse(valParts[0]); } catch { Console.WriteLine("default min hitcount = 1 is applied."); } try { maxCount = int.Parse(valParts[1]); } catch { Console.WriteLine("default maxCount = 5 is applied."); } try { expand = bool.Parse(valParts[2]); } catch { Console.WriteLine("default expand=false is applied."); } if ("hits".Equals(valParts[3])) { sort = FacetSpec.FacetSortSpec.OrderHitsDesc; } else { sort = FacetSpec.FacetSortSpec.OrderValueAsc; } _reqBuilder.ApplyFacetSpec(name, minHitCount, maxCount, expand, sort); } } catch (Exception e) { Console.WriteLine(e.ToString()); } } } else if ("select".Equals(cmd, StringComparison.OrdinalIgnoreCase)) { if (parsed.Length < 2) { Console.WriteLine("selection not defined."); } else { try { string selString = parsed[1]; string[] parts = selString.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); string name = parts[0]; string selList = parts[1]; string[] sels = selList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string sel in sels) { bool isNot = false; string val = sel; if (sel.StartsWith("!")) { isNot = true; val = sel.Substring(1); } if (!string.IsNullOrEmpty(val)) { _reqBuilder.AddSelection(name, val, isNot); } } } catch (Exception e) { Console.WriteLine(e.ToString()); } } } else if ("page".Equals(cmd, StringComparison.OrdinalIgnoreCase)) { try { string pageString = parsed[1]; string[] parts = pageString.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); _reqBuilder.Offset = int.Parse(parts[0]); _reqBuilder.Count = int.Parse(parts[1]); } catch (Exception e) { Console.WriteLine(e.ToString()); } } else if ("clearFacetSpec".Equals(cmd, StringComparison.OrdinalIgnoreCase)) { if (parsed.Length < 2) { Console.WriteLine("facet spec not defined."); } else { string name = parsed[1]; _reqBuilder.ClearFacetSpec(name); } } else if ("clearSelection".Equals(cmd, StringComparison.OrdinalIgnoreCase)) { if (parsed.Length < 2) { Console.WriteLine("selection name not defined."); } else { string name = parsed[1]; _reqBuilder.ClearSelection(name); } } else if ("clearSelections".Equals(cmd, StringComparison.OrdinalIgnoreCase)) { _reqBuilder.ClearSelections(); } else if ("clearFacetSpecs".Equals(cmd, StringComparison.OrdinalIgnoreCase)) { _reqBuilder.ClearFacetSpecs(); } else if ("clear".Equals(cmd, StringComparison.OrdinalIgnoreCase)) { _reqBuilder.Clear(); } else if ("showReq".Equals(cmd, StringComparison.OrdinalIgnoreCase)) { BrowseRequest req = _reqBuilder.Request; Console.WriteLine(req.ToString()); } else if ("sort".Equals(cmd, StringComparison.OrdinalIgnoreCase)) { if (parsed.Length == 2) { string sortString = parsed[1]; string[] sorts = sortString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); var sortList = new List <SortField>(); foreach (var sort in sorts) { string[] sortParams = sort.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); bool rev = false; if (sortParams.Length > 0) { string sortName = sortParams[0]; if (sortParams.Length > 1) { try { rev = bool.Parse(sortParams[1]); } catch (Exception e) { Console.WriteLine(e.Message + ", default rev to false"); } } sortList.Add(new SortField(sortName, SortField.STRING, rev)); } } _reqBuilder.ApplySort(sortList.ToArray()); } else { _reqBuilder.ApplySort(null); } } else if ("browse".Equals(cmd, StringComparison.OrdinalIgnoreCase)) { BrowseRequest req = _reqBuilder.Request; BrowseResult res = _svc.Browse(req); string output = BrowseResultFormatter.FormatResults(res); Console.WriteLine(output); } else { Console.WriteLine("Unknown command: " + cmd + ", do help for list of supported commands"); } }
public void TestThatAllWebPagesAreInTheIndex() { int minID; int maxID; SqlCommand sqlCommand = new SqlCommand("Select Min(ID) From WebPages"); sqlCommand.Connection = new SqlConnection("Data Source=.;Initial Catalog=arachnode.net;Integrated Security=True;Connection Timeout=3600;"); sqlCommand.Connection.Open(); using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader()) { sqlDataReader.Read(); minID = int.Parse(sqlDataReader.GetValue(0).ToString()); } sqlCommand.CommandText = "Select Max(ID) From WebPages"; using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader()) { sqlDataReader.Read(); maxID = int.Parse(sqlDataReader.GetValue(0).ToString()); } ApplicationSettings applicationSettings = new ApplicationSettings(); ArachnodeDAO arachnodeDAO = new ArachnodeDAO(applicationSettings.ConnectionString); IndexSearcher _indexSearcher = new IndexSearcher(FSDirectory.Open(new DirectoryInfo("M:\\LDNI")), true); StandardAnalyzer standardAnalyzer = new StandardAnalyzer(); QueryParser queryParser = new QueryParser("discoveryid", standardAnalyzer); for (int i = minID; i <= maxID; i++) { Debug.Print(i.ToString()); ArachnodeDataSet.WebPagesRow webPagesRow = arachnodeDAO.GetWebPage(i.ToString()); Query query = queryParser.Parse("\"" + webPagesRow.ID + "\""); Hits hits = _indexSearcher.Search(query); bool constainsTheWebPageAbsoluteUri = false; for (int j = 0; j < hits.Length(); j++) { if (hits.Doc(j).GetField("discoverytype").StringValue() == "webpage") { constainsTheWebPageAbsoluteUri = true; } } if (!constainsTheWebPageAbsoluteUri) { //ANODET: Set Breakpoint... } Assert.IsTrue(constainsTheWebPageAbsoluteUri); } sqlCommand.Connection.Close(); }
public void Can_parse_phrases() { var q = new QueryParser(new Analyzer(), 0.5f).Parse("title:was up~ subtitle:in da house"); Assert.AreEqual("+title:was~ title:up~ subtitle:in subtitle:da subtitle:house", q.ToString()); }
public void Can_parse_fuzzy_phrase() { var q = new QueryParser(new Analyzer(), 0.5f).Parse("title:was up~"); Assert.AreEqual("+title:was~ title:up~", q.ToString()); }
public void Can_parse_fuzzy_term() { var q = new QueryParser(new Analyzer()).Parse("title:raymbo~"); Assert.AreEqual("+title:raymbo~", q.ToString()); }
public void Can_parse_prefix_term() { var q = new QueryParser(new Analyzer()).Parse("title:ram*"); Assert.AreEqual("+title:ram*", q.ToString()); }
public static IQueryable <int> CreateIntQueryable() { return(new TestQueryable <int> (QueryParser.CreateDefault(), s_executor)); }
/// <summary>Creates an instance of the queryable entity factory.</summary> /// <param name="entityContext">Entity context to be used by this provider.</param> /// <param name="entitySource">Entity source.</param> /// <param name="store">Entity store</param> public EntityQueryable(IEntityContext entityContext, IEntitySource entitySource, IEntityStore store) : base(QueryParser.CreateDefault(), new EntityQueryExecutor(entityContext, entitySource, store)) { }
public static IQueryable <T> CreateQueryable <T> (IQueryExecutor executor) { ArgumentUtility.CheckNotNull("executor", executor); return(new TestQueryable <T> (QueryParser.CreateDefault(), executor)); }
// ------------------------------------------------------- // PRIVATE MEMBERS // ------------------------------------------------------- /// <summary> /// Gets the movie list SQL string. /// </summary> /// <param name="str">The STR.</param> /// <param name="filter">The filter.</param> /// <param name="value">The value.</param> /// <param name="number">The number.</param> /// <returns></returns> private StringBuilder GetMovieListSqlString(StringBuilder str, FilterType filter, string value, int number) { QueryParser parser = new QueryParser(); switch(filter) { case FilterType.NoFilter: case FilterType.ResetFilter: break; case FilterType.AllConferred: str.Append(" WHERE is_conferred = 1"); break; case FilterType.AllOriginals: str.Append(" WHERE is_original = 1"); break; case FilterType.Codec: str.Append(" WHERE codec = " + number.ToString()); break; case FilterType.WithoutGenre: str.Remove(0, str.Length); str.Append("SELECT DISTINCT m.*, s.Content"); str.Append(" FROM tbl_movies AS m"); str.Append(" "); str.Append(" INNER JOIN tbl_static AS s"); str.Append(" ON s.value = m.country AND s.tag = 'C002'"); str.Append(" "); str.Append(" LEFT JOIN tbl_movies_to_genres AS mg"); str.Append(" ON mg.movie_pkid = m.pkid"); str.Append(" LEFT JOIN tbl_genres AS g"); str.Append(" ON g.pkid = mg.genre_pkid"); str.Append(" WHERE g.pkid IS NULL"); break; case FilterType.Genre: str.Remove(0, str.Length); str.Append("SELECT DISTINCT m.*, s.Content"); str.Append(" FROM tbl_movies AS m"); str.Append(" "); str.Append(" INNER JOIN tbl_static AS s"); str.Append(" ON s.value = m.country AND s.tag = 'C002'"); str.Append(" "); str.Append(" INNER JOIN tbl_movies_to_genres AS mg"); str.Append(" ON mg.movie_pkid = m.pkid"); str.Append(" INNER JOIN tbl_genres AS g"); str.Append(" ON g.pkid = mg.genre_pkid"); if(this._cfg.ProviderType != ProviderType.SQLite) { str.Append(" WHERE g.pkid = '" + value + "'"); } else { str.Append(" WHERE lower(g.pkid) = lower('{" + value + "}')"); } break; case FilterType.Name: //str.Append(" WHERE (LOWER(name) LIKE LOWER('{" + value + "}')"); //str.Append(" OR number LIKE '{" + value + "}')"); //str.Append(" OR LOWER(note) LIKE LOWER('{" + value + "}')"); if(!value.IsNullOrTrimmedEmpty()) { parser.DefaultOperator = QueryOperator.AND; parser.QueryType = QueryType.Like; value = parser.Parse(value, new string[] { "name", "number", "note" }); str.Append(" WHERE (" + value + ")"); } break; case FilterType.NameAndAllConferred: //str.Append(" WHERE (LOWER(name) LIKE LOWER('{" + value + "}')"); //str.Append(" OR number LIKE '{" + value + "}')"); parser.DefaultOperator = QueryOperator.AND; parser.QueryType = QueryType.Like; value = parser.Parse(value, new string[] { "name", "number", "note" }); str.Append(" WHERE (" + value + ")"); str.Append(" AND is_conferred = 1"); break; case FilterType.NameAndAllOriginals: //str.Append(" WHERE (LOWER(name) LIKE LOWER('{" + value + "}')"); //str.Append(" OR number LIKE '{" + value + "}')"); parser.DefaultOperator = QueryOperator.AND; parser.QueryType = QueryType.Like; value = parser.Parse(value, new string[] { "name", "number", "note" }); str.Append(" WHERE (" + value + ")"); str.Append(" AND is_original = 1"); break; case FilterType.Actor: case FilterType.ActorAndAllConferred: case FilterType.ActorAndAllOriginals: case FilterType.Director: case FilterType.DirectorAndAllConferred: case FilterType.DirectorAndAllOriginals: case FilterType.Producer: case FilterType.ProducerAndAllConferred: case FilterType.ProducerAndAllOriginals: case FilterType.Cutter: case FilterType.CutterAndAllConferred: case FilterType.CutterAndAllOriginals: case FilterType.Cameraman: case FilterType.CameramanAndAllConferred: case FilterType.CameramanAndAllOriginals: case FilterType.Musician: case FilterType.MusicianAndAllConferred: case FilterType.MusicianAndAllOriginals: case FilterType.Writer: case FilterType.WriterAndAllConferred: case FilterType.WriterAndAllOriginals: str.Remove(0, str.Length); str.Append(" SELECT m.*, s.*"); str.Append(" FROM tbl_movies AS m"); str.Append(" "); str.Append(" INNER JOIN tbl_static AS s"); str.Append(" ON s.value = m.country AND s.tag = 'C002'"); str.Append(" "); str.Append(" INNER JOIN tbl_movies_to_persons AS mp"); str.Append(" ON mp.movie_pkid = m.pkid"); str.Append(" INNER JOIN tbl_persons AS p"); str.Append(" ON p.pkid = mp.person_pkid"); str.Append(" WHERE NOT m.pkid IS NULL"); str.Append(" AND ( "); if(this._cfg.ProviderType == ProviderType.SQLite) { str.Append(" ( LOWER(firstname) LIKE LOWER('{" + value + "}') AND LOWER(lastname) LIKE LOWER('{" + value + "}') )"); str.Append(" OR ( LOWER(firstname) + ' ' + LOWER(lastname) LIKE LOWER('{" + value + "}') ) "); str.Append(" OR ( LOWER(lastname) + ' ' + LOWER(firstname) LIKE LOWER('{" + value + "}') )"); str.Append(" )"); } else { str.Append(" ( LOWER(firstname) LIKE LOWER('%" + value + "%') AND LOWER(lastname) LIKE LOWER('%" + value + "%') )"); str.Append(" OR ( LOWER(firstname) + ' ' + LOWER(lastname) LIKE LOWER('%" + value + "%') ) "); str.Append(" OR ( LOWER(lastname) + ' ' + LOWER(firstname) LIKE LOWER('%" + value + "%') )"); str.Append(" )"); } break; case FilterType.Country: str.Remove(0, str.Length); str.Append(" SELECT m.*, s.*"); str.Append(" FROM tbl_movies AS m"); str.Append(" "); str.Append(" INNER JOIN tbl_static AS s"); str.Append(" ON s.value = m.country AND s.tag = 'C002'"); str.Append(" "); str.Append(" WHERE NOT m.pkid IS NULL"); str.Append(" AND s.tag = 'C002'"); str.Append(" AND ( "); if(this._cfg.ProviderType == ProviderType.SQLite) { str.Append(" s.content LIKE '{" + value + "}')"); str.Append(" OR s.value LIKE '{" + value + "}')"); } else { str.Append(" s.content LIKE '%" + value + "%')"); str.Append(" OR s.value LIKE '%" + value + "%')"); } str.Append(" )"); break; case FilterType.CountryAndAllConferred: str.Remove(0, str.Length); str.Append(" SELECT m.*, s.*"); str.Append(" FROM tbl_movies AS m"); str.Append(" "); str.Append(" INNER JOIN tbl_static AS s"); str.Append(" ON s.value = m.country AND s.tag = 'C002'"); str.Append(" "); str.Append(" WHERE NOT m.pkid IS NULL"); str.Append(" AND is_conferred = 1"); str.Append(" AND s.tag = 'C002'"); str.Append(" AND ( "); if(this._cfg.ProviderType == ProviderType.SQLite) { str.Append(" s.content LIKE '{" + value + "}')"); str.Append(" OR s.value LIKE '{" + value + "}')"); } else { str.Append(" s.content LIKE '%" + value + "%')"); str.Append(" OR s.value LIKE '%" + value + "%')"); } str.Append(" )"); break; case FilterType.CountryAndAllOriginals: str.Remove(0, str.Length); str.Append(" SELECT m.*, s.*"); str.Append(" FROM tbl_movies AS m"); str.Append(" "); str.Append(" INNER JOIN tbl_static AS s"); str.Append(" ON s.value = m.country AND s.tag = 'C002'"); str.Append(" "); str.Append(" WHERE NOT m.pkid IS NULL"); str.Append(" AND is_original = 1"); str.Append(" AND s.tag = 'C002'"); str.Append(" AND ( "); if(this._cfg.ProviderType == ProviderType.SQLite) { str.Append(" s.content LIKE '{" + value + "}')"); str.Append(" OR s.value LIKE '{" + value + "}')"); } else { str.Append(" s.content LIKE '%" + value + "%')"); str.Append(" OR s.value LIKE '%" + value + "%')"); } str.Append(" )"); break; case FilterType.WithoutCategory: str.Remove(0, str.Length); str.Append("SELECT DISTINCT m.*, s.Content"); str.Append(" FROM tbl_movies AS m"); str.Append(" "); str.Append(" INNER JOIN tbl_static AS s"); str.Append(" ON s.value = m.country AND s.tag = 'C002'"); str.Append(" "); str.Append(" LEFT JOIN tbl_movies_to_categories AS mc"); str.Append(" ON mc.movie_pkid = m.pkid"); str.Append(" LEFT JOIN tbl_categories AS c"); str.Append(" ON c.pkid = mc.category_pkid"); str.Append(" WHERE c.pkid IS NULL"); break; case FilterType.Category: str.Remove(0, str.Length); str.Append("SELECT DISTINCT m.*, s.Content"); str.Append(" FROM tbl_movies AS m"); str.Append(" "); str.Append(" INNER JOIN tbl_static AS s"); str.Append(" ON s.value = m.country AND s.tag = 'C002'"); str.Append(" "); str.Append(" INNER JOIN tbl_movies_to_categories AS mc"); str.Append(" ON mc.movie_pkid = m.pkid"); str.Append(" INNER JOIN tbl_categories AS c"); str.Append(" ON c.pkid = mc.category_pkid"); if(this._cfg.ProviderType != ProviderType.SQLite) { str.Append(" WHERE c.pkid = '" + value + "'"); } else { str.Append(" WHERE lower(c.pkid) = lower('{" + value + "}')"); } break; } switch(filter) { case FilterType.Actor: case FilterType.ActorAndAllConferred: case FilterType.ActorAndAllOriginals: str.Append(" AND p.is_actor = 1"); str.Append(" AND mp.as_actor = 1"); break; case FilterType.Director: case FilterType.DirectorAndAllConferred: case FilterType.DirectorAndAllOriginals: str.Append(" AND p.is_director = 1"); str.Append(" AND mp.as_director = 1"); break; case FilterType.Producer: case FilterType.ProducerAndAllConferred: case FilterType.ProducerAndAllOriginals: str.Append(" AND p.is_producer = 1"); str.Append(" AND mp.as_producer = 1"); break; case FilterType.Cutter: case FilterType.CutterAndAllConferred: case FilterType.CutterAndAllOriginals: str.Append(" AND p.is_cutter = 1"); str.Append(" AND mp.as_cutter = 1"); break; case FilterType.Cameraman: case FilterType.CameramanAndAllConferred: case FilterType.CameramanAndAllOriginals: str.Append(" AND p.is_cameraman = 1"); str.Append(" AND mp.as_cameraman = 1"); break; case FilterType.Musician: case FilterType.MusicianAndAllConferred: case FilterType.MusicianAndAllOriginals: str.Append(" AND p.is_musician = 1"); str.Append(" AND mp.as_musician = 1"); break; case FilterType.Writer: case FilterType.WriterAndAllConferred: case FilterType.WriterAndAllOriginals: str.Append(" AND p.is_writer = 1"); str.Append(" AND mp.as_writer = 1"); break; } switch(filter) { case FilterType.ActorAndAllConferred: case FilterType.DirectorAndAllConferred: case FilterType.ProducerAndAllConferred: case FilterType.CutterAndAllConferred: case FilterType.CameramanAndAllConferred: case FilterType.MusicianAndAllConferred: case FilterType.WriterAndAllConferred: str.Append(" AND m.is_conferred = 1"); break; case FilterType.ActorAndAllOriginals: case FilterType.DirectorAndAllOriginals: case FilterType.ProducerAndAllOriginals: case FilterType.CutterAndAllOriginals: case FilterType.CameramanAndAllOriginals: case FilterType.MusicianAndAllOriginals: case FilterType.WriterAndAllOriginals: str.Append(" AND m.is_original = 1"); break; } if(this._cfg.ProviderType == ProviderType.SQLite) { str = str.Replace(" = 1", " = 'True'"); } return str; }
public static QueryModel ParseQuery(Expression queryExpression) { var parser = QueryParser.CreateDefault(); return(parser.GetParsedQuery(queryExpression)); }
private void button7_Click_1(object sender, EventArgs e) { IndexSearcher searcher = new IndexSearcher(@"E:\2008-12-22"); Query query = new QueryParser("J_md5_bai", new KTDictSegAnalyzer()).Parse("B9D7929DF4E58E3906356832703DB072"); MessageBox.Show(searcher.Search(query).Length().ToString()); }
/// <summary> /// Initializes a new instance of the <see cref="SearchInfoSummary"/> class. /// </summary> /// <param name="features">The features to enable when building the summary data.</param> public SearchInfoSummary(SearchInfoSummaryFeature features) { this.totalCanceledSearches = 0L; this.totalSearchesWithResults = 0L; this.totalSearches = 0L; this.totalSearchesFromEachMethod = new Dictionary<SearchMethodType, int>(3); this.totalSearchesFromEachMethod.Add(SearchMethodType.Full, 0); this.totalSearchesFromEachMethod.Add(SearchMethodType.Normal, 0); this.totalSearchesFromEachMethod.Add(SearchMethodType.Quick, 0); this.totalSearchesFromEachIndex = new Dictionary<string, int>(2); this.totalUniqueSearchesAndTotalTimesUsed = new Dictionary<string, int>(); this.totalUniqueClausesAndTotalTimesUsed = new Dictionary<string, int>(); this.searchSpread = new Dictionary<DateTime, int>(); this.parser = new QueryParser(StaticValues.LibraryVersion, "Query Parser - SearchInfoSummary", new StandardAnalyzer(StaticValues.LibraryVersion)); this.features = features; }
public Statement ValidateQuery(string query) { Statement ret = null; try { ANTLRStringStream string_stream = new ANTLRStringStream(query); QueryLexer lexer = new QueryLexer(string_stream); CommonTokenStream tokens = new CommonTokenStream(lexer); QueryParser parser = new QueryParser(tokens); QueryParser.statement_return obj = parser.statement(); ret = obj.ret; } catch (Antlr.Runtime.MismatchedTokenException ex) { string token = m_TokenProcessor.GetTokenName(ex.Expecting); if (token == null) token = "."; else token = token + "."; string msg = "Error in processing query. Missing or invalid statement. Expecting: " + token; QueryParserException exception = new QueryParserException(msg); throw exception; } catch (Antlr.Runtime.MismatchedTreeNodeException ex) { QueryParserException exception = new QueryParserException(ex.Message); throw exception; } catch (Antlr.Runtime.NoViableAltException ex) { QueryParserException exception = new QueryParserException(ex.Message); throw exception; } catch (Antlr.Runtime.EarlyExitException ex) { QueryParserException exception = new QueryParserException(ex.Message); throw exception; } catch (Antlr.Runtime.FailedPredicateException ex) { QueryParserException exception = new QueryParserException(ex.Message); throw exception; } catch (Antlr.Runtime.MismatchedRangeException ex) { QueryParserException exception = new QueryParserException(ex.Message); throw exception; } catch (Antlr.Runtime.MismatchedSetException ex) { QueryParserException exception = new QueryParserException(ex.Message); throw exception; } catch (Antlr.Runtime.RecognitionException ex) { QueryParserException exception = new QueryParserException(ex.Message); throw exception; } catch (QueryParserException ex) { throw ex; } return ret; }
/// <summary> /// Add an Audio Track Array Dict /// </summary> /// <param name="xmlWriter"> /// The xml writer. /// </param> /// <param name="parsed"> /// The parsed. /// </param> private static void AudioListArrayDict(XmlTextWriter xmlWriter, QueryParser parsed) { xmlWriter.WriteStartElement("key"); xmlWriter.WriteString("AudioList"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("array"); foreach (AudioTrack track in parsed.AudioInformation) { AddAudioItem(xmlWriter, track); } xmlWriter.WriteEndElement(); }
static void SearchForPhrase(IndexSearcher searcher, string phrase) { using (new AutoStopWatch(string.Format("Search for {0}", phrase))) { var parser = new QueryParser(org.apache.lucene.util.Version.LUCENE_CURRENT, "Body", new StandardAnalyzer(org.apache.lucene.util.Version.LUCENE_CURRENT)); Query query = parser.parse(phrase); var hits = searcher.search(query, 100); Console.WriteLine("Found {0} results for {1}", hits.totalHits, phrase); int max = hits.totalHits; if (max > 100) max = 100; for (int i = 0; i < max; i++) { Console.WriteLine(hits.scoreDocs[i].doc); } } }
/// <summary> /// Write the Preset to a file /// </summary> /// <param name="xmlWriter"> /// The xml writer. /// </param> /// <param name="parsed"> /// The parsed. /// </param> /// <param name="preset"> /// The preset. /// </param> private static void WritePreset(XmlTextWriter xmlWriter, QueryParser parsed, Preset preset) { xmlWriter.WriteStartElement("dict"); AudioListArrayDict(xmlWriter, parsed); AddEncodeSettings(xmlWriter, parsed, preset); xmlWriter.WriteEndElement(); }
public void Can_parse_two_terms_as_NOT() { var q = new QueryParser(new Analyzer()).Parse("title:first -title:blood"); Assert.AreEqual("+title:first -title:blood", q.ToString()); }
public void Can_parse_three_terms_as_AND() { var q = new QueryParser(new Analyzer()).Parse("title:first +title:blood +genre:action"); Assert.AreEqual("+title:first +title:blood +genre:action", q.ToString()); }
/// <summary>Simple command-line based search demo.</summary> public static void Main(string[] args) { // The <CONSOLE_APP_NAME> should be the assembly name of the application // this code is compiled into. In .NET Framework, it is the name of the EXE file. // In .NET Core, you have the option of compiling this into either a DLL or an EXE // (see https://docs.microsoft.com/en-us/dotnet/core/deploying/index). // In the first case, the <CONSOLE_APP_NAME> will be "dotnet <DLL_NAME>.dll". string usage = "Usage: <CONSOLE_APP_NAME> <INDEX_DIRECTORY> [-f|--field <FIELD>] " + "[-r|--repeat <NUMBER>] [-qf|--queries-file <PATH>] [-q|--query <QUERY>] " + "[--raw] [-p|--page-size <NUMBER>]\n\n" + "Use no --query or --queries-file option for interactive mode.\n\n" + "See http://lucene.apache.org/core/4_8_0/demo/ for details."; if (args.Length < 1 || args.Length > 0 && ("?".Equals(args[0], StringComparison.Ordinal) || "-h".Equals(args[0], StringComparison.Ordinal) || "--help".Equals(args[0], StringComparison.Ordinal))) { Console.WriteLine(usage); Environment.Exit(0); } string index = args[0]; string field = "contents"; string queries = null; int repeat = 0; bool raw = false; string queryString = null; int hitsPerPage = 10; for (int i = 0; i < args.Length; i++) { if ("-f".Equals(args[i], StringComparison.Ordinal) || "-field".Equals(args[i], StringComparison.Ordinal)) { field = args[i + 1]; i++; } else if ("-qf".Equals(args[i], StringComparison.Ordinal) || "--queries-file".Equals(args[i], StringComparison.Ordinal)) { queries = args[i + 1]; i++; } else if ("-q".Equals(args[i], StringComparison.Ordinal) || "--query".Equals(args[i], StringComparison.Ordinal)) { queryString = args[i + 1]; i++; } else if ("-r".Equals(args[i], StringComparison.Ordinal) || "--repeat".Equals(args[i], StringComparison.Ordinal)) { repeat = int.Parse(args[i + 1], CultureInfo.InvariantCulture); i++; } else if ("--raw".Equals(args[i], StringComparison.Ordinal)) { raw = true; } else if ("-p".Equals(args[i], StringComparison.Ordinal) || "--paging".Equals(args[i], StringComparison.Ordinal)) { hitsPerPage = int.Parse(args[i + 1], CultureInfo.InvariantCulture); if (hitsPerPage <= 0) { Console.WriteLine("There must be at least 1 hit per page."); Environment.Exit(1); } i++; } } using (IndexReader reader = DirectoryReader.Open(FSDirectory.Open(index))) { IndexSearcher searcher = new IndexSearcher(reader); // :Post-Release-Update-Version.LUCENE_XY: Analyzer analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48); TextReader input = null; if (queries != null) { input = new StreamReader(new FileStream(queries, FileMode.Open, FileAccess.Read), Encoding.UTF8); } else { input = Console.In; } // :Post-Release-Update-Version.LUCENE_XY: QueryParser parser = new QueryParser(LuceneVersion.LUCENE_48, field, analyzer); while (true) { if (queries == null && queryString == null) { // prompt the user Console.WriteLine("Enter query (or press Enter to exit): "); } string line = queryString != null ? queryString : input.ReadLine(); if (line == null || line.Length == 0) { break; } line = line.Trim(); if (line.Length == 0) { break; } Query query = parser.Parse(line); Console.WriteLine("Searching for: " + query.ToString(field)); if (repeat > 0) // repeat & time as benchmark { DateTime start = DateTime.UtcNow; for (int i = 0; i < repeat; i++) { searcher.Search(query, null, 100); } DateTime end = DateTime.UtcNow; Console.WriteLine("Time: " + (end - start).TotalMilliseconds + "ms"); } DoPagingSearch(searcher, query, hitsPerPage, raw, queries == null && queryString == null); if (queryString != null) { break; } } } // Disposes reader }
private static Query QueryFromStringExpression(string expression, string field, Analyzer a) { return(QueryParser.Parse(c.What, field, a)); }
public int SearchIndex(string keyword, Page pg, List <Record> re) { int tothit = 0; Dictionary <string, string> dic = new Dictionary <string, string>(); BooleanQuery bQuery = new BooleanQuery(); string st = GetKeyWordsSplitBySpace(keyword); if (st != null && st != "") { //title = GetKeyWordsSplitBySpace(Request.Form["title"].ToString()); //QueryParser parseTitle = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Title", pgAnalyzer); //parseTitle.DefaultOperator = QueryParser.Operator.AND; //Query queryT = parseTitle.Parse(GetKeyWordsSplitBySpace(st)); //bQuery.Add(queryT, Occur.MUST); QueryParser parseContent = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Content", pgAnalyzer); parseContent.DefaultOperator = QueryParser.Operator.AND; // Query queryC = parseContent.Parse(GetKeyWordsSplitBySpace(st)); Query queryC = parseContent.Parse(st); bQuery.Add(queryC, Occur.MUST); dic.Add("title", st); // txtTitle = Request.Form["title"].ToString(); } //if (Request.Form["content"] != null && Request.Form["content"].ToString() != "") //{ // content = GetKeyWordsSplitBySpace(Request.Form["content"].ToString()); // QueryParser parse = new QueryParser("Content", PanGuAnalyzer); // Query query = parse.Parse(content); // parse.SetDefaultOperator(QueryParser.Operator.AND); // bQuery.Add(query, BooleanClause.Occur.MUST); // dic.Add("content", Request.Form["content"].ToString()); // txtContent = Request.Form["content"].ToString(); //} if (bQuery != null && bQuery.GetClauses().Length > 0) { //Lucene.Net.Store.Directory ad = Lucene.Net.Store.FSDirectory.Open(new DirectoryInfo(IndexDic)); IndexSearcher search = new IndexSearcher(Lu_IndexDic, true); Stopwatch stopwatch = Stopwatch.StartNew(); Sort sort = new Sort(new SortField("Title", SortField.DOC, true)); TopDocs docs = search.Search(bQuery, (Filter)null, pg.PageSize * pg.PageIndex, sort); stopwatch.Stop(); if (docs != null && docs.TotalHits > 0) { // lSearchTime = stopwatch.ElapsedMilliseconds; //txtPageFoot = GetPageFoot(PageIndex, PageSize, docs.totalHits, "sabrosus"); tothit = docs.TotalHits; for (int i = 0; i < docs.TotalHits; i++) { if (i >= (pg.PageIndex - 1) * pg.PageSize && i < pg.PageIndex * pg.PageSize) { Document doc = search.Doc(docs.ScoreDocs[i].Doc); Record model = new Record() { Title = doc.Get("Title").ToString(), Content = doc.Get("Content").ToString(), // AddTime = doc.Get("AddTime").ToString(), Uri = doc.Get("Uri").ToString() }; // re += model.Title + " " + model.Uri; Console.WriteLine(model.Title + " " + model.Uri); re.Add(SetHighlighter(keyword, model)); //list.Add(SetHighlighter(dicKeywords, model)); } } } } return(tothit); }
public void Can_parse() { var q = new QueryParser(new Analyzer()).Parse("title:first* -title:blood~ genre:action*"); Assert.AreEqual("+title:first* -title:blood~ genre:action*", q.ToString()); }
/// <summary> /// Add the encode settings to the preset /// </summary> /// <param name="xmlWriter"> /// The xml writer. /// </param> /// <param name="parsed"> /// The parsed. /// </param> /// <param name="preset"> /// The preset. /// </param> private static void AddEncodeSettings(XmlTextWriter xmlWriter, QueryParser parsed, Preset preset) { AddEncodeElement(xmlWriter, "ChapterMarkers", "integer", parsed.ChapterMarkers ? "1" : "0"); AddEncodeElement(xmlWriter, "Default", "integer", "0"); AddEncodeElement(xmlWriter, "FileFormat", "string", parsed.Format.ToUpper() + " file"); AddBooleanElement(xmlWriter, "Folder", false); AddEncodeElement(xmlWriter, "Mp4HttpOptimize", "integer", parsed.OptimizeMP4 ? "1" : "0"); AddEncodeElement(xmlWriter, "Mp4LargeFile", "integer", parsed.LargeMP4 ? "1" : "0"); AddEncodeElement(xmlWriter, "Mp4iPodCompatible", "integer", parsed.IpodAtom ? "1" : "0"); AddEncodeElement(xmlWriter, "PictureAutoCrop", "integer", "1"); AddEncodeElement(xmlWriter, "PictureBottomCrop", "integer", parsed.CropBottom); // Filters AddEncodeElement(xmlWriter, "PictureDeblock", "integer", parsed.DeBlock.ToString()); switch (parsed.Decomb) { case "Off": AddEncodeElement(xmlWriter, "PictureDecomb", "integer", "0"); AddEncodeElement(xmlWriter, "PictureDecombCustom", "string", string.Empty); break; case "Default": AddEncodeElement(xmlWriter, "PictureDecomb", "integer", "1"); AddEncodeElement(xmlWriter, "PictureDecombCustom", "string", string.Empty); break; default: AddEncodeElement(xmlWriter, "PictureDecomb", "integer", "2"); AddEncodeElement(xmlWriter, "PictureDecombCustom", "string", parsed.Decomb); break; } AddEncodeElement(xmlWriter, "PictureDecombDeinterlace", "integer", parsed.Decomb != "Off" ? "0" : "1"); switch (parsed.DeInterlace) { case "Off": AddEncodeElement(xmlWriter, "PictureDeinterlace", "integer", "0"); AddEncodeElement(xmlWriter, "PictureDeinterlaceCustom", "string", string.Empty); break; case "Fast": AddEncodeElement(xmlWriter, "PictureDeinterlace", "integer", "1"); AddEncodeElement(xmlWriter, "PictureDeinterlaceCustom", "string", string.Empty); break; case "Slow": AddEncodeElement(xmlWriter, "PictureDeinterlace", "integer", "2"); AddEncodeElement(xmlWriter, "PictureDeinterlaceCustom", "string", string.Empty); break; case "Slower": AddEncodeElement(xmlWriter, "PictureDeinterlace", "integer", "3"); AddEncodeElement(xmlWriter, "PictureDeinterlaceCustom", "string", string.Empty); break; default: AddEncodeElement(xmlWriter, "PictureDeinterlace", "integer", "4"); AddEncodeElement(xmlWriter, "PictureDeinterlaceCustom", "string", parsed.DeInterlace); break; } switch (parsed.DeNoise) { case "Off": AddEncodeElement(xmlWriter, "PictureDenoise", "integer", "0"); AddEncodeElement(xmlWriter, "PictureDenoiseCustom", "string", string.Empty); break; case "Weak": AddEncodeElement(xmlWriter, "PictureDenoise", "integer", "1"); AddEncodeElement(xmlWriter, "PictureDenoiseCustom", "string", string.Empty); break; case "Medium": AddEncodeElement(xmlWriter, "PictureDenoise", "integer", "2"); AddEncodeElement(xmlWriter, "PictureDenoiseCustom", "string", string.Empty); break; case "Strong": AddEncodeElement(xmlWriter, "PictureDenoise", "integer", "3"); AddEncodeElement(xmlWriter, "PictureDenoiseCustom", "string", string.Empty); break; default: AddEncodeElement(xmlWriter, "PictureDenoise", "integer", "4"); AddEncodeElement(xmlWriter, "PictureDenoiseCustom", "string", parsed.DeNoise); break; } int detelecine; switch (parsed.DeTelecine) { case "Off": detelecine = 0; break; case "Default": detelecine = 2; break; default: detelecine = 1; break; } AddEncodeElement(xmlWriter, "PictureDetelecine", "integer", detelecine.ToString()); AddEncodeElement(xmlWriter, "PictureDetelecineCustom", "string", detelecine == 1 ? parsed.DeTelecine : string.Empty); // Picture Settings AddEncodeElement(xmlWriter, "PictureHeight", "integer", parsed.Height.ToString()); AddEncodeElement(xmlWriter, "PictureKeepRatio", "integer", parsed.KeepDisplayAsect ? "1" : "0"); AddEncodeElement(xmlWriter, "PictureLeftCrop", "integer", parsed.CropLeft); AddEncodeElement(xmlWriter, "PictureModulus", "integer", parsed.AnamorphicModulus.ToString()); AddEncodeElement(xmlWriter, "PicturePAR", "integer", parsed.AnamorphicMode.ToString()); AddEncodeElement(xmlWriter, "PictureRightCrop", "integer", parsed.CropRight); AddEncodeElement(xmlWriter, "PictureTopCrop", "integer", parsed.CropTop); AddEncodeElement(xmlWriter, "PictureWidth", "integer", parsed.Width.ToString()); // Preset Information AddEncodeElement(xmlWriter, "PresetBuildNumber", "string", Properties.Settings.Default.hb_build.ToString()); AddEncodeElement(xmlWriter, "PresetDescription", "string", "No Description"); AddEncodeElement(xmlWriter, "PresetName", "string", preset.Name); AddEncodeElement(xmlWriter, "Type", "integer", "1"); // 1 is user preset, 0 is built in // Preset Settings AddEncodeElement(xmlWriter, "UsesMaxPictureSettings", "integer", (parsed.MaxWidth != 0 || parsed.MaxHeight != 0) ? "1" : "0"); AddEncodeElement(xmlWriter, "UsesPictureFilters", "integer", "1"); AddEncodeElement(xmlWriter, "UsesPictureSettings", "integer", "2"); // Video Settings AddEncodeElement(xmlWriter, "VideoAvgBitrate", "string", parsed.AverageVideoBitrate); AddEncodeElement(xmlWriter, "VideoEncoder", "string", parsed.VideoEncoder); AddEncodeElement(xmlWriter, "VideoFramerate", "string", parsed.VideoFramerate); AddEncodeElement(xmlWriter, "VideFrameratePFR", "integer", parsed.Pfr ? "1" : "0"); AddEncodeElement(xmlWriter, "VideoGrayScale", "integer", parsed.Grayscale ? "1" : "0"); AddEncodeElement(xmlWriter, "VideoQualitySlider", "real", parsed.VideoQuality.ToString()); int videoQualityType = 0; if (!string.IsNullOrEmpty(parsed.VideoTargetSize)) videoQualityType = 0; else if (!string.IsNullOrEmpty(parsed.AverageVideoBitrate)) videoQualityType = 1; else if (parsed.VideoQuality != 0) videoQualityType = 2; AddEncodeElement(xmlWriter, "VideoQualityType", "integer", videoQualityType.ToString()); AddEncodeElement(xmlWriter, "VideoTargetSize", "string", parsed.VideoTargetSize); AddEncodeElement(xmlWriter, "VideoTurboTwoPass", "integer", parsed.TurboFirstPass ? "1" : "0"); AddEncodeElement(xmlWriter, "VideoTwoPass", "integer", parsed.TwoPass ? "1" : "0"); // x264 string AddEncodeElement(xmlWriter, "x264Option", "string", parsed.H264Query); }
public virtual async Task <SearchResult> AutoCompleteAsync(string prefix, int languageId = -1, int maxResult = 10, SearchResultSortType orderBy = SearchResultSortType.LastUpDate) { var result = new SearchResult(); if (string.IsNullOrWhiteSpace(prefix)) { return(result); } prefix = prefix.Trim(); //replace multiple spaces with a single space RegexOptions options = RegexOptions.None; Regex regex = new Regex("[ ]{2,}", options); prefix = regex.Replace(prefix, " "); var watch = new System.Diagnostics.Stopwatch(); watch.Start(); try { await Task.Run(() => { using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath))) { using (var searcher = new IndexSearcher(directory, readOnly: true)) { BooleanFilter filter = null; if (languageId > -1) { filter = new BooleanFilter(); filter.Add(new FilterClause( new QueryWrapperFilter(new TermQuery(new Term("LanguageId", languageId.ToString()))), Occur.MUST)); } Sort sort = new Sort(SortField.FIELD_SCORE); switch (orderBy) { case SearchResultSortType.NumberOfVisits: sort = new Sort(new SortField("NumberOfVisit", SortField.INT, true)); break; case SearchResultSortType.PublishDate: sort = new Sort(new SortField("PublishDate", SortField.LONG, true)); break; case SearchResultSortType.LastUpDate: sort = new Sort(new SortField("LastUpDate", SortField.LONG, true)); break; } var analyzer = new StandardAnalyzer(Version); var parser = new QueryParser(Version, "Title", analyzer); var query = ParseQuery(prefix.Replace(" ", "*") + "*", parser); var hits = searcher.Search(query, filter, maxResult, sort).ScoreDocs; foreach (var scoreDoc in hits) { var doc = searcher.Doc(scoreDoc.Doc); result.Documents.Add(new SearchResultDocument() { DocumentId = int.Parse(doc.Get("ID")), LanguageId = int.Parse(doc.Get("LanguageId")), LanguageIsoCode = doc.Get("LanguageCode"), Score = scoreDoc.Score, DocumentTitle = doc.Get("Title"), DocumentBody = doc.Get("Description"), DocumentKeywords = doc.Get("Keywords"), DocumentTags = doc.Get("Tags"), }); } result.Documents = result.Documents.DistinctBy(p => new { p.DocumentId }) .ToList(); } } }); } catch (Exception ex) { result.Error = ex; result.HasError = true; } watch.Stop(); result.ElapsedMilliseconds = watch.ElapsedMilliseconds; return(result); }
public static QueryParser Import(string filename) { XmlNode root = loadFile(filename); if (root == null) return null; // We'll query a query parser object and use it's public var structures to store all the data. // This will allow the preset loader logic to be used instead of writing custom logic just for this file. QueryParser queryParsed = new QueryParser(); string qualityMode = string.Empty; #region Get a List of Audio Track Objects XmlNode audioListDict = root.ChildNodes[2].ChildNodes[0].FirstChild.ChildNodes[1]; ArrayList audioTracks = new ArrayList(); for (int i = 0; i < audioListDict.ChildNodes.Count; i++) { XmlNode audioChannel = audioListDict.ChildNodes[i]; AudioTrack track = new AudioTrack(); for (int subi = 0; subi < audioChannel.ChildNodes.Count; subi += 2) { // Audio Channel Information is here. string key = audioChannel.ChildNodes[subi].InnerText; string value = audioChannel.ChildNodes[subi + 1].InnerText; switch (key) { case "AudioBitrate": track.Bitrate = value; break; case "AudioEncoder": track.Encoder = value.Replace("AAC (CoreAudio)", "AAC (faac)"); break; case "AudioMixdown": track.MixDown = value; break; case "AudioSamplerate": track.SampleRate = value; break; case "AudioTrack": track.Track = value; break; case "AudioTrackDRCSlider": track.DRC = value; break; } } audioTracks.Add(track); queryParsed.AudioInformation = audioTracks; } #endregion #region Parse the reset of the plist keys into local variables XmlNode presetSettings = root.ChildNodes[2].ChildNodes[0].FirstChild; // Start from 2 to avoid the audio settings which we don't need. for (int i = 2; i < presetSettings.ChildNodes.Count; i += 2) { string key = presetSettings.ChildNodes[i].InnerText; string value = presetSettings.ChildNodes[i + 1].InnerText; switch (key) { // Output Settings case "FileFormat": queryParsed.Format = value; break; case "Mp4HttpOptimize": queryParsed.OptimizeMP4 = value == "1"; break; case "Mp4LargeFile": queryParsed.LargeMP4 = value == "1"; break; case "Mp4iPodCompatible": queryParsed.IpodAtom = value == "1"; break; // Picture Settings case "PictureAutoCrop": // Not used break; case "PictureTopCrop": queryParsed.CropTop = value; break; case "PictureBottomCrop": queryParsed.CropBottom = value; break; case "PictureLeftCrop": queryParsed.CropLeft = value; break; case "PictureRightCrop": queryParsed.CropRight = value; break; case "PictureHeight": queryParsed.Height = int.Parse(value); break; case "PictureWidth": queryParsed.Width = int.Parse(value); break; case "PictureKeepRatio": queryParsed.KeepDisplayAsect = value == "1"; break; case "PicturePAR": queryParsed.AnamorphicMode = int.Parse(value); break; // Filters case "PictureDeblock": queryParsed.DeBlock = int.Parse(value); break; case "PictureDecomb": queryParsed.Decomb = "Off"; // Don't place custom here as it's handled in the filter panel if (value == "2") queryParsed.Decomb = "Default"; break; case "PictureDecombCustom": if (value != string.Empty) queryParsed.Decomb = value; break; case "PictureDecombDeinterlace": // Not Used break; case "PictureDeinterlace": switch (value) { case "0": queryParsed.DeInterlace = "Off"; break; // Don't place custom here as it's handled in the filter panel case "2": queryParsed.DeInterlace = "Fast"; break; case "3": queryParsed.DeInterlace = "Slow"; break; case "4": queryParsed.DeInterlace = "Slowest"; break; } break; case "PictureDeinterlaceCustom": if (value != string.Empty) queryParsed.DeInterlace = value; break; case "PictureDenoise": switch (value) { case "0": queryParsed.DeNoise = "Off"; break; // Don't place custom here as it's handled in the filter panel case "2": queryParsed.DeNoise = "Weak"; break; case "3": queryParsed.DeNoise = "Medium"; break; case "4": queryParsed.DeNoise = "Strong"; break; } break; case "PictureDenoiseCustom": if (value != string.Empty) queryParsed.DeNoise = value; break; case "PictureDetelecine": queryParsed.DeTelecine = "Off"; if (value == "1") queryParsed.DeTelecine = "Default"; break; case "PictureDetelecineCustom": if (value != string.Empty) queryParsed.DeTelecine = value; break; // Video Tab case "VideoAvgBitrate": queryParsed.AverageVideoBitrate = value; break; case "VideoEncoder": queryParsed.VideoEncoder = value; break; case "VideoFramerate": queryParsed.VideoFramerate = value; break; case "VideoGrayScale": queryParsed.Grayscale = value == "1"; break; case "VideoQualitySlider": queryParsed.VideoQuality = float.Parse(value); break; case "VideoQualityType": // The Type of Quality Mode used qualityMode = value; break; case "VideoTargetSize": queryParsed.VideoTargetSize = value; break; case "VideoTurboTwoPass": queryParsed.TurboFirstPass = value == "1"; break; case "VideoTwoPass": queryParsed.TwoPass = value == "1"; break; // Chapter Markers Tab case "ChapterMarkers": queryParsed.ChapterMarkers = value == "1"; break; // Advanced x264 tab case "x264Option": queryParsed.H264Query = value; break; // Preset Information case "PresetBuildNumber": queryParsed.PresetBuildNumber = int.Parse(value); break; case "PresetDescription": queryParsed.PresetDescription = value; break; case "PresetName": queryParsed.PresetName = value; break; case "Type": queryParsed.Type = value; break; case "UsesMaxPictureSettings": queryParsed.UsesMaxPictureSettings = value == "1"; break; case "UsesPictureFilters": queryParsed.UsesPictureFilters = value == "1"; break; case "UsesPictureSettings": queryParsed.UsesPictureSettings = value == "1"; break; } } // Kill any Quality values we don't need. switch (qualityMode) { case "0": // FileSize queryParsed.VideoQuality = -1; queryParsed.AverageVideoBitrate = null; break; case "1": // Avg Bitrate queryParsed.VideoQuality = -1; queryParsed.VideoTargetSize = null; break; case "2": // CQ queryParsed.AverageVideoBitrate = null; queryParsed.VideoTargetSize = null; break; } #endregion return queryParsed; }
public DGraphQueryable() : base(QueryParser.CreateDefault(), CreateExecutor()) { }
/// <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)) { if (c.IsFuzzySearch) { var keywords = c.SearchPhrase.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); //var keywords = Regex.Split(c.SearchPhrase, @"\s+"); var searchPhrase = string.Empty; searchPhrase = keywords.Aggregate( searchPhrase, (current, keyword) => current + String.Format("{0}~{1}", keyword.Replace("~", ""), c.FuzzyMinSimilarity)); var parser = new QueryParser(u.Version.LUCENE_30, "__content", analyzer) { DefaultOperator = QueryParser .Operator.AND }; var searchQuery = parser.Parse(searchPhrase); query.Add(searchQuery, Occur.MUST); } else { var parser = new QueryParser(u.Version.LUCENE_30, "__content", analyzer) { DefaultOperator = QueryParser .Operator.AND }; var searchQuery = parser.Parse(c.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; }
public void LuceneObjectsProjectionWithList() { IFullTextSession s = Search.CreateFullTextSession(this.OpenSession()); this.PrepEmployeeIndex(s); s.Clear(); ITransaction tx = s.BeginTransaction(); QueryParser parser = new QueryParser("Dept", new StandardAnalyzer()); Query query = parser.Parse("Dept:Accounting"); IFullTextQuery hibQuery = s.CreateFullTextQuery(query, typeof(Employee)); hibQuery.SetProjection( "Id", "Lastname", "Dept", ProjectionConstants.THIS, ProjectionConstants.SCORE, ProjectionConstants.BOOST, ProjectionConstants.DOCUMENT, ProjectionConstants.ID, ProjectionConstants.DOCUMENT_ID); IList result = hibQuery.List(); Assert.IsNotNull(result); object[] projection = (Object[])result[0]; Assert.IsNotNull(projection); Assert.AreEqual(1001, projection[0], "id incorrect"); Assert.AreEqual("Jackson", projection[1], "last name incorrect"); Assert.AreEqual("Accounting", projection[2], "dept incorrect"); Assert.AreEqual("Jackson", ((Employee)projection[3]).Lastname, "THIS incorrect"); Assert.AreEqual(projection[3], s.Get <Employee>(projection[0]), "THIS incorrect"); Assert.AreEqual(1.0F, projection[4], "SCORE incorrect"); Assert.AreEqual(1.0F, projection[5], "BOOST incorrect"); Assert.IsTrue(projection[6] is Document, "DOCUMENT incorrect"); Assert.AreEqual(4, ((Document)projection[6]).GetFields().Count, "DOCUMENT size incorrect"); Assert.AreEqual(1001, projection[7], "ID incorrect"); Assert.IsNotNull(projection[8], "Lucene internal doc id"); // Change the projection order and null one hibQuery.SetProjection( ProjectionConstants.DOCUMENT, ProjectionConstants.THIS, ProjectionConstants.SCORE, null, ProjectionConstants.ID, "Id", "Lastname", "Dept", ProjectionConstants.DOCUMENT_ID); result = hibQuery.List(); Assert.IsNotNull(result); projection = (object[])result[0]; Assert.IsNotNull(projection); Assert.IsTrue(projection[0] is Document, "DOCUMENT incorrect"); Assert.AreEqual(4, ((Document)projection[0]).GetFields().Count, "DOCUMENT size incorrect"); Assert.AreEqual(projection[1], s.Get <Employee>(projection[4]), "THIS incorrect"); Assert.AreEqual(1.0F, projection[2], "SCORE incorrect"); Assert.IsNull(projection[3], "BOOST not removed"); Assert.AreEqual(1001, projection[4], "ID incorrect"); Assert.AreEqual(1001, projection[5], "id incorrect"); Assert.AreEqual("Jackson", projection[6], "last name incorrect"); Assert.AreEqual("Accounting", projection[7], "dept incorrect"); Assert.IsNotNull(projection[8], "Lucene internal doc id"); // cleanup s.Delete("from System.Object"); tx.Commit(); s.Close(); }
public static Query CreateQuery(string sField, string sSearchCriteria, bool bEscapeCharecters = true) { QueryParser oParser = new QueryParser(LuceneVersion, sField, new StandardAnalyzer(LuceneVersion)); return(oParser.Parse(bEscapeCharecters ? QueryParser.Escape(sSearchCriteria) : sSearchCriteria)); }
/// <summary> /// Main searching method /// </summary> /// <param name="lookQuery"></param> /// <returns>an IEnumerableWithTotal</returns> public static IEnumerableWithTotal <LookMatch> Query(LookQuery lookQuery) { IEnumerableWithTotal <LookMatch> lookMatches = null; // prepare return value if (lookQuery == null) { LogHelper.Warn(typeof(LookService), "Supplied search query was null"); } else { var searchProvider = LookService.Searcher; var searchCriteria = searchProvider.CreateSearchCriteria(); var query = searchCriteria.Field(string.Empty, string.Empty); // Text if (!string.IsNullOrWhiteSpace(lookQuery.TextQuery.SearchText)) { if (lookQuery.TextQuery.Fuzzyness > 0) { query.And().Field(LookService.TextField, lookQuery.TextQuery.SearchText.Fuzzy(lookQuery.TextQuery.Fuzzyness)); } else { query.And().Field(LookService.TextField, lookQuery.TextQuery.SearchText); } } // Tags if (lookQuery.TagQuery != null) { var allTags = new List <string>(); var anyTags = new List <string>(); if (lookQuery.TagQuery.AllTags != null) { allTags.AddRange(lookQuery.TagQuery.AllTags); allTags.RemoveAll(x => string.IsNullOrWhiteSpace(x)); } if (lookQuery.TagQuery.AnyTags != null) { anyTags.AddRange(lookQuery.TagQuery.AnyTags); anyTags.RemoveAll(x => string.IsNullOrWhiteSpace(x)); } if (allTags.Any()) { query.And().GroupedAnd(allTags.Select(x => LookService.TagsField), allTags.ToArray()); } if (anyTags.Any()) { query.And().GroupedOr(allTags.Select(x => LookService.TagsField), anyTags.ToArray()); } } // TODO: Date // TODO: Name // Nodes if (lookQuery.NodeQuery != null) { if (lookQuery.NodeQuery.TypeAliases != null) { var typeAliases = new List <string>(); typeAliases.AddRange(lookQuery.NodeQuery.TypeAliases); typeAliases.RemoveAll(x => string.IsNullOrWhiteSpace(x)); if (typeAliases.Any()) { query.And().GroupedOr(typeAliases.Select(x => UmbracoContentIndexer.NodeTypeAliasFieldName), typeAliases.ToArray()); } } if (lookQuery.NodeQuery.ExcludeIds != null) { foreach (var excudeId in lookQuery.NodeQuery.ExcludeIds.Distinct()) { query.Not().Id(excudeId); } } } try { searchCriteria = query.Compile(); } catch (Exception exception) { LogHelper.WarnWithException(typeof(LookService), "Could not compile the Examine query", exception); } if (searchCriteria != null && searchCriteria is LuceneSearchCriteria) { Sort sort = null; Filter filter = null; Func <int, double?> getDistance = x => null; Func <string, IHtmlString> getHighlight = null; TopDocs topDocs = null; switch (lookQuery.SortOn) { case SortOn.Date: // newest -> oldest sort = new Sort(new SortField(LuceneIndexer.SortedFieldNamePrefix + LookService.DateField, SortField.LONG, true)); break; case SortOn.Name: // a -> z sort = new Sort(new SortField(LuceneIndexer.SortedFieldNamePrefix + LookService.NameField, SortField.STRING)); break; } if (lookQuery.LocationQuery != null && lookQuery.LocationQuery.Location != null) { double maxDistance = LookService.MaxDistance; if (lookQuery.LocationQuery.MaxDistance != null) { maxDistance = Math.Min(lookQuery.LocationQuery.MaxDistance.GetMiles(), maxDistance); } var distanceQueryBuilder = new DistanceQueryBuilder( lookQuery.LocationQuery.Location.Latitude, lookQuery.LocationQuery.Location.Longitude, maxDistance, LookService.LocationField + "_Latitude", LookService.LocationField + "_Longitude", CartesianTierPlotter.DefaltFieldPrefix, true); // update filter filter = distanceQueryBuilder.Filter; if (lookQuery.SortOn == SortOn.Distance) { // update sort sort = new Sort( new SortField( LookService.DistanceField, new DistanceFieldComparatorSource(distanceQueryBuilder.DistanceFilter))); } // raw data for the getDistance func var distances = distanceQueryBuilder.DistanceFilter.Distances; // update getDistance func getDistance = new Func <int, double?>(x => { if (distances.ContainsKey(x)) { return(distances[x]); } return(null); }); } var indexSearcher = new IndexSearcher(((LuceneIndexer)LookService.Indexer).GetLuceneDirectory(), false); var luceneSearchCriteria = (LuceneSearchCriteria)searchCriteria; // Do the Lucene search topDocs = indexSearcher.Search( luceneSearchCriteria.Query, // the query build by Examine filter ?? new QueryWrapperFilter(luceneSearchCriteria.Query), LookService.MaxLuceneResults, sort ?? new Sort(SortField.FIELD_SCORE)); if (topDocs.TotalHits > 0) { // setup the highlighing func if required if (lookQuery.TextQuery.HighlightFragments > 0 && !string.IsNullOrWhiteSpace(lookQuery.TextQuery.SearchText)) { var version = Lucene.Net.Util.Version.LUCENE_29; Analyzer analyzer = new StandardAnalyzer(version); var queryParser = new QueryParser(version, LookService.TextField, analyzer); var queryScorer = new QueryScorer(queryParser .Parse(lookQuery.TextQuery.SearchText) .Rewrite(indexSearcher.GetIndexReader())); Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter("<strong>", "</strong>"), queryScorer); // update the func so it does real highlighting work getHighlight = (x) => { var tokenStream = analyzer.TokenStream(LookService.TextField, new StringReader(x)); var highlight = highlighter.GetBestFragments( tokenStream, x, lookQuery.TextQuery.HighlightFragments, // max number of fragments lookQuery.TextQuery.HighlightSeparator); // fragment separator return(new HtmlString(highlight)); }; } lookMatches = new EnumerableWithTotal <LookMatch>( LookSearchService.GetLookMatches( lookQuery, indexSearcher, topDocs, getHighlight, getDistance), topDocs.TotalHits); } } } return(lookMatches ?? new EnumerableWithTotal <LookMatch>(Enumerable.Empty <LookMatch>(), 0)); }
public void AThirdExampleUsingParseQuery() { EmployeeDataContext db = new EmployeeDataContext(); IDictionary<string, Field<Employee>> fieldDefinitions = new Dictionary<string, Field<Employee>>() { { "id", new IntegerField<Employee>(e => e.id) }, { "lastname", new TextField<Employee>(e => e.LastName) }, { "firstname", new TextField<Employee>(e => e.FirstName) }, { "sex", new TextField<Employee>(e => e.Sex.ToString()) }, { "birthdate", new DateField<Employee>(e => e.BirthDate) }, { "hiredate", new DateField<Employee>(e => e.HireDate) }, { "age", new IntegerField<Employee>(e => (DateTime.Today.Year - e.BirthDate.Year)) } }; QueryParser<Employee> parser = new QueryParser<Employee>(e => fieldDefinitions.ContainsKey(e) ? fieldDefinitions[e] : null); var expr = parser.parse("LastName eq 'Higgins' and age < 25"); var result = db.Employees.Where(expr).Select(e => e.id).Count(); Assert.AreEqual(39, result); }
protected override Query[] PrepareQueries() { Analyzer anlzr = NewAnalyzerTask.CreateAnalyzer(m_config.Get("analyzer", typeof(Lucene.Net.Analysis.Standard.StandardAnalyzer).AssemblyQualifiedName)); string defaultField = m_config.Get("file.query.maker.default.field", DocMaker.BODY_FIELD); QueryParser qp = new QueryParser( #pragma warning disable 612, 618 LuceneVersion.LUCENE_CURRENT, #pragma warning restore 612, 618 defaultField, anlzr); qp.AllowLeadingWildcard = true; List <Query> qq = new List <Query>(); string fileName = m_config.Get("file.query.maker.file", null); if (fileName != null) { FileInfo file = new FileInfo(fileName); TextReader reader = null; // note: we use a decoding reader, so if your queries are screwed up you know if (file.Exists) { reader = IOUtils.GetDecodingReader(file, Encoding.UTF8); } else { //see if we can find it as a resource Stream asStream = typeof(FileBasedQueryMaker).FindAndGetManifestResourceStream(fileName); if (asStream != null) { reader = IOUtils.GetDecodingReader(asStream, Encoding.UTF8); } } if (reader != null) { try { string line = null; int lineNum = 0; while ((line = reader.ReadLine()) != null) { line = line.Trim(); if (line.Length != 0 && !line.StartsWith("#", StringComparison.Ordinal)) { try { qq.Add(qp.Parse(line)); } catch (Lucene.Net.QueryParsers.Classic.ParseException e) { Console.Error.WriteLine("Exception: " + e.Message + " occurred while parsing line: " + lineNum + " Text: " + line); } } lineNum++; } } finally { reader.Dispose(); } } else { Console.Error.WriteLine("No Reader available for: " + fileName); } } return(qq.ToArray()); }