/* * Check if src and dest have overlapped part and if it is, create PhraseQueries and add expandQueries. * * ex1) src="a b", dest="c d" => no overlap * ex2) src="a b", dest="a b c" => no overlap * ex3) src="a b", dest="b c" => overlap; expandQueries={"a b c"} * ex4) src="a b c", dest="b c d" => overlap; expandQueries={"a b c d"} * ex5) src="a b c", dest="b c" => no overlap * ex6) src="a b c", dest="b" => no overlap * ex7) src="a a a a", dest="a a a" => overlap; * expandQueries={"a a a a a","a a a a a a"} * ex8) src="a b c d", dest="b c" => no overlap */ private void CheckOverlap(Dictionary<Query,Query> expandQueries, Term[] src, Term[] dest, int slop, float boost) { // beginning from 1 (not 0) is safe because that the PhraseQuery has multiple terms // is guaranteed in flatten() method (if PhraseQuery has only one term, flatten() // converts PhraseQuery to TermQuery) for (int i = 1; i < src.Length; i++) { bool overlap = true; for (int j = i; j < src.Length; j++) { if ((j - i) < dest.Length && !src[j].Text().Equals(dest[j - i].Text())) { overlap = false; break; } } if (overlap && src.Length - i < dest.Length) { PhraseQuery pq = new PhraseQuery(); foreach (Term srcTerm in src) pq.Add(srcTerm); for (int k = src.Length - i; k < dest.Length; k++) { pq.Add(new Term(src[0].Field(), dest[k].Text())); } pq.SetSlop(slop); pq.SetBoost(boost); if (!expandQueries.ContainsKey(pq)) expandQueries.Add(pq,pq); } } }
/// <summary> /// Adds a standard type clause to this instance /// </summary> /// <param name="term">Term to add to this query.</param> /// <param name="occurrence">Defines how the term is added to this query.</param> /// <param name="slop">The amount of allowed slop in a phrase query.</param> /// <remarks> /// Slop is the amount of movement each word is allowed in a non-exact phrase query. /// For instance if you search for "Adobe Systems Incorporated" and the slop is set to 0 then /// only results with that term is allowed. If you set the slop to 2 then two movements can be /// made, max, for each word. In the same example with slop set to 2 results would be returned /// for "Adobe Systems Incorporated", "Adobe Incorporated Systems", "Systems Adobe Incorporated", /// and "Systems Incorporated Adobe". /// </remarks> public void AddBooleanClause(SearchTerm term, ClauseOccurrence occurrence, int slop) { if (term == null) { throw new ArgumentNullException("term", "term cannot be null"); } IncrementTotalClauses(1); if (term.IsPhrase) { PhraseQuery phraseQuery = new PhraseQuery(); phraseQuery.Add(term.GetLuceneTerm()); phraseQuery.SetSlop(slop); phraseQuery.SetBoost(term.Boost); this.luceneQuery.Add(phraseQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); phraseQuery = null; } else { TermQuery termQuery = new TermQuery(term.GetLuceneTerm()); termQuery.SetBoost(term.Boost); this.luceneQuery.Add(termQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); termQuery = null; } }
protected Query Pq(float boost, int slop, String field, params String[] texts) { PhraseQuery query = new PhraseQuery(); foreach (String text in texts) { query.Add(new Term(field, text)); } query.SetBoost(boost); query.SetSlop(slop); return(query); }
/// <summary> /// Adds a standard type clause to this instance /// </summary> /// <param name="term">Term to add to this query.</param> /// <param name="occurrence">Defines how the term is added to this query.</param> /// <param name="slop">The amount of allowed slop in a phrase query.</param> /// <remarks> /// Slop is the amount of movement each word is allowed in a non-exact phrase query. /// For instance if you search for "Adobe Systems Incorporated" and the slop is set to 0 then /// only results with that term is allowed. If you set the slop to 2 then two movements can be /// made, max, for each word. In the same example with slop set to 2 results would be returned /// for "Adobe Systems Incorporated", "Adobe Incorporated Systems", "Systems Adobe Incorporated", /// and "Systems Incorporated Adobe". /// </remarks> public void AddBooleanClause(SearchTerm term, ClauseOccurrence occurrence, int slop) { if (term == null) throw new ArgumentNullException("term", "term cannot be null"); IncrementTotalClauses(1); if (term.IsPhrase) { PhraseQuery phraseQuery = new PhraseQuery(); phraseQuery.Add(term.GetLuceneTerm()); phraseQuery.SetSlop(slop); phraseQuery.SetBoost(term.Boost); this.luceneQuery.Add(phraseQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); phraseQuery = null; } else { TermQuery termQuery = new TermQuery(term.GetLuceneTerm()); termQuery.SetBoost(term.Boost); this.luceneQuery.Add(termQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence)); termQuery = null; } }