Beispiel #1
0
        public virtual void TestNoSuchMultiTermsInOr()
        {
            //test to make sure non existent multiterms aren't throwing null pointer exceptions
            FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
            SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(fuzzyNoSuch);
            SpanQuery term = new SpanTermQuery(new Term("field", "brown"));
            SpanOrQuery near = new SpanOrQuery(new SpanQuery[] { term, spanNoSuch });
            Assert.AreEqual(1, searcher.Search(near, 10).TotalHits);

            //flip
            near = new SpanOrQuery(new SpanQuery[] { spanNoSuch, term });
            Assert.AreEqual(1, searcher.Search(near, 10).TotalHits);

            WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
            SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(wcNoSuch);
            near = new SpanOrQuery(new SpanQuery[] { term, spanWCNoSuch });
            Assert.AreEqual(1, searcher.Search(near, 10).TotalHits);

            RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
            SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(rgxNoSuch);
            near = new SpanOrQuery(new SpanQuery[] { term, spanRgxNoSuch });
            Assert.AreEqual(1, searcher.Search(near, 10).TotalHits);

            PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
            SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(prfxNoSuch);
            near = new SpanOrQuery(new SpanQuery[] { term, spanPrfxNoSuch });
            Assert.AreEqual(1, searcher.Search(near, 10).TotalHits);

            near = new SpanOrQuery(new SpanQuery[] { spanPrfxNoSuch });
            Assert.AreEqual(0, searcher.Search(near, 10).TotalHits);

            near = new SpanOrQuery(new SpanQuery[] { spanPrfxNoSuch, spanPrfxNoSuch });
            Assert.AreEqual(0, searcher.Search(near, 10).TotalHits);
        }
Beispiel #2
0
        public Result Search(string term, int count, int start)
        {
            try {
                term = term.ToLower();
                Term  htTerm = new Term("hottext", term);
                Query qq1    = new FuzzyQuery(htTerm);
                Query qq2    = new TermQuery(htTerm);
                qq2.Boost = 10f;
                Query qq3 = new PrefixQuery(htTerm);
                qq3.Boost = 10f;
                DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0f);
                q1.Add(qq1);
                q1.Add(qq2);
                q1.Add(qq3);
                Query q2 = new TermQuery(new Term("text", term));
                q2.Boost = 3f;
                Query q3 = new TermQuery(new Term("examples", term));
                q3.Boost = 3f;
                DisjunctionMaxQuery q = new DisjunctionMaxQuery(0f);

                q.Add(q1);
                q.Add(q2);
                q.Add(q3);

                TopDocs top = SearchInternal(q, count, start);
                Result  r   = new Result(term, searcher, top.ScoreDocs);
                return(r);
            } catch (IOException) {
                Console.WriteLine("No index in {0}", dir);
                return(null);
            }
        }
        /// <summary>
        /// Gets the highlighted wildcard text.
        /// </summary>
        /// <param name="indexField">The index field value.</param>
        /// <param name="searchQuery">The search query.</param>
        /// <param name="highlightField">The highlight field name.</param>
        /// <param name="examineIndexSetName">Name of the examine index set.</param>
        /// <param name="maxNumFragments">Maximum number of fragments to retrieve.</param>
        /// <param name="preTag">Highlight pre tag.</param>
        /// <param name="postTag">Highlight post tag.</param>
        /// <returns></returns>
        public static string GetHighlightWithWildcards(string indexField, string searchQuery, string highlightField, string examineIndexSetName, int maxNumFragments, string preTag, string postTag)
        {
            if (indexField == null)
            {
                throw new ArgumentNullException(nameof(indexField));
            }
            if (searchQuery == null)
            {
                throw new ArgumentNullException(nameof(searchQuery));
            }
            if (highlightField == null)
            {
                throw new ArgumentNullException(nameof(highlightField));
            }
            if (examineIndexSetName == null)
            {
                throw new ArgumentNullException(nameof(examineIndexSetName));
            }

            BooleanQuery finalQuery = new BooleanQuery();

            foreach (string term in searchQuery.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
            {
                FuzzyQuery fuzzyQuery = new FuzzyQuery(new Lucene.Net.Index.Term(highlightField, term), 0.5f, 0);
                finalQuery.Add(new BooleanClause(fuzzyQuery, BooleanClause.Occur.SHOULD));
            }

            string indexFieldStrippedHtmlValue = indexField.StripHtml();
            SimpleHTMLFormatter formatter      = new SimpleHTMLFormatter(preTag, postTag);
            QueryScorer         fragmentScorer = new QueryScorer(finalQuery.Rewrite(GetIndexSearcher(examineIndexSetName).GetIndexReader()));
            Highlighter         highlighter    = new Highlighter(formatter, fragmentScorer);
            TokenStream         tokenStream    = new StandardAnalyzer(Version.LUCENE_29).TokenStream(highlightField, new StringReader(indexFieldStrippedHtmlValue));

            return(highlighter.GetBestFragments(tokenStream, indexFieldStrippedHtmlValue, maxNumFragments, "..."));
        }
Beispiel #4
0
        private IList <string> SearchFuzzy(string text)
        {
            var term  = new Term(CONTENT_FIELD, text);
            var query = new FuzzyQuery(term);

            return(RunQuery(query));
        }
Beispiel #5
0
        public void SearchDoc_Fuzzy(ElasticClient client)
        {
            var fuzzyQuery = new FuzzyQuery
            {
                Name  = "named_query",
                Boost = 1.1,
                Field = Infer.Field <Product>(p => p.Name),
                Value = "test"
            };

            var searchRequest = new SearchRequest <Product>(Indices.Parse("esdemo"), Types.Parse("product"))
            {
                From  = 0,
                Size  = 10,
                Query = fuzzyQuery
            };

            var searchResponse = client.Search <Product>(searchRequest);

            var list  = searchResponse.Documents.ToList();
            var count = searchResponse.Total;

            System.Console.WriteLine("FuzzyQuery");
            System.Console.WriteLine(list.ToJsonString());
            System.Console.WriteLine(count);
        }
        public Product[] Search(int catalogId, string text)
        {
            var result = new List <Product>();

            using (var analyzer = new SimpleAnalyzer(LuceneVersion.LUCENE_48))
                using (var indexDirectory = FSDirectory.Open(GetIndexDirectory()))
                    using (var indexReader = DirectoryReader.Open(indexDirectory))
                    {
                        var query = new FuzzyQuery(new Term("Name", text), 2);

                        var indexSearcher = new IndexSearcher(indexReader);

                        var searchResults = indexSearcher.Search(query, 10).ScoreDocs;

                        foreach (var searchResultItem in searchResults)
                        {
                            var doc = indexSearcher.Doc(searchResultItem.Doc);

                            var product = new Product()
                            {
                                Id   = (int)doc.GetField("Id")?.GetInt32Value(),
                                Name = doc.GetField("Name")?.GetStringValue()
                            };
                            result.Add(product);
                        }

                        return(result.ToArray());
                    }
        }
Beispiel #7
0
        public virtual void TestNoSuchMultiTermsInSpanFirst()
        {
            //this hasn't been a problem
            FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
            SpanQuery  spanNoSuch  = new SpanMultiTermQueryWrapper <MultiTermQuery>(fuzzyNoSuch);
            SpanQuery  spanFirst   = new SpanFirstQuery(spanNoSuch, 10);

            Assert.AreEqual(0, Searcher.Search(spanFirst, 10).TotalHits);

            WildcardQuery wcNoSuch     = new WildcardQuery(new Term("field", "noSuch*"));
            SpanQuery     spanWCNoSuch = new SpanMultiTermQueryWrapper <MultiTermQuery>(wcNoSuch);

            spanFirst = new SpanFirstQuery(spanWCNoSuch, 10);
            Assert.AreEqual(0, Searcher.Search(spanFirst, 10).TotalHits);

            RegexpQuery rgxNoSuch     = new RegexpQuery(new Term("field", "noSuch"));
            SpanQuery   spanRgxNoSuch = new SpanMultiTermQueryWrapper <MultiTermQuery>(rgxNoSuch);

            spanFirst = new SpanFirstQuery(spanRgxNoSuch, 10);
            Assert.AreEqual(0, Searcher.Search(spanFirst, 10).TotalHits);

            PrefixQuery prfxNoSuch     = new PrefixQuery(new Term("field", "noSuch"));
            SpanQuery   spanPrfxNoSuch = new SpanMultiTermQueryWrapper <MultiTermQuery>(prfxNoSuch);

            spanFirst = new SpanFirstQuery(spanPrfxNoSuch, 10);
            Assert.AreEqual(0, Searcher.Search(spanFirst, 10).TotalHits);
        }
Beispiel #8
0
        public string DidYouMean(string pattern)
        {
            try
            {
                IndexSearcher searcher = new IndexSearcher(m_HistoryPath);

                Term       t     = new Term(Constants.SearchedText, pattern);
                FuzzyQuery query = new FuzzyQuery(t);

                Hits hits = searcher.Search(query);

                if (hits.Length() != 0)
                {
                    return(hits.Doc(0).Get(Constants.SearchedText));
                }
                else
                {
                    return("");
                }
            }
            catch (Exception)
            {
                return("");
            }
        }
Beispiel #9
0
        public IEnumerable <string> FindMatches(string searchTerm, int maxDifference)
        {
            if (String.IsNullOrEmpty(searchTerm))
            {
                yield break;
            }
            var dir          = FSDirectory.Open(IndexLocation);
            var searcher     = new IndexSearcher(DirectoryReader.Open(dir));
            var directPhrase = new MultiPhraseQuery();

            foreach (var item in searchTerm.Split(' '))
            {
                directPhrase.Add(new Term("data", item));
            }
            var directHits = searcher.Search(directPhrase, 20 /* top 20 */).ScoreDocs;

            foreach (var hit in directHits)
            {
                yield return(ConvertHits(searcher, hit));
            }
            // don't do expensive fuzzyQuery if results are enough
            if (directHits.Length > 10)
            {
                yield break;
            }

            var phrase = new FuzzyQuery(new Term("data", searchTerm), 2);
            var hits   = searcher.Search(phrase, 20 /* top 20 */).ScoreDocs;

            foreach (var hit in hits)
            {
                yield return(ConvertHits(searcher, hit));
            }
        }
Beispiel #10
0
        protected Query ToWildCardQuery(string query, IEnumerable <LuceneFieldInfo> fields)
        {
            var terms            = query.ToLowerInvariant().Split(new[] { " ,." }, StringSplitOptions.RemoveEmptyEntries);
            var wildCardQuery    = new BooleanQuery();
            var enumeratedFields = fields.ToList();

            foreach (var term in terms)
            {
                var booleanQuery = new BooleanQuery();

                foreach (var field in enumeratedFields)
                {
                    var subQuery = new FuzzyQuery(
                        new Term(field.LuceneFieldAttribute.Name, term),
                        field.LuceneFieldAttribute.Fuzziness)
                    {
                        Boost = field.LuceneFieldAttribute.Boost
                    };

                    booleanQuery.Add(subQuery, Occur.SHOULD);
                }

                wildCardQuery.Add(booleanQuery, Occur.MUST);
            }

            return(wildCardQuery);
        }
Beispiel #11
0
 public virtual void TestFuzzy()
 {
     FuzzyQuery fq = new FuzzyQuery(new Term("field", "broan"));
     SpanQuery sfq = new SpanMultiTermQueryWrapper<MultiTermQuery>(fq);
     // will not match quick brown fox
     SpanPositionRangeQuery sprq = new SpanPositionRangeQuery(sfq, 3, 6);
     Assert.AreEqual(2, searcher.Search(sprq, 10).TotalHits);
 }
 public virtual void TestFuzzy()
 {
     FuzzyQuery fq = new FuzzyQuery(new Term("field", "broan"));
     SpanQuery sfq = new SpanMultiTermQueryWrapper<MultiTermQuery>(fq);
     // will not match quick brown fox
     SpanPositionRangeQuery sprq = new SpanPositionRangeQuery(sfq, 3, 6);
     Assert.AreEqual(2, Searcher.Search(sprq, 10).TotalHits);
 }
        public FilteredQuery CreateFilteredQuery(ILuceneQueryService builder, LuceneQueryContext context, string type, JToken filter, Query toFilter)
        {
            if (type != "fuzzy")
            {
                return(null);
            }

            if (!(toFilter is BooleanQuery booleanQuery))
            {
                return(null);
            }

            var queryObj = filter as JObject;
            var first    = queryObj.Properties().First();

            FuzzyQuery fuzzyQuery;

            switch (first.Value.Type)
            {
            case JTokenType.String:
                fuzzyQuery = new FuzzyQuery(new Term(first.Name, first.Value.ToString()));
                break;

            case JTokenType.Object:
                var obj = (JObject)first.Value;

                if (!obj.TryGetValue("value", out var value))
                {
                    throw new ArgumentException("Missing value in fuzzy query");
                }

                obj.TryGetValue("fuzziness", out var fuzziness);
                obj.TryGetValue("prefix_length", out var prefixLength);
                obj.TryGetValue("max_expansions", out var maxExpansions);

                fuzzyQuery = new FuzzyQuery(
                    new Term(first.Name, value.Value <string>()),
                    fuzziness?.Value <int>() ?? LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE,
                    prefixLength?.Value <int>() ?? 0,
                    maxExpansions?.Value <int>() ?? 50,
                    true);

                if (obj.TryGetValue("boost", out var boost))
                {
                    fuzzyQuery.Boost = boost.Value <float>();
                }

                break;

            default: throw new ArgumentException("Invalid fuzzy query");
            }

            booleanQuery.Add(fuzzyQuery, Occur.MUST);
            var queryFilter = new QueryWrapperFilter(fuzzyQuery);

            return(new FilteredQuery(booleanQuery, queryFilter));
        }
Beispiel #14
0
        public static void FuzzyQueryTest(Analyzer analyzer, string field, string keyword)
        {
            Console.WriteLine("====FuzzyQuery====");
            FuzzyQuery query = new FuzzyQuery(new Term(field, keyword));

            ShowQueryExpression(analyzer, query, keyword);
            SearchToShow(query);
            Console.WriteLine();
        }
Beispiel #15
0
 public virtual void TestFuzzy2()
 {
     // maximum of 1 term expansion
     FuzzyQuery fq = new FuzzyQuery(new Term("field", "broan"), 1, 0, 1, false);
     SpanQuery sfq = new SpanMultiTermQueryWrapper<MultiTermQuery>(fq);
     // will only match jumps over lazy broun dog
     SpanPositionRangeQuery sprq = new SpanPositionRangeQuery(sfq, 0, 100);
     Assert.AreEqual(1, searcher.Search(sprq, 10).TotalHits);
 }
Beispiel #16
0
        public override Query VisitFuzzyQuery(FuzzyQuery fuzzyq)
        {
            _text.Append(TermToString(fuzzyq.GetTerm()));
            _text.Append('~');
            _text.Append(Lucene.Net.Support.Single.ToString(fuzzyq.GetMinSimilarity()));
            _text.Append(BoostToString(fuzzyq.GetBoost()));

            return(base.VisitFuzzyQuery(fuzzyq));
        }
Beispiel #17
0
        public IEnumerable <LuceneHit> ContactSearch(string searchterm, string subdomain_name)
        {
            var ids = new HashSet <LuceneHit>();

            using (var searcher = new IndexSearcher(LuceneUtil.GetDirectoryInfo(LuceneIndexType.CONTACTS, subdomain_name), true))
            {
                var term1 = new Term("email", searchterm);
                var term2 = new Term("name", searchterm);
                var term3 = new Term("orgname", searchterm);
                var term4 = new Term("fullname", searchterm);
                var query = new FuzzyQuery(term1, 0.7f);
                var hits1 = searcher.Search(query);
                query = new FuzzyQuery(term2, 0.7f);
                var hits2 = searcher.Search(query);
                query = new FuzzyQuery(term3, 0.7f);
                var hits3 = searcher.Search(query);
                query = new FuzzyQuery(term4, 0.7f);
                var hits4 = searcher.Search(query);

                for (int i = 0; i < hits1.Length(); i++)
                {
                    Document doc = hits1.Doc(i);
                    ids.Add(new LuceneHit()
                    {
                        id = doc.Get("id"), score = hits1.Score(i)
                    });
                }

                for (int i = 0; i < hits2.Length(); i++)
                {
                    Document doc = hits2.Doc(i);
                    ids.Add(new LuceneHit()
                    {
                        id = doc.Get("id"), score = hits2.Score(i)
                    });
                }

                for (int i = 0; i < hits3.Length(); i++)
                {
                    Document doc = hits3.Doc(i);
                    ids.Add(new LuceneHit()
                    {
                        id = doc.Get("id"), score = hits3.Score(i)
                    });
                }
                for (int i = 0; i < hits4.Length(); i++)
                {
                    Document doc = hits4.Doc(i);
                    ids.Add(new LuceneHit()
                    {
                        id = doc.Get("id"), score = hits4.Score(i)
                    });
                }
            }

            return(ids);
        }
Beispiel #18
0
        private static void FuzzySearch(IndexWriter writer)
        {
            var phrase = new FuzzyQuery(new Term("line_text_full", "Mary"));

            var searcher = new IndexSearcher(writer.GetReader(true));

            var result = searcher.Search(phrase, 200);

            var _ = result.ScoreDocs;
        }
Beispiel #19
0
        public virtual Query VisitFuzzyQuery(FuzzyQuery fuzzyq)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            var term = fuzzyq.GetTerm();
#pragma warning restore CS0618 // Type or member is obsolete
            var visited = VisitTerm(term);
            if (term == visited)
                return fuzzyq;
            return new FuzzyQuery(visited);
        }
Beispiel #20
0
        /// <summary>
        /// Builds a new <see cref="FuzzyQuery"/> instance
        /// </summary>
        /// <param name="term">Term</param>
        /// <param name="minimumSimilarity">minimum similarity</param>
        /// <param name="prefixLength">prefix length</param>
        /// <returns>new <see cref="FuzzyQuery"/> Instance</returns>
        protected internal virtual Query NewFuzzyQuery(Term term, float minimumSimilarity, int prefixLength)
        {
            // FuzzyQuery doesn't yet allow constant score rewrite
            string text = term.Text();

#pragma warning disable 612, 618
            int numEdits = FuzzyQuery.SingleToEdits(minimumSimilarity,
                                                    text.CodePointCount(0, text.Length));
#pragma warning restore 612, 618
            return(new FuzzyQuery(term, numEdits, prefixLength));
        }
Beispiel #21
0
        public virtual Query VisitFuzzyQuery(FuzzyQuery fuzzyq)
        {
            var term    = fuzzyq.GetTerm();
            var visited = VisitTerm(term);

            if (term == visited)
            {
                return(fuzzyq);
            }
            return(new FuzzyQuery(visited));
        }
Beispiel #22
0
        public override Query VisitFuzzyQuery(FuzzyQuery fuzzyq)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            _text.Append(TermToString(fuzzyq.GetTerm()));
#pragma warning restore CS0618 // Type or member is obsolete
            _text.Append('~');
            _text.Append(Lucene.Net.Support.Single.ToString(fuzzyq.GetMinSimilarity()));
            _text.Append(BoostToString(fuzzyq.GetBoost()));

            return(base.VisitFuzzyQuery(fuzzyq));
        }
Beispiel #23
0
        private ParameterizedSql BuildFuzzy(FuzzyQuery fuzzyQuery)
        {
            Term term = CopyTerm(fuzzyQuery.Term);

            if (term != null)
            {
                return(BuildQuery(new FuzzyQuery(term, fuzzyQuery.MinSimilarity, fuzzyQuery.PrefixLength)));
            }

            return(null);
        }
Beispiel #24
0
        public static QueryContainer MakeFuzzyQuery(string query, string field, int fuzziness = -1)
        {
            QueryContainer fuzzyQuery = new FuzzyQuery
            {
                Field     = field,
                Value     = query,
                Fuzziness = fuzziness == -1 ? Fuzziness.Auto : Fuzziness.EditDistance(fuzziness)
            };

            return(fuzzyQuery);
        }
Beispiel #25
0
        public void Fetch_WhenCalledForLargeDocument_ShouldReturnSomeHits()
        {
            var phrase = new FuzzyQuery(new Term("text", "Mary"));

            var searcher = _fixture.CreateSearcher();

            var result = searcher.Search(phrase, 200);

            var hits = result.ScoreDocs;

            Assert.NotEmpty(hits);
        }
Beispiel #26
0
        /// <summary>
        /// Adds a fuzzy clause to this instance
        /// </summary>
        /// <remarks>Fuzzy clauses find results within a particular relevance distance of each hit</remarks>
        /// <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="minimumSimilarity">Defines the amount of similarity that is allowed between matches</param>
        public void AddFuzzyClause(SearchTerm term, ClauseOccurrence occurrence, float minimumSimilarity)
        {
            if (term == null)
            {
                throw new ArgumentNullException("term", "term cannot be null");
            }
            IncrementTotalClauses(1);
            FuzzyQuery fuzzyQuery = new FuzzyQuery(term.GetLuceneTerm(), minimumSimilarity);

            fuzzyQuery.SetBoost(term.Boost);
            this.luceneQuery.Add(fuzzyQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence));
            fuzzyQuery = null;
        }
        public FuzzyQueryInstance CreateFuzzyQuery(string fieldName, string text)
        {
            var query = new FuzzyQuery
            {
                Term = new Term
                {
                    FieldName = fieldName,
                    Value     = text
                }
            };

            return(new FuzzyQueryInstance(Engine.Object.InstancePrototype, query));
        }
Beispiel #28
0
        public virtual Query Build(IQueryNode queryNode)
        {
            FuzzyQueryNode fuzzyNode = (FuzzyQueryNode)queryNode;
            string         text      = fuzzyNode.GetTextAsString();

#pragma warning disable 612, 618
            int numEdits = FuzzyQuery.SingleToEdits(fuzzyNode.Similarity,
                                                    text.CodePointCount(0, text.Length));
#pragma warning restore 612, 618

            return(new FuzzyQuery(new Term(fuzzyNode.GetFieldAsString(), fuzzyNode
                                           .GetTextAsString()), numEdits, fuzzyNode
                                  .PrefixLength));
        }
Beispiel #29
0
        static void DifferentQuerysInEs(QueryContainer qc)
        {
            //匹配查询
            qc &= new MatchQuery()
            {
                Field              = "analyized field name",
                Query              = "关键词",
                Operator           = Operator.Or,
                MinimumShouldMatch = "100%",
                Analyzer           = "ik_smart"
            };

            //https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-syntax
            //https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html
            //query string自定义了一个查询语法
            var qsq = new QueryStringQuery()
            {
            };

            //https://www.elastic.co/guide/cn/elasticsearch/guide/current/_wildcard_and_regexp_queries.html
            //使用通配符查询,比如name.*
            var wcq = new WildcardQuery()
            {
            };

            //精准匹配,不分词
            var tq = new TermQuery()
            {
            };

            //字段存在且不为空
            var eq = new ExistsQuery()
            {
            };

            //https://www.elastic.co/guide/cn/elasticsearch/guide/current/fuzzy-query.html
            //模糊查询,它会计算关键词和目标字段的“距离”。如果在允许的距离范围内,计算拼写错误也可以匹配到
            var fq = new FuzzyQuery()
            {
            };

            //范围查询
            var drq = new DateRangeQuery()
            {
            };
            var nrq = new NumericRangeQuery()
            {
            };
        }
        /// <summary>
        /// Factory method to generate a fuzzy query.
        /// </summary>
        protected virtual Query NewFuzzyQuery(string text, int fuzziness)
        {
            BooleanQuery bq = new BooleanQuery(true);

            foreach (var entry in m_weights)
            {
                Query q = new FuzzyQuery(new Term(entry.Key, text), fuzziness);
                if (q != null)
                {
                    q.Boost = entry.Value;
                    bq.Add(q, Occur.SHOULD);
                }
            }
            return(Simplify(bq));
        }
Beispiel #31
0
        public override Query VisitFuzzyQuery(FuzzyQuery fuzzyq)
        {
            _dump.Append("FuzzyQ(");
            var q  = base.VisitFuzzyQuery(fuzzyq);
            var fq = q as FuzzyQuery;

            if (fq != null)
            {
                _dump.Append(", minSimilarity:");
                _dump.Append(fq.GetMinSimilarity());
            }
            _dump.Append(BoostToString(q));
            _dump.Append(")");
            return(q);
        }
 public virtual void TestFuzzy2()
 {
     // maximum of 1 term expansion
     FuzzyQuery fq = new FuzzyQuery(new Term("field", "broan"), 1, 0, 1, false);
     SpanQuery sfq = new SpanMultiTermQueryWrapper<MultiTermQuery>(fq);
     // will only match jumps over lazy broun dog
     SpanPositionRangeQuery sprq = new SpanPositionRangeQuery(sfq, 0, 100);
     Assert.AreEqual(1, Searcher.Search(sprq, 10).TotalHits);
 }
        public virtual void TestNoSuchMultiTermsInOr()
        {
            //test to make sure non existent multiterms aren't throwing null pointer exceptions
            FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
            SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(fuzzyNoSuch);
            SpanQuery term = new SpanTermQuery(new Term("field", "brown"));
            SpanOrQuery near = new SpanOrQuery(new SpanQuery[] { term, spanNoSuch });
            Assert.AreEqual(1, Searcher.Search(near, 10).TotalHits);

            //flip
            near = new SpanOrQuery(new SpanQuery[] { spanNoSuch, term });
            Assert.AreEqual(1, Searcher.Search(near, 10).TotalHits);

            WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
            SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(wcNoSuch);
            near = new SpanOrQuery(new SpanQuery[] { term, spanWCNoSuch });
            Assert.AreEqual(1, Searcher.Search(near, 10).TotalHits);

            RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
            SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(rgxNoSuch);
            near = new SpanOrQuery(new SpanQuery[] { term, spanRgxNoSuch });
            Assert.AreEqual(1, Searcher.Search(near, 10).TotalHits);

            PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
            SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(prfxNoSuch);
            near = new SpanOrQuery(new SpanQuery[] { term, spanPrfxNoSuch });
            Assert.AreEqual(1, Searcher.Search(near, 10).TotalHits);

            near = new SpanOrQuery(new SpanQuery[] { spanPrfxNoSuch });
            Assert.AreEqual(0, Searcher.Search(near, 10).TotalHits);

            near = new SpanOrQuery(new SpanQuery[] { spanPrfxNoSuch, spanPrfxNoSuch });
            Assert.AreEqual(0, Searcher.Search(near, 10).TotalHits);
        }
        public virtual void TestNoSuchMultiTermsInSpanFirst()
        {
            //this hasn't been a problem
            FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
            SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(fuzzyNoSuch);
            SpanQuery spanFirst = new SpanFirstQuery(spanNoSuch, 10);

            Assert.AreEqual(0, Searcher.Search(spanFirst, 10).TotalHits);

            WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
            SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(wcNoSuch);
            spanFirst = new SpanFirstQuery(spanWCNoSuch, 10);
            Assert.AreEqual(0, Searcher.Search(spanFirst, 10).TotalHits);

            RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
            SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(rgxNoSuch);
            spanFirst = new SpanFirstQuery(spanRgxNoSuch, 10);
            Assert.AreEqual(0, Searcher.Search(spanFirst, 10).TotalHits);

            PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
            SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(prfxNoSuch);
            spanFirst = new SpanFirstQuery(spanPrfxNoSuch, 10);
            Assert.AreEqual(0, Searcher.Search(spanFirst, 10).TotalHits);
        }
 /// <summary>
 /// Adds a fuzzy clause to this instance
 /// </summary>
 /// <remarks>Fuzzy clauses find results within a particular relevance distance of each hit</remarks>
 /// <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="minimumSimilarity">Defines the amount of similarity that is allowed between matches</param>
 public void AddFuzzyClause(SearchTerm term, ClauseOccurrence occurrence, float minimumSimilarity)
 {
     if (term == null)
         throw new ArgumentNullException("term", "term cannot be null");
     IncrementTotalClauses(1);
     FuzzyQuery fuzzyQuery = new FuzzyQuery(term.GetLuceneTerm(), minimumSimilarity);
     fuzzyQuery.SetBoost(term.Boost);
     this.luceneQuery.Add(fuzzyQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence));
     fuzzyQuery = null;
 }
		public SpanMultiQuery(FuzzyQuery query)
		{
			_query = query;
		}