19 using System.Collections.Generic;
22 using Lucene.Net.Index;
23 using Lucene.Net.Util;
25 namespace Lucene.Net.Search.Highlight
32 public static class QueryTermExtractor
42 public static WeightedTerm[] GetTerms(
Query query)
44 return GetTerms(query,
false);
55 public static WeightedTerm[] GetIdfWeightedTerms(
Query query,
IndexReader reader,
string fieldName)
57 WeightedTerm[] terms = GetTerms(query,
false, fieldName);
58 int totalNumDocs = reader.
NumDocs();
59 foreach (WeightedTerm t
in terms)
63 int docFreq = reader.
DocFreq(
new Term(fieldName, t.Term));
65 if (totalNumDocs < docFreq)
67 docFreq = totalNumDocs;
70 var idf = (float)(Math.Log((
float)totalNumDocs / (
double)(docFreq + 1)) + 1.0);
86 public static WeightedTerm[] GetTerms(
Query query,
bool prohibited,
string fieldName)
88 var terms =
new HashSet<WeightedTerm>();
89 if (fieldName != null)
93 GetTerms(query, terms, prohibited, fieldName);
94 return terms.ToArray();
106 public static WeightedTerm[] GetTerms(
Query query,
bool prohibited)
108 return GetTerms(query, prohibited, null);
112 private static void GetTerms(
Query query, HashSet<WeightedTerm> terms,
bool prohibited,
string fieldName)
117 GetTermsFromBooleanQuery((BooleanQuery) query, terms, prohibited, fieldName);
119 GetTermsFromFilteredQuery((FilteredQuery) query, terms, prohibited, fieldName);
122 var nonWeightedTerms = Support.Compatibility.SetFactory.CreateHashSet<
Term>();
123 query.ExtractTerms(nonWeightedTerms);
124 foreach (var term
in nonWeightedTerms)
126 if ((fieldName == null) || (term.Field == fieldName))
128 terms.Add(
new WeightedTerm(query.Boost, term.Text));
133 catch (System.NotSupportedException ignore)
149 private static void GetTermsFromBooleanQuery(BooleanQuery query, HashSet<WeightedTerm> terms,
bool prohibited,
string fieldName)
152 for (
int i = 0; i < queryClauses.Length; i++)
154 if (prohibited || queryClauses[i].
Occur !=
Occur.MUST_NOT)
155 GetTerms(queryClauses[i].
Query, terms, prohibited, fieldName);
158 private static void GetTermsFromFilteredQuery(FilteredQuery query, HashSet<WeightedTerm> terms,
bool prohibited,
string fieldName)
160 GetTerms(query.Query, terms, prohibited, fieldName);