Пример #1
0
 public static Query BuildQuery(string query)
 {
     var matchCollection = untokenizedQuery.Matches(query);
     if (matchCollection.Count == 0)
         return new QueryParser(Version.LUCENE_CURRENT, "", new StandardAnalyzer(Version.LUCENE_CURRENT)).Parse(query);
     var sb = new StringBuilder(query);
     var booleanQuery = new BooleanQuery();
     foreach (Match match in matchCollection)
     {
         BooleanClause.Occur occur;
         switch (match.Groups[1].Value)
         {
             case "+":
                 occur=BooleanClause.Occur.MUST;
                 break;
             case "-":
                 occur=BooleanClause.Occur.MUST_NOT;
                 break;
             default:
                 occur=BooleanClause.Occur.SHOULD;
                 break;
         }
         booleanQuery.Add(new TermQuery(new Term(match.Groups[2].Value, match.Groups[3].Value)), occur);
         sb.Replace(match.Value, "");
     }
     var remaining = sb.ToString().Trim();
     if(remaining.Length > 0)
     {
         booleanQuery.Add(new QueryParser(Version.LUCENE_CURRENT, "", new StandardAnalyzer(Version.LUCENE_CURRENT)).Parse(remaining), BooleanClause.Occur.SHOULD);
     }
     return booleanQuery;
 }
Пример #2
0
		public virtual void  TestNullOrSubScorer()
		{
			Directory dir = new MockRAMDirectory();
			IndexWriter w = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);
			Document doc = new Document();
			doc.Add(new Field("field", "a b c d", Field.Store.NO, Field.Index.ANALYZED));
			w.AddDocument(doc);
			IndexReader r = w.GetReader();
			IndexSearcher s = new IndexSearcher(r);
			BooleanQuery q = new BooleanQuery();
			q.Add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD);
			
			// PhraseQuery w/ no terms added returns a null scorer
			PhraseQuery pq = new PhraseQuery();
			q.Add(pq, BooleanClause.Occur.SHOULD);
			Assert.AreEqual(1, s.Search(q, 10).TotalHits);
			
			// A required clause which returns null scorer should return null scorer to
			// IndexSearcher.
			q = new BooleanQuery();
			pq = new PhraseQuery();
			q.Add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD);
			q.Add(pq, BooleanClause.Occur.MUST);
			Assert.AreEqual(0, s.Search(q, 10).TotalHits);
			
			DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(1.0f);
			dmq.Add(new TermQuery(new Term("field", "a")));
			dmq.Add(pq);
			Assert.AreEqual(1, s.Search(dmq, 10).TotalHits);
			
			r.Close();
			w.Close();
			dir.Close();
		}
		public virtual void  TestQuery()
		{
			RAMDirectory dir = new RAMDirectory();
			IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(), true);
			AddDoc("one", iw);
			AddDoc("two", iw);
			AddDoc("three four", iw);
			iw.Close();
			
			IndexSearcher is_Renamed = new IndexSearcher(dir);
			Hits hits = is_Renamed.Search(new MatchAllDocsQuery());
			Assert.AreEqual(3, hits.Length());
			
			// some artificial queries to trigger the use of skipTo():
			
			BooleanQuery bq = new BooleanQuery();
			bq.Add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
			bq.Add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
			hits = is_Renamed.Search(bq);
			Assert.AreEqual(3, hits.Length());
			
			bq = new BooleanQuery();
			bq.Add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
			bq.Add(new TermQuery(new Term("key", "three")), BooleanClause.Occur.MUST);
			hits = is_Renamed.Search(bq);
			Assert.AreEqual(1, hits.Length());
			
			// delete a document:
			is_Renamed.GetIndexReader().DeleteDocument(0);
			hits = is_Renamed.Search(new MatchAllDocsQuery());
			Assert.AreEqual(2, hits.Length());
			
			is_Renamed.Close();
		}
Пример #4
0
        public Query GetQuery()
        {
            if (String.IsNullOrWhiteSpace(Term) && String.IsNullOrWhiteSpace(Type) && !CreatedOnTo.HasValue && !CreatedOnFrom.HasValue && Parent == null)
                return new MatchAllDocsQuery();

            var booleanQuery = new BooleanQuery();
            if (!String.IsNullOrWhiteSpace(Term))
            {
                var indexDefinition = IndexingHelper.Get<AdminWebpageIndexDefinition>();
                var analyser = indexDefinition.GetAnalyser();
                var parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, indexDefinition.SearchableFieldNames, analyser);
                Query query = Term.SafeGetSearchQuery(parser, analyser);

                booleanQuery.Add(query, Occur.MUST);
            }
            if (CreatedOnFrom.HasValue || CreatedOnTo.HasValue)
                booleanQuery.Add(GetDateQuery(), Occur.MUST);
            if (!string.IsNullOrEmpty(Type))
                booleanQuery.Add(new TermQuery(new Term(FieldDefinition.GetFieldName<TypeFieldDefinition>(), Type)),
                                 Occur.MUST);
            if (Parent != null)
                booleanQuery.Add(
                    new TermQuery(new Term(FieldDefinition.GetFieldName<ParentIdFieldDefinition>(), Parent.Id.ToString())), Occur.MUST);

            return booleanQuery;
        }
 public virtual void TestBQ14()
 {
     BooleanQuery q = new BooleanQuery(true);
     q.Add(new TermQuery(new Term(FIELD, "QQQQQ")), BooleanClause.Occur.SHOULD);
     q.Add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
     Qtest(q, new int[] { 0, 1, 2, 3 });
 }
        public IEnumerable<BeerReview> Get(string reviewText = null, int limit = 10, double minAroma = 0, double minAppearance = 0, double minOverall = 0, double minTaste = 0, double minPalate = 0)
        {
            var query = new BooleanQuery();
            Query reviewTextQuery = new TermQuery(new Term("reviewText", reviewText));
            Query reviewAppearanceQuery = NumericRangeQuery.NewDoubleRange("reviewAppearance", minAppearance, null, minInclusive: true, maxInclusive: true);
            Query reviewAromaQuery = NumericRangeQuery.NewDoubleRange("reviewAroma", minAroma, null, minInclusive: true, maxInclusive: true);
            Query reviewPalateQuery = NumericRangeQuery.NewDoubleRange("reviewPalate", minPalate, null, minInclusive: true, maxInclusive: true);
            Query reviewTasteQuery = NumericRangeQuery.NewDoubleRange("reviewTaste", minTaste, null, minInclusive: true, maxInclusive: true);
            Query reviewOverallQuery = NumericRangeQuery.NewDoubleRange("reviewOverall", minOverall, null, minInclusive: true, maxInclusive: true);

            if (reviewText != null) { query.Add(reviewTextQuery, Occur.MUST); }
            query.Add(reviewAppearanceQuery, Occur.MUST);
            query.Add(reviewAromaQuery, Occur.MUST);
            query.Add(reviewOverallQuery, Occur.MUST);
            query.Add(reviewPalateQuery, Occur.MUST);
            query.Add(reviewTasteQuery, Occur.MUST);

            var hits = indexSearcher.Search(query, limit);

            var beers = new List<BeerReview>();

            for (int i = 0; i < hits.ScoreDocs.Count(); i++)
            {
                beers.Add(BeerReviewFromDoc(hits.ScoreDocs[i]));
            }

            return beers;
        }
Пример #7
0
        public virtual void TestAsterisk()
        {
            Directory indexStore = GetIndexStore("body", new string[] { "metal", "metals" });
            IndexReader reader = DirectoryReader.Open(indexStore);
            IndexSearcher searcher = NewSearcher(reader);
            Query query1 = new TermQuery(new Term("body", "metal"));
            Query query2 = new WildcardQuery(new Term("body", "metal*"));
            Query query3 = new WildcardQuery(new Term("body", "m*tal"));
            Query query4 = new WildcardQuery(new Term("body", "m*tal*"));
            Query query5 = new WildcardQuery(new Term("body", "m*tals"));

            BooleanQuery query6 = new BooleanQuery();
            query6.Add(query5, BooleanClause.Occur.SHOULD);

            BooleanQuery query7 = new BooleanQuery();
            query7.Add(query3, BooleanClause.Occur.SHOULD);
            query7.Add(query5, BooleanClause.Occur.SHOULD);

            // Queries do not automatically lower-case search terms:
            Query query8 = new WildcardQuery(new Term("body", "M*tal*"));

            AssertMatches(searcher, query1, 1);
            AssertMatches(searcher, query2, 2);
            AssertMatches(searcher, query3, 1);
            AssertMatches(searcher, query4, 2);
            AssertMatches(searcher, query5, 1);
            AssertMatches(searcher, query6, 1);
            AssertMatches(searcher, query7, 2);
            AssertMatches(searcher, query8, 0);
            AssertMatches(searcher, new WildcardQuery(new Term("body", "*tall")), 0);
            AssertMatches(searcher, new WildcardQuery(new Term("body", "*tal")), 1);
            AssertMatches(searcher, new WildcardQuery(new Term("body", "*tal*")), 2);
            reader.Dispose();
            indexStore.Dispose();
        }
        internal static void AddDateRangeQuery(BooleanQuery query, DateRangeSearchParam.DateRangeField dateRangeField, BooleanClause.Occur occurance)
        {
            var startDateTime = dateRangeField.StartDate;
            if (dateRangeField.InclusiveStart)
            {
                if (startDateTime != DateTime.MinValue)
                {
                    startDateTime.ChangeTime(0, 0, 0, 0);
                }
            }

            var endDateTime = dateRangeField.EndDate;
            if (dateRangeField.InclusiveStart)
            {
                if (endDateTime != DateTime.MaxValue)
                {
                    endDateTime = endDateTime.ChangeTime(23, 59, 59, 59);
                }
            }

            BooleanQuery.SetMaxClauseCount(int.MaxValue);

            if (!(dateRangeField.StartDate == DateTime.MinValue && dateRangeField.EndDate == DateTime.MaxValue))
            {
                query.Add(new RangeQuery(new Term(SearchFieldIDs.CreatedDate, startDateTime.ToString("yyyyMMdd")), new Term(SearchFieldIDs.CreatedDate, endDateTime.ToString("yyyyMMdd")), true), occurance);
            }
        }
Пример #9
0
        public static Lucene.Net.Search.Query FilterQueryByClasses(ISet<System.Type> classesAndSubclasses, Lucene.Net.Search.Query luceneQuery)
        {
            // A query filter is more practical than a manual class filtering post query (esp on scrollable resultsets)
            // it also probably minimise the memory footprint
            if (classesAndSubclasses == null)
            {
                return luceneQuery;
            }

            BooleanQuery classFilter = new BooleanQuery();

            // annihilate the scoring impact of DocumentBuilder.CLASS_FIELDNAME
            classFilter.SetBoost(0);
            foreach (System.Type clazz in classesAndSubclasses)
            {
                Term t = new Term(DocumentBuilder.CLASS_FIELDNAME, TypeHelper.LuceneTypeName(clazz));
                TermQuery termQuery = new TermQuery(t);
                classFilter.Add(termQuery, BooleanClause.Occur.SHOULD);
            }

            BooleanQuery filteredQuery = new BooleanQuery();
            filteredQuery.Add(luceneQuery, BooleanClause.Occur.MUST);
            filteredQuery.Add(classFilter, BooleanClause.Occur.MUST);
            return filteredQuery;
        }
        public virtual Query Build(IQueryNode queryNode)
        {
            StandardBooleanQueryNode booleanNode = (StandardBooleanQueryNode)queryNode;

            BooleanQuery bQuery = new BooleanQuery(booleanNode.DisableCoord);
            IList<IQueryNode> children = booleanNode.GetChildren();

            if (children != null)
            {
                foreach (IQueryNode child in children)
                {
                    object obj = child.GetTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);

                    if (obj != null)
                    {
                        Query query = (Query)obj;

                        try
                        {
                            bQuery.Add(query, GetModifierValue(child));
                        }
                        catch (BooleanQuery.TooManyClauses ex)
                        {
                            throw new QueryNodeException(new MessageImpl(
                                QueryParserMessages.TOO_MANY_BOOLEAN_CLAUSES, BooleanQuery
                                    .MaxClauseCount, queryNode
                                    .ToQueryString(new EscapeQuerySyntaxImpl())), ex);
                        }
                    }
                }
            }

            return bQuery;
        }
Пример #11
0
 /// <summary>
 /// Decorates the specified query adding context information.
 /// </summary>
 /// <param name="query">The source query.</param>
 /// <returns>The decorated query.</returns>
 public Lucene.Net.Search.Query Decorate(Lucene.Net.Search.Query query)
 {
     BooleanQuery result = new BooleanQuery(true);
       result.Add(query, Occur.MUST);
       this.AddDecorations(result);
       return result;
 }
        /// <summary>
        ///     Adds the query.
        /// </summary>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="query">The query.</param>
        /// <param name="filter">The filter.</param>
        protected void AddQuery(string fieldName, BooleanQuery query, StringCollection filter)
        {
            fieldName = fieldName.ToLower();
            if (filter.Count > 0)
            {
                if (filter.Count != 1)
                {
                    var booleanQuery = new BooleanQuery();
                    var containsFilter = false;
                    foreach (var index in filter)
                    {
                        if (String.IsNullOrEmpty(index))
                        {
                            continue;
                        }

                        var nodeQuery = new WildcardQuery(new Term(fieldName, index));
                        booleanQuery.Add(nodeQuery, Occur.SHOULD);
                        containsFilter = true;
                    }
                    if (containsFilter)
                    {
                        query.Add(booleanQuery, Occur.MUST);
                    }
                }
                else
                {
                    if (!String.IsNullOrEmpty(filter[0]))
                    {
                        this.AddQuery(fieldName, query, filter[0].ToLower());
                    }
                }
            }
        }
Пример #13
0
        public virtual void TestMethod()
        {
            Directory directory = NewDirectory();

            string[] values = new string[] { "1", "2", "3", "4" };

            RandomIndexWriter writer = new RandomIndexWriter(Random(), directory, Similarity, TimeZone);
            for (int i = 0; i < values.Length; i++)
            {
                Document doc = new Document();
                doc.Add(NewStringField(FIELD, values[i], Field.Store.YES));
                writer.AddDocument(doc);
            }
            IndexReader ir = writer.Reader;
            writer.Dispose();

            BooleanQuery booleanQuery1 = new BooleanQuery();
            booleanQuery1.Add(new TermQuery(new Term(FIELD, "1")), BooleanClause.Occur.SHOULD);
            booleanQuery1.Add(new TermQuery(new Term(FIELD, "2")), BooleanClause.Occur.SHOULD);

            BooleanQuery query = new BooleanQuery();
            query.Add(booleanQuery1, BooleanClause.Occur.MUST);
            query.Add(new TermQuery(new Term(FIELD, "9")), BooleanClause.Occur.MUST_NOT);

            IndexSearcher indexSearcher = NewSearcher(ir);
            ScoreDoc[] hits = indexSearcher.Search(query, null, 1000).ScoreDocs;
            Assert.AreEqual(2, hits.Length, "Number of matched documents");
            ir.Dispose();
            directory.Dispose();
        }
Пример #14
0
		public virtual void  TestMethod()
		{
			RAMDirectory directory = new RAMDirectory();
			
			System.String[] values = new System.String[]{"1", "2", "3", "4"};
			
			try
			{
				IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
				for (int i = 0; i < values.Length; i++)
				{
					Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
					doc.Add(new Field(FIELD, values[i], Field.Store.YES, Field.Index.UN_TOKENIZED));
					writer.AddDocument(doc);
				}
				writer.Close();
				
				BooleanQuery booleanQuery1 = new BooleanQuery();
				booleanQuery1.Add(new TermQuery(new Term(FIELD, "1")), BooleanClause.Occur.SHOULD);
				booleanQuery1.Add(new TermQuery(new Term(FIELD, "2")), BooleanClause.Occur.SHOULD);
				
				BooleanQuery query = new BooleanQuery();
				query.Add(booleanQuery1, BooleanClause.Occur.MUST);
				query.Add(new TermQuery(new Term(FIELD, "9")), BooleanClause.Occur.MUST_NOT);
				
				IndexSearcher indexSearcher = new IndexSearcher(directory);
				Hits hits = indexSearcher.Search(query);
				Assert.AreEqual(2, hits.Length(), "Number of matched documents");
			}
			catch (System.IO.IOException e)
			{
				Assert.Fail(e.Message);
			}
		}
        private static Query ParseQuery(string searchTerm)
        {
            var fields = new Dictionary<string, float> { { "Id", 1.2f }, { "Title", 1.0f }, { "Tags", 1.0f}, { "Description", 0.8f }, { "Author", 0.6f } };
            var analyzer = new StandardAnalyzer(LuceneCommon.LuceneVersion);
            searchTerm = QueryParser.Escape(searchTerm).ToLowerInvariant();

            var queryParser = new MultiFieldQueryParser(LuceneCommon.LuceneVersion, fields.Keys.ToArray(), analyzer, fields);

            var conjuctionQuery = new BooleanQuery();
            conjuctionQuery.SetBoost(1.5f);
            var disjunctionQuery = new BooleanQuery();
            var wildCardQuery = new BooleanQuery();
            wildCardQuery.SetBoost(0.7f);
            var exactIdQuery = new TermQuery(new Term("Id-Exact", searchTerm));
            exactIdQuery.SetBoost(2.5f);

            foreach(var term in searchTerm.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
            {
                conjuctionQuery.Add(queryParser.Parse(term), BooleanClause.Occur.MUST);
                disjunctionQuery.Add(queryParser.Parse(term), BooleanClause.Occur.SHOULD);

                foreach (var field in fields)
                {
                    var wildCardTermQuery = new WildcardQuery(new Term(field.Key, term + "*"));
                    wildCardTermQuery.SetBoost(0.7f * field.Value);
                    wildCardQuery.Add(wildCardTermQuery, BooleanClause.Occur.SHOULD);
                }
            }

            return conjuctionQuery.Combine(new Query[] { exactIdQuery, conjuctionQuery, disjunctionQuery, wildCardQuery });
        }
    /// <summary>
    /// Adds the decorations to the BooleanQuery object. Override in derived class to change the way context affects queries.
    /// </summary>
    /// <param name="result">The result.</param>
    protected override void AddDecorations(BooleanQuery result)
    {
      Assert.ArgumentNotNull(result, "result");
      User user = this.User;
      if (user != null)
      {
        result.Add(new TermQuery(new Term(Sitecore.Search.BuiltinFields.Creator, user.Name)), Occur.SHOULD);
        result.Add(new TermQuery(new Term(Sitecore.Search.BuiltinFields.Editor, user.Name)), Occur.SHOULD);
      }

      Item item = this.Item;
      if (item != null)
      {
        result.Add(new TermQuery(new Term(Sitecore.Search.BuiltinFields.Path, ShortID.Encode(item.ID).ToLowerInvariant())), Occur.MUST);
        result.Add(new TermQuery(new Term(Sitecore.Search.BuiltinFields.Database, item.Database.Name)), Occur.MUST);
        // result.Add(new TermQuery(new Term(BuiltinFields.Language, item.Language.ToString())), Occur.MUST);
      }

      if (!this.IgnoreContentEditorOptions)
      {
        if (!UserOptions.View.ShowHiddenItems)
        {
          result.Add(new TermQuery(new Term(Sitecore.Search.BuiltinFields.Hidden, "1")), Occur.MUST_NOT);
        }

        if (!UserOptions.View.ShowEntireTree && (item != null))
        {
          Item item2 = item.Database.GetItem(RootSections.GetSection(item));
          if (item2 != null)
          {
            result.Add(new TermQuery(new Term(Sitecore.Search.BuiltinFields.Path, ShortID.Encode(item2.ID).ToLowerInvariant())), Occur.MUST);
          }
        }
      }
    }
Пример #17
0
        public void AndExtension()
        {
            BooleanQuery originalQuery = new BooleanQuery();
            BooleanQuery innerQuery = new BooleanQuery();

            Term term = new Term("_name", "value1");
            TermQuery termQuery1 = new TermQuery(term);
            innerQuery.Add(termQuery1, Occur.MUST);

            Term term2 = new Term("_name", "value2");
            TermQuery termQuery2 = new TermQuery(term2);
            innerQuery.Add(termQuery2, Occur.MUST);

            originalQuery.Add(innerQuery, Occur.MUST);
            string queryString = originalQuery.ToString();

            QueryBuilder builder = new QueryBuilder();
            builder.And
                (
                    x => x.Term("_name", "value1"),
                    x => x.Term("_name", "value2")
                );
            Query replacementQuery = builder.Build();
            string newQueryString = replacementQuery.ToString();

            Assert.AreEqual(queryString, newQueryString);
            Console.Write(queryString);
        }
        public Query Get(ProductSearchQuery searchQuery)
        {
            var booleanQuery = new BooleanQuery { ProductSearchPublishedDefinition.PublishedOnly };
            if (!searchQuery.Options.Any() && !searchQuery.Specifications.Any() && Math.Abs(searchQuery.PriceFrom - 0) < 0.01 && !searchQuery.PriceTo.HasValue &&
                !searchQuery.CategoryId.HasValue && string.IsNullOrWhiteSpace(searchQuery.SearchTerm)
                && !searchQuery.BrandId.HasValue)
            {
                return booleanQuery;
            }

            if (searchQuery.Options.Any())
                booleanQuery.Add(GetOptionsQuery(searchQuery.Options), Occur.MUST);
            if (searchQuery.Specifications.Any())
                booleanQuery.Add(GetSpecificationsQuery(searchQuery.Specifications), Occur.MUST);
            if (searchQuery.CategoryId.HasValue)
                booleanQuery.Add(GetCategoriesQuery(searchQuery.CategoryId.Value), Occur.MUST);
            if (searchQuery.PriceFrom > 0 || searchQuery.PriceTo.HasValue)
                booleanQuery.Add(GetPriceRangeQuery(searchQuery), Occur.MUST);
            if (!String.IsNullOrWhiteSpace(searchQuery.SearchTerm))
            {
                IndexDefinition indexDefinition = IndexingHelper.Get<ProductSearchIndex>();
                Analyzer analyser = indexDefinition.GetAnalyser();
                var parser = new MultiFieldQueryParser(Version.LUCENE_30, indexDefinition.SearchableFieldNames, analyser);
                Query query = searchQuery.SearchTerm.SafeGetSearchQuery(parser, analyser);

                booleanQuery.Add(query, Occur.MUST);
            }
            if (searchQuery.BrandId.HasValue)
                booleanQuery.Add(GetBrandQuery(searchQuery.BrandId.Value), Occur.MUST);
            return booleanQuery;
        }
Пример #19
0
        public virtual void TestOutOfOrderDocsScoringSort()
        {
            // Two Sort criteria to instantiate the multi/single comparators.
            Sort[] sort = new Sort[] { new Sort(SortField.FIELD_DOC), new Sort() };
            bool[][] tfcOptions = new bool[][] { new bool[] { false, false, false }, new bool[] { false, false, true }, new bool[] { false, true, false }, new bool[] { false, true, true }, new bool[] { true, false, false }, new bool[] { true, false, true }, new bool[] { true, true, false }, new bool[] { true, true, true } };
            string[] actualTFCClasses = new string[] { "OutOfOrderOneComparatorNonScoringCollector", "OutOfOrderOneComparatorScoringMaxScoreCollector", "OutOfOrderOneComparatorScoringNoMaxScoreCollector", "OutOfOrderOneComparatorScoringMaxScoreCollector", "OutOfOrderOneComparatorNonScoringCollector", "OutOfOrderOneComparatorScoringMaxScoreCollector", "OutOfOrderOneComparatorScoringNoMaxScoreCollector", "OutOfOrderOneComparatorScoringMaxScoreCollector" };

            BooleanQuery bq = new BooleanQuery();
            // Add a Query with SHOULD, since bw.Scorer() returns BooleanScorer2
            // which delegates to BS if there are no mandatory clauses.
            bq.Add(new MatchAllDocsQuery(), Occur.SHOULD);
            // Set minNrShouldMatch to 1 so that BQ will not optimize rewrite to return
            // the clause instead of BQ.
            bq.MinimumNumberShouldMatch = 1;
            for (int i = 0; i < sort.Length; i++)
            {
                for (int j = 0; j < tfcOptions.Length; j++)
                {
                    TopDocsCollector<Entry> tdc = TopFieldCollector.Create(sort[i], 10, tfcOptions[j][0], tfcOptions[j][1], tfcOptions[j][2], false);

                    Assert.IsTrue(tdc.GetType().Name.EndsWith(actualTFCClasses[j]));

                    @is.Search(bq, tdc);

                    TopDocs td = tdc.TopDocs();
                    ScoreDoc[] sd = td.ScoreDocs;
                    Assert.AreEqual(10, sd.Length);
                }
            }
        }
Пример #20
0
		public static Query BuildQuery(string query)
		{
			var untokenizedMatches = untokenizedQuery.Matches(query);
			var rangeMatches = rangeQuery.Matches(query);
		    var standardAnalyzer = new StandardAnalyzer(Version.LUCENE_29);
		    try
		    {
                if (untokenizedMatches.Count == 0 && rangeMatches.Count == 0)
                    return new QueryParser(Version.LUCENE_29, "", standardAnalyzer).Parse(query);
                var sb = new StringBuilder(query);
                var booleanQuery = new BooleanQuery();
                AddUntokenizedTerms(untokenizedMatches, booleanQuery, sb, query);
		    	AddRangeTerms(rangeMatches, booleanQuery, sb, query);
                var remaining = sb.ToString();
		    	remaining = hangingConditionAtStart.Replace(remaining, "");
		    	remaining = hangingConditionAtEnd.Replace(remaining, "");
				remaining = remaining.Trim();
		    	if (remaining.Length > 0)
                {
                    booleanQuery.Add(new QueryParser(Version.LUCENE_29, "", standardAnalyzer).Parse(remaining), BooleanClause.Occur.SHOULD);
                }
                return booleanQuery;
		    }
		    finally
		    {
		        standardAnalyzer.Close();
		    }
		}
Пример #21
0
        public ProjectData[] Search(string searchTerm)
        {
            IndexSearcher searcher = new IndexSearcher(luceneIndexDirectory);
            IntegralCollector searcherCollector = new IntegralCollector();
            // Setup the fields to search through
            string[] searchfields = new string[] { "name", "vessel" };

            // Build our booleanquery that will be a combination of all the queries for each individual search term
            var finalQuery = new BooleanQuery();
            var parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, searchfields, analyzer);

            // Split the search string into separate search terms by word
            string[] terms = searchTerm.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string term in terms)
                finalQuery.Add(parser.Parse(term.Replace("~", "") + "~"),Occur.SHOULD);

            searcher.Search(finalQuery, searcherCollector);
            var results = new ProjectData[searcherCollector.Docs.Count];
            for (int i = 0; i < searcherCollector.Docs.Count; i++)
            {
                var doc = searcher.Doc(searcherCollector.Docs[i]);
                results[i] = new ProjectData(doc.Get("name"), doc.Get("vessel"));
            }
            return results;
        }
        private static IEnumerable<CustomerId> _search(string searchQuery)
        {
            // validation
            if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", ""))) return new List<CustomerId>();

            // set up lucene searcher
            using (var searcher = new IndexSearcher(_directory, false))
            {
                var hits_limit = 1000;
                var analyzer = new StandardAnalyzer(Version.LUCENE_29);

                {
                    //var query = new TermQuery(new Term("CustomerName", searchQuery));
                    var query = new BooleanQuery();
                    query.Add(new TermQuery(new Term("CustomerName", searchQuery)), Occur.MUST);
                    var hits = searcher.Search(query, hits_limit).ScoreDocs;
                    var results = _mapLuceneToDataList(hits, searcher);

                    analyzer.Close();
                    searcher.Dispose();
                    return results;
                }
                
            }
        }
 public virtual void TestBQ1()
 {
     BooleanQuery query = new BooleanQuery();
     query.Add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
     query.Add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.MUST);
     Qtest(query, new int[] { 0, 1, 2, 3 });
 }
Пример #24
0
		private void HandleRangeFacet(string index, Facet facet, IndexQuery indexQuery, IndexSearcher currentIndexSearcher, Dictionary<string, IEnumerable<FacetValue>> results)
		{
			var rangeResults = new List<FacetValue>();
			foreach (var range in facet.Ranges)
			{
				var baseQuery = database.IndexStorage.GetLuceneQuery(index, indexQuery, database.IndexQueryTriggers);
				//TODO the built-in parser can't handle [NULL TO 100.0}, i.e. a mix of [ and }
				//so we need to handle this ourselves (greater and less-than-or-equal)
				var rangeQuery = database.IndexStorage.GetLuceneQuery(index, new IndexQuery
				{
					Query = facet.Name + ":" + range
				}, database.IndexQueryTriggers);

				var joinedQuery = new BooleanQuery();
				joinedQuery.Add(baseQuery, BooleanClause.Occur.MUST);
				joinedQuery.Add(rangeQuery, BooleanClause.Occur.MUST);

				var topDocs = currentIndexSearcher.Search(joinedQuery, null, 1);

				if (topDocs.TotalHits > 0)
				{
					rangeResults.Add(new FacetValue
					{
						Count = topDocs.TotalHits,
						Range = range
					});
				}
			}

			results[facet.Name] = rangeResults;
		}
Пример #25
0
		public virtual void  TestBasic()
		{
			Directory dir = new RAMDirectory();
			IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
			Document doc = new Document();
			doc.Add(new Field("field", "value", Field.Store.NO, Field.Index.ANALYZED));
			writer.AddDocument(doc);
			writer.Close();
			
			TermQuery termQuery = new TermQuery(new Term("field", "value"));
			
			// should not throw exception with primitive query
			QueryWrapperFilter qwf = new QueryWrapperFilter(termQuery);
			
			IndexSearcher searcher = new IndexSearcher(dir, true);
			TopDocs hits = searcher.Search(new MatchAllDocsQuery(), qwf, 10);
			Assert.AreEqual(1, hits.totalHits);
			
			// should not throw exception with complex primitive query
			BooleanQuery booleanQuery = new BooleanQuery();
			booleanQuery.Add(termQuery, Occur.MUST);
			booleanQuery.Add(new TermQuery(new Term("field", "missing")), Occur.MUST_NOT);
			qwf = new QueryWrapperFilter(termQuery);
			
			hits = searcher.Search(new MatchAllDocsQuery(), qwf, 10);
			Assert.AreEqual(1, hits.totalHits);
			
			// should not throw exception with non primitive Query (doesn't implement
			// Query#createWeight)
			qwf = new QueryWrapperFilter(new FuzzyQuery(new Term("field", "valu")));
			
			hits = searcher.Search(new MatchAllDocsQuery(), qwf, 10);
			Assert.AreEqual(1, hits.totalHits);
		}
Пример #26
0
 public override Query Rewrite(IndexReader reader)
 {
     BooleanQuery query = new BooleanQuery(true);
     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) && term.Field() == prefixField)
             {
                 TermQuery tq = new TermQuery(term); // found a match
                 tq.SetBoost(GetBoost()); // set the boost
                 query.Add(tq, BooleanClause.Occur.SHOULD); // add to query
                 //System.out.println("added " + term);
             }
             else
             {
                 break;
             }
         }
         while (enumerator.Next());
     }
     finally
     {
         enumerator.Close();
     }
     return query;
 }
Пример #27
0
        public void SearchFiltered(IndexWriter writer, Directory directory, Filter filter, bool optimize)
        {
            try
            {
                for (int i = 0; i < 60; i++)
                {//Simple docs
                    Document doc = new Document();
                    doc.Add(new Field(FIELD, i.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    writer.AddDocument(doc);
                }
                if (optimize)
                    writer.Optimize();
                writer.Close();

                BooleanQuery booleanQuery = new BooleanQuery();
                booleanQuery.Add(new TermQuery(new Term(FIELD, "36")), Occur.SHOULD);


                IndexSearcher indexSearcher = new IndexSearcher(directory);
                ScoreDoc[] hits = indexSearcher.Search(booleanQuery, filter, 1000).ScoreDocs;
                Assert.AreEqual(1, hits.Length, "Number of matched documents");

            }
            catch (System.IO.IOException e)
            {
                Assert.Fail(e.Message);
            }

        }
Пример #28
0
        public static BooleanQuery ParseRange(string fieldName, long lowerValue, long upperValue, bool inclusive)
        {
            if (lowerValue > upperValue)
            {
                return null;
            }

            //var rangeQuery = new BooleanQuery();
            var dateQuery = new BooleanQuery();
            BooleanQuery.SetMaxClauseCount(int.MaxValue);

            for (long i = lowerValue; i < upperValue; i++)
            {
                var term = new Term(fieldName, i.ToString());
                var q = new TermQuery(term);
                dateQuery.Add(q, BooleanClause.Occur.SHOULD);
            }

            if (inclusive)
            {
                var term = new Term(fieldName, upperValue.ToString());
                var q = new TermQuery(term);
                dateQuery.Add(q, BooleanClause.Occur.SHOULD);
            }

            //if (dateQuery.GetClauses() != null || dateQuery.GetClauses().Length != 0)
            //{
            //    rangeQuery.Add(dateQuery, BooleanClause.Occur.MUST);
            //}

            return dateQuery;
        }
Пример #29
0
        public static LuceneResult MultiSearchBIMXchange(Dictionary<string,string> terms, int pageSize, int pageNumber)
        {
            var directory = FSDirectory.Open(new DirectoryInfo("LuceneIndex"));
            var booleanQuery = new BooleanQuery();
            foreach(var term in terms)
            {
                var query = new TermQuery(new Term(term.Key, term.Value));
                booleanQuery.Add(query,BooleanClause.Occur.MUST);
            }
            var searcher = new IndexSearcher(directory, true);

            var topDocs = searcher.Search(booleanQuery, 10);

            var docs = new List<Document>();
            var start = (pageNumber - 1) * pageSize;
            for (var i = start; i < start + pageSize && i < topDocs.TotalHits; i++)
            {
                var scoreDoc = topDocs.ScoreDocs[i];
                var docId = scoreDoc.doc;
                var doc = searcher.Doc(docId);
                docs.Add(doc);
            }

            searcher.Close();
            directory.Close();
            var result = new LuceneResult {Results = docs, TotalCount = topDocs.TotalHits};
            return result;
        }
 private static BooleanQuery GetCombinedTermAndSplitQuery(string prefix, IEnumerable<Query> upperSplitQuery, Range currentPart)
 {
     var upperRangeQuery = new BooleanQuery();
     upperRangeQuery.AddTermQuery(currentPart.Name(prefix), currentPart.Value(currentPart.End));
     upperRangeQuery.AddClause(upperSplitQuery.First());
     return upperRangeQuery;
 }