public static Query BuildQuery(string query, IndexQuery indexQuery, RavenPerFieldAnalyzerWrapper analyzer) { var originalQuery = query; try { var queryParser = new RangeQueryParser(Version.LUCENE_29, indexQuery.DefaultField ?? string.Empty, analyzer) { DefaultOperator = indexQuery.DefaultOperator == QueryOperator.Or ? QueryParser.Operator.OR : QueryParser.Operator.AND, AllowLeadingWildcard = true }; query = PreProcessComments(query); query = PreProcessMixedInclusiveExclusiveRangeQueries(query); query = PreProcessUntokenizedTerms(query, queryParser); query = PreProcessSearchTerms(query); query = PreProcessDateTerms(query, queryParser); var generatedQuery = queryParser.Parse(query); generatedQuery = HandleMethods(generatedQuery, analyzer); return generatedQuery; } catch (ParseException pe) { if (originalQuery == query) throw new ParseException("Could not parse: '" + query + "'", pe); throw new ParseException("Could not parse modified query: '" + query + "' original was: '" + originalQuery + "'", pe); } }
private static Query HandleMethods(Query query, RavenPerFieldAnalyzerWrapper analyzer) { var termQuery = query as TermQuery; if (termQuery != null && termQuery.Term.Field.StartsWith("@")) { return HandleMethodsForQueryAndTerm(query, termQuery.Term); } var pharseQuery = query as PhraseQuery; if (pharseQuery != null) { var terms = pharseQuery.GetTerms(); if (terms.All(x => x.Field.StartsWith("@")) == false || terms.Select(x => x.Field).Distinct().Count() != 1) return query; return HandleMethodsForQueryAndTerm(query, terms); } var wildcardQuery = query as WildcardQuery; if (wildcardQuery != null) { return HandleMethodsForQueryAndTerm(query, wildcardQuery.Term); } var booleanQuery = query as BooleanQuery; if (booleanQuery != null) { foreach (var c in booleanQuery.Clauses) { c.Query = HandleMethods(c.Query, analyzer); } if (booleanQuery.Clauses.Count == 0) return booleanQuery; //merge only clauses that have "OR" operator between them var mergeGroups = booleanQuery.Clauses.Where(clause => clause.Occur == Occur.SHOULD) .Select(x=>x.Query) .OfType<IRavenLuceneMethodQuery>() .GroupBy(x => x.Field) .ToArray(); if (mergeGroups.Length == 0) return booleanQuery; foreach (var mergeGroup in mergeGroups) { var clauses = mergeGroup.ToArray(); var first = clauses[0]; foreach (var mergedClause in clauses.Skip(1)) { booleanQuery.Clauses.RemoveAll(x => ReferenceEquals(x.Query, mergedClause)); } var ravenLuceneMethodQuery = clauses.Skip(1).Aggregate(first, (methodQuery, clause) => methodQuery.Merge(clause)); booleanQuery.Clauses.First(x => ReferenceEquals(x.Query, first)).Query = (Query)ravenLuceneMethodQuery; } if (booleanQuery.Clauses.Count == 1) return booleanQuery.Clauses[0].Query; return booleanQuery; } return query; }
public static Query BuildQuery(string query, RavenPerFieldAnalyzerWrapper analyzer) { return(BuildQuery(query, new IndexQuery(), analyzer)); }
private static Query HandleMethods(Query query, RavenPerFieldAnalyzerWrapper analyzer) { var termQuery = query as TermQuery; if (termQuery != null && termQuery.Term.Field.StartsWith("@")) { return(HandleMethodsForQueryAndTerm(query, termQuery.Term)); } var pharseQuery = query as PhraseQuery; if (pharseQuery != null) { var terms = pharseQuery.GetTerms(); if (terms.All(x => x.Field.StartsWith("@")) == false || terms.Select(x => x.Field).Distinct().Count() != 1) { return(query); } return(HandleMethodsForQueryAndTerm(query, terms)); } var wildcardQuery = query as WildcardQuery; if (wildcardQuery != null) { return(HandleMethodsForQueryAndTerm(query, wildcardQuery.Term)); } var booleanQuery = query as BooleanQuery; if (booleanQuery != null) { foreach (var c in booleanQuery.Clauses) { c.Query = HandleMethods(c.Query, analyzer); } if (booleanQuery.Clauses.Count == 0) { return(booleanQuery); } //merge only clauses that have "OR" operator between them var mergeGroups = booleanQuery.Clauses.Where(clause => clause.Occur == Occur.SHOULD) .Select(x => x.Query) .OfType <IRavenLuceneMethodQuery>() .GroupBy(x => x.Field) .ToArray(); if (mergeGroups.Length == 0) { return(booleanQuery); } foreach (var mergeGroup in mergeGroups) { var clauses = mergeGroup.ToArray(); var first = clauses[0]; foreach (var mergedClause in clauses.Skip(1)) { booleanQuery.Clauses.RemoveAll(x => ReferenceEquals(x.Query, mergedClause)); } var ravenLuceneMethodQuery = clauses.Skip(1).Aggregate(first, (methodQuery, clause) => methodQuery.Merge(clause)); booleanQuery.Clauses.First(x => ReferenceEquals(x.Query, first)).Query = (Query)ravenLuceneMethodQuery; } if (booleanQuery.Clauses.Count == 1) { return(booleanQuery.Clauses[0].Query); } return(booleanQuery); } return(query); }
public RavenPerFieldAnalyzerWrapper CreateAnalyzer(Analyzer defaultAnalyzer, ICollection<Action> toDispose, bool forQuerying = false) { toDispose.Add(defaultAnalyzer.Close); string value; if (indexDefinition.Analyzers.TryGetValue(Constants.AllFields, out value)) { defaultAnalyzer = IndexingExtensions.CreateAnalyzerInstance(Constants.AllFields, value); toDispose.Add(defaultAnalyzer.Close); } var perFieldAnalyzerWrapper = new RavenPerFieldAnalyzerWrapper(defaultAnalyzer); foreach (var analyzer in indexDefinition.Analyzers) { Analyzer analyzerInstance = IndexingExtensions.CreateAnalyzerInstance(analyzer.Key, analyzer.Value); toDispose.Add(analyzerInstance.Close); if (forQuerying) { var customAttributes = analyzerInstance.GetType().GetCustomAttributes(typeof(NotForQueryingAttribute), false); if (customAttributes.Length > 0) continue; } perFieldAnalyzerWrapper.AddAnalyzer(analyzer.Key, analyzerInstance); } StandardAnalyzer standardAnalyzer = null; KeywordAnalyzer keywordAnalyzer = null; foreach (var fieldIndexing in indexDefinition.Indexes) { switch (fieldIndexing.Value) { case FieldIndexing.NotAnalyzed: if (keywordAnalyzer == null) { keywordAnalyzer = new KeywordAnalyzer(); toDispose.Add(keywordAnalyzer.Close); } perFieldAnalyzerWrapper.AddAnalyzer(fieldIndexing.Key, keywordAnalyzer); break; case FieldIndexing.Analyzed: if (indexDefinition.Analyzers.ContainsKey(fieldIndexing.Key)) continue; if (standardAnalyzer == null) { standardAnalyzer = new StandardAnalyzer(Version.LUCENE_29); toDispose.Add(standardAnalyzer.Close); } perFieldAnalyzerWrapper.AddAnalyzer(fieldIndexing.Key, standardAnalyzer); break; } } return perFieldAnalyzerWrapper; }
private static void DisposeAnalyzerAndFriends(List<Action> toDispose, RavenPerFieldAnalyzerWrapper analyzer) { if (analyzer != null) analyzer.Close(); foreach (Action dispose in toDispose) { dispose(); } toDispose.Clear(); }
public static Query BuildQuery(string query, RavenPerFieldAnalyzerWrapper analyzer) { return BuildQuery(query, new IndexQuery(), analyzer); }
private static Query Parse(string q) { using (var defaultAnalyzer = new KeywordAnalyzer()) using (var perFieldAnalyzerWrapper = new RavenPerFieldAnalyzerWrapper(defaultAnalyzer)) return QueryBuilder.BuildQuery(q, perFieldAnalyzerWrapper); }
public SimpleFilesQueryParser(Analyzer analyzer) : base(Version.LUCENE_29, string.Empty, analyzer) { fieldAnalyzer = new RavenPerFieldAnalyzerWrapper(fieldAnalyzer); }