public override Query Rewrite(Monodoc.Lucene.Net.Index.IndexReader reader) { BooleanQuery query = new BooleanQuery(); TermEnum enumerator = reader.Terms(prefix); try { System.String prefixText = prefix.Text(); System.String prefixField = prefix.Field(); do { Term term = enumerator.Term(); if (term != null && term.Text().StartsWith(prefixText) && (System.Object)term.Field() == (System.Object)prefixField) { TermQuery tq = new TermQuery(term); // found a match tq.SetBoost(GetBoost()); // set the boost query.Add(tq, false, false); // add to query //System.out.println("added " + term); } else { break; } }while (enumerator.Next()); } finally { enumerator.Close(); } return(query); }
/// <summary> Adds a term to the end of the query phrase. /// The relative position of the term within the phrase is specified explicitly. /// This allows e.g. phrases with more than one term at the same position /// or phrases with gaps (e.g. in connection with stopwords). /// /// </summary> /// <param name="">term /// </param> /// <param name="">position /// </param> public virtual void Add(Term term, int position) { if (terms.Count == 0) { field = term.Field(); } else if ((System.Object)term.Field() != (System.Object)field) { throw new System.ArgumentException("All phrase terms must be in the same field: " + term); } terms.Add(term); positions.Add((System.Int32)position); }
/// <summary>The pattern used to detect integer values in a Field </summary> /// <summary>removed for java 1.3 compatibility /// protected static final Pattern pIntegers = Pattern.compile ("[0-9\\-]+"); /// /// </summary> /// <summary>The pattern used to detect float values in a Field </summary> /// <summary> removed for java 1.3 compatibility /// protected static final Object pFloats = Pattern.compile ("[0-9+\\-\\.eEfFdD]+"); /// </summary> // inherit javadocs public virtual System.Object GetAuto(Monodoc.Lucene.Net.Index.IndexReader reader, System.String field) { field = String.Intern(field); System.Object ret = Lookup(reader, field, SortField.AUTO); if (ret == null) { TermEnum enumerator = reader.Terms(new Term(field, "")); try { Term term = enumerator.Term(); if (term == null) { throw new System.SystemException("no terms in Field " + field + " - cannot determine sort type"); } if ((System.Object)term.Field() == (System.Object)field) { System.String termtext = term.Text().Trim(); /// <summary> Java 1.4 level code: /// if (pIntegers.matcher(termtext).matches()) /// return IntegerSortedHitQueue.comparator (reader, enumerator, Field); /// else if (pFloats.matcher(termtext).matches()) /// return FloatSortedHitQueue.comparator (reader, enumerator, Field); /// </summary> // Java 1.3 level code: try { System.Int32.Parse(termtext); ret = GetInts(reader, field); } catch (System.FormatException nfe1) { try { System.Single.Parse(termtext); ret = GetFloats(reader, field); } catch (System.FormatException nfe2) { ret = GetStringIndex(reader, field); } } if (ret != null) { Store(reader, field, SortField.AUTO, ret); } } else { throw new System.SystemException("Field \"" + field + "\" does not appear to be indexed"); } } finally { enumerator.Close(); } } return(ret); }
/// <summary> Constructor for enumeration of all terms from specified <code>reader</code> which share a prefix of /// length <code>prefixLength</code> with <code>term</code> and which have a fuzzy similarity > /// <code>minSimilarity</code>. /// /// </summary> /// <param name="reader">Delivers terms. /// </param> /// <param name="term">Pattern term. /// </param> /// <param name="minSimilarity">Minimum required similarity for terms from the reader. Default value is 0.5f. /// </param> /// <param name="prefixLength">Length of required common prefix. Default value is 0. /// </param> /// <throws> IOException </throws> public FuzzyTermEnum(Monodoc.Lucene.Net.Index.IndexReader reader, Term term, float minSimilarity, int prefixLength) : base() { InitBlock(); minimumSimilarity = minSimilarity; scale_factor = 1.0f / (1.0f - minimumSimilarity); searchTerm = term; field = searchTerm.Field(); text = searchTerm.Text(); textlen = text.Length; if (prefixLength > 0 && prefixLength < textlen) { this.prefixLength = prefixLength; prefix = text.Substring(0, (prefixLength) - (0)); text = text.Substring(prefixLength); textlen = text.Length; } SetEnum(reader.Terms(new Term(searchTerm.Field(), prefix))); }
/// <summary>Prints a user-readable version of this query. </summary> public override System.String ToString(System.String field) { System.Text.StringBuilder buffer = new System.Text.StringBuilder(); if (!term.Field().Equals(field)) { buffer.Append(term.Field()); buffer.Append(":"); } buffer.Append(term.Text()); if (GetBoost() != 1.0f) { System.Globalization.NumberFormatInfo nfi = new System.Globalization.CultureInfo("en-US", false).NumberFormat; nfi.NumberDecimalDigits = 1; buffer.Append("^"); buffer.Append(GetBoost().ToString("N", nfi)); } return(buffer.ToString()); }
/// <summary> Adds a term to the end of the query phrase. /// The relative position of the term within the phrase is specified explicitly. /// This allows e.g. phrases with more than one term at the same position /// or phrases with gaps (e.g. in connection with stopwords). /// /// </summary> /// <param name="">term /// </param> /// <param name="">position /// </param> public virtual void Add(Term term, int position) { if (terms.Count == 0) field = term.Field(); else if ((System.Object) term.Field() != (System.Object) field) { throw new System.ArgumentException("All phrase terms must be in the same field: " + term); } terms.Add(term); positions.Add((System.Int32) position); }
/// <summary> FIXME: Describe <code>rewrite</code> method here. /// /// </summary> /// <param name="reader">an <code>Monodoc.Lucene.Net.Index.IndexReader</code> value /// </param> /// <returns> a <code>Query</code> value /// </returns> /// <exception cref=""> IOException if an error occurs /// </exception> public override Query Rewrite(Monodoc.Lucene.Net.Index.IndexReader reader) { BooleanQuery query = new BooleanQuery(); TermEnum enumerator = reader.Terms(lowerTerm); try { bool checkLower = false; if (!inclusive) { // make adjustments to set to exclusive checkLower = true; } System.String testField = GetField(); do { Term term = enumerator.Term(); if (term != null && (System.Object)term.Field() == (System.Object)testField) { if (!checkLower || String.CompareOrdinal(term.Text(), lowerTerm.Text()) > 0) { checkLower = false; if (upperTerm != null) { int compare = String.CompareOrdinal(upperTerm.Text(), term.Text()); /* if beyond the upper term, or is exclusive and * this is equal to the upper term, break out */ if ((compare < 0) || (!inclusive && compare == 0)) { break; } } TermQuery tq = new TermQuery(term); // found a match tq.SetBoost(GetBoost()); // set the boost query.Add(tq, false, false); // add to query } } else { break; } }while (enumerator.Next()); } finally { enumerator.Close(); } return(query); }
protected internal override bool TermCompare(Term term) { if ((System.Object) field == (System.Object) term.Field()) { System.String searchText = term.Text(); if (searchText.StartsWith(pre)) { return WildcardEquals(text, 0, searchText, preLen); } } endEnum = true; return false; }
protected internal override bool TermCompare(Term term) { if ((System.Object)field == (System.Object)term.Field()) { System.String searchText = term.Text(); if (searchText.StartsWith(pre)) { return(WildcardEquals(text, 0, searchText, preLen)); } } endEnum = true; return(false); }
// inherit javadocs public virtual float[] GetFloats(Monodoc.Lucene.Net.Index.IndexReader reader, System.String field) { field = String.Intern(field); System.Object ret = Lookup(reader, field, SortField.FLOAT); if (ret == null) { float[] retArray = new float[reader.MaxDoc()]; if (retArray.Length > 0) { TermDocs termDocs = reader.TermDocs(); TermEnum termEnum = reader.Terms(new Term(field, "")); try { if (termEnum.Term() == null) { throw new System.SystemException("no terms in Field " + field); } do { Term term = termEnum.Term(); if ((System.Object)term.Field() != (System.Object)field) { break; } float termval; try { termval = SupportClass.Single.Parse(term.Text()); } catch (Exception e) { termval = 0; } termDocs.Seek(termEnum); while (termDocs.Next()) { retArray[termDocs.Doc()] = termval; } }while (termEnum.Next()); } finally { termDocs.Close(); termEnum.Close(); } } Store(reader, field, SortField.FLOAT, retArray); return(retArray); } return((float[])ret); }
/// <summary>The termCompare method in FuzzyTermEnum uses Levenshtein distance to /// calculate the distance between the given term and the comparing term. /// </summary> protected internal override bool TermCompare(Term term) { System.String termText = term.Text(); if ((System.Object)field == (System.Object)term.Field() && termText.StartsWith(prefix)) { System.String target = termText.Substring(prefixLength); int targetlen = target.Length; int dist = EditDistance(text, target, textlen, targetlen); distance = 1 - ((double)dist / (double)System.Math.Min(textlen, targetlen)); return(distance > minimumSimilarity); } endEnum = true; return(false); }
/// <summary> Creates a new <code>WildcardTermEnum</code>. Passing in a /// {@link Monodoc.Lucene.Net.Index.Term Term} that does not contain a /// <code>WILDCARD_CHAR</code> will cause an exception to be thrown. /// </summary> public WildcardTermEnum(Monodoc.Lucene.Net.Index.IndexReader reader, Term term):base() { searchTerm = term; field = searchTerm.Field(); text = searchTerm.Text(); int sidx = text.IndexOf((System.Char) WILDCARD_STRING); int cidx = text.IndexOf((System.Char) WILDCARD_CHAR); int idx = sidx; if (idx == - 1) { idx = cidx; } else if (cidx >= 0) { idx = System.Math.Min(idx, cidx); } pre = searchTerm.Text().Substring(0, (idx) - (0)); preLen = pre.Length; text = text.Substring(preLen); SetEnum(reader.Terms(new Term(searchTerm.Field(), pre))); }
/// <summary> Creates a new <code>WildcardTermEnum</code>. Passing in a /// {@link Monodoc.Lucene.Net.Index.Term Term} that does not contain a /// <code>WILDCARD_CHAR</code> will cause an exception to be thrown. /// </summary> public WildcardTermEnum(Monodoc.Lucene.Net.Index.IndexReader reader, Term term) : base() { searchTerm = term; field = searchTerm.Field(); text = searchTerm.Text(); int sidx = text.IndexOf((System.Char)WILDCARD_STRING); int cidx = text.IndexOf((System.Char)WILDCARD_CHAR); int idx = sidx; if (idx == -1) { idx = cidx; } else if (cidx >= 0) { idx = System.Math.Min(idx, cidx); } pre = searchTerm.Text().Substring(0, (idx) - (0)); preLen = pre.Length; text = text.Substring(preLen); SetEnum(reader.Terms(new Term(searchTerm.Field(), pre))); }
/// <summary>Constructs a query selecting all terms greater than /// <code>lowerTerm</code> but less than <code>upperTerm</code>. /// There must be at least one term and either term may be null, /// in which case there is no bound on that side, but if there are /// two terms, both terms <b>must</b> be for the same Field. /// </summary> public RangeQuery(Term lowerTerm, Term upperTerm, bool inclusive) { if (lowerTerm == null && upperTerm == null) { throw new System.ArgumentException("At least one term must be non-null"); } if (lowerTerm != null && upperTerm != null && (System.Object) lowerTerm.Field() != (System.Object) upperTerm.Field()) { throw new System.ArgumentException("Both terms must be for the same Field"); } // if we have a lowerTerm, start there. otherwise, start at beginning if (lowerTerm != null) { this.lowerTerm = lowerTerm; } else { this.lowerTerm = new Term(upperTerm.Field(), ""); } this.upperTerm = upperTerm; this.inclusive = inclusive; }
// inherit javadocs public virtual System.IComparable[] GetCustom(Monodoc.Lucene.Net.Index.IndexReader reader, System.String field, SortComparator comparator) { field = String.Intern(field); System.Object ret = Lookup(reader, field, comparator); if (ret == null) { System.IComparable[] retArray = new System.IComparable[reader.MaxDoc()]; if (retArray.Length > 0) { TermDocs termDocs = reader.TermDocs(); TermEnum termEnum = reader.Terms(new Term(field, "")); try { if (termEnum.Term() == null) { throw new System.SystemException("no terms in Field " + field); } do { Term term = termEnum.Term(); if ((System.Object)term.Field() != (System.Object)field) { break; } System.IComparable termval = comparator.GetComparable(term.Text()); termDocs.Seek(termEnum); while (termDocs.Next()) { retArray[termDocs.Doc()] = termval; } }while (termEnum.Next()); } finally { termDocs.Close(); termEnum.Close(); } } Store(reader, field, SortField.CUSTOM, retArray); return(retArray); } return((System.IComparable[])ret); }
/// <summary>Constructs a query selecting all terms greater than /// <code>lowerTerm</code> but less than <code>upperTerm</code>. /// There must be at least one term and either term may be null, /// in which case there is no bound on that side, but if there are /// two terms, both terms <b>must</b> be for the same Field. /// </summary> public RangeQuery(Term lowerTerm, Term upperTerm, bool inclusive) { if (lowerTerm == null && upperTerm == null) { throw new System.ArgumentException("At least one term must be non-null"); } if (lowerTerm != null && upperTerm != null && (System.Object)lowerTerm.Field() != (System.Object)upperTerm.Field()) { throw new System.ArgumentException("Both terms must be for the same Field"); } // if we have a lowerTerm, start there. otherwise, start at beginning if (lowerTerm != null) { this.lowerTerm = lowerTerm; } else { this.lowerTerm = new Term(upperTerm.Field(), ""); } this.upperTerm = upperTerm; this.inclusive = inclusive; }
/// <summary> Constructor for enumeration of all terms from specified <code>reader</code> which share a prefix of /// length <code>prefixLength</code> with <code>term</code> and which have a fuzzy similarity > /// <code>minSimilarity</code>. /// /// </summary> /// <param name="reader">Delivers terms. /// </param> /// <param name="term">Pattern term. /// </param> /// <param name="minSimilarity">Minimum required similarity for terms from the reader. Default value is 0.5f. /// </param> /// <param name="prefixLength">Length of required common prefix. Default value is 0. /// </param> /// <throws> IOException </throws> public FuzzyTermEnum(Monodoc.Lucene.Net.Index.IndexReader reader, Term term, float minSimilarity, int prefixLength):base() { InitBlock(); minimumSimilarity = minSimilarity; scale_factor = 1.0f / (1.0f - minimumSimilarity); searchTerm = term; field = searchTerm.Field(); text = searchTerm.Text(); textlen = text.Length; if (prefixLength > 0 && prefixLength < textlen) { this.prefixLength = prefixLength; prefix = text.Substring(0, (prefixLength) - (0)); text = text.Substring(prefixLength); textlen = text.Length; } SetEnum(reader.Terms(new Term(searchTerm.Field(), prefix))); }
/// <summary>The termCompare method in FuzzyTermEnum uses Levenshtein distance to /// calculate the distance between the given term and the comparing term. /// </summary> protected internal override bool TermCompare(Term term) { System.String termText = term.Text(); if ((System.Object) field == (System.Object) term.Field() && termText.StartsWith(prefix)) { System.String target = termText.Substring(prefixLength); int targetlen = target.Length; int dist = EditDistance(text, target, textlen, targetlen); distance = 1 - ((double) dist / (double) System.Math.Min(textlen, targetlen)); return (distance > minimumSimilarity); } endEnum = true; return false; }
/// <summary>Returns the Field name for this query </summary> public virtual System.String GetField() { return(lowerTerm != null?lowerTerm.Field():upperTerm.Field()); }
public override System.String GetField() { return(term.Field()); }
// inherit javadocs public virtual StringIndex GetStringIndex(Monodoc.Lucene.Net.Index.IndexReader reader, System.String field) { field = String.Intern(field); System.Object ret = Lookup(reader, field, Monodoc.Lucene.Net.Search.FieldCache_Fields.STRING_INDEX); if (ret == null) { int[] retArray = new int[reader.MaxDoc()]; System.String[] mterms = new System.String[reader.MaxDoc() + 1]; if (retArray.Length > 0) { TermDocs termDocs = reader.TermDocs(); TermEnum termEnum = reader.Terms(new Term(field, "")); int t = 0; // current term number // an entry for documents that have no terms in this Field // should a document with no terms be at top or bottom? // this puts them at the top - if it is changed, FieldDocSortedHitQueue // needs to change as well. mterms[t++] = null; try { if (termEnum.Term() == null) { throw new System.SystemException("no terms in Field " + field); } do { Term term = termEnum.Term(); if ((System.Object)term.Field() != (System.Object)field) { break; } // store term text // we expect that there is at most one term per document if (t >= mterms.Length) { throw new System.SystemException("there are more terms than documents in Field \"" + field + "\""); } mterms[t] = term.Text(); termDocs.Seek(termEnum); while (termDocs.Next()) { retArray[termDocs.Doc()] = t; } t++; }while (termEnum.Next()); } finally { termDocs.Close(); termEnum.Close(); } if (t == 0) { // if there are no terms, make the term array // have a single null entry mterms = new System.String[1]; } else if (t < mterms.Length) { // if there are less terms than documents, // trim off the dead array space System.String[] terms = new System.String[t]; Array.Copy(mterms, 0, terms, 0, t); mterms = terms; } } StringIndex value_Renamed = new StringIndex(retArray, mterms); Store(reader, field, Monodoc.Lucene.Net.Search.FieldCache_Fields.STRING_INDEX, value_Renamed); return(value_Renamed); } return((StringIndex)ret); }