private SearchResults SearchCore(SearchFilter searchFilter)
        {
            // Get index timestamp
            DateTime timestamp = File.GetLastWriteTimeUtc(LuceneCommon.GetIndexMetadataPath());

            int numRecords = searchFilter.Skip + searchFilter.Take;

            var searcher = new IndexSearcher(_directory, readOnly: true);
            var query    = ParseQuery(searchFilter.SearchTerm);

            // IF searching by relevance, boost scores by download count.
            if (searchFilter.SortOrder == SortOrder.Relevance)
            {
                var downloadCountBooster = new FieldScoreQuery("DownloadCount", FieldScoreQuery.Type.INT);
                query = new CustomScoreQuery(query, downloadCountBooster);
            }

            string filterTerm;

            if (SemVerLevelKey.ForSemVerLevel(searchFilter.SemVerLevel) == SemVerLevelKey.SemVer2)
            {
                filterTerm = searchFilter.IncludePrerelease ? "IsLatestSemVer2" : "IsLatestStableSemVer2";
            }
            else
            {
                filterTerm = searchFilter.IncludePrerelease ? "IsLatest" : "IsLatestStable";
            }

            Query filterQuery = new TermQuery(new Term(filterTerm, Boolean.TrueString));

            if (searchFilter.CuratedFeed != null)
            {
                var          feedFilterQuery  = new TermQuery(new Term("CuratedFeedKey", searchFilter.CuratedFeed.Key.ToString(CultureInfo.InvariantCulture)));
                BooleanQuery conjunctionQuery = new BooleanQuery();
                conjunctionQuery.Add(filterQuery, Occur.MUST);
                conjunctionQuery.Add(feedFilterQuery, Occur.MUST);
                filterQuery = conjunctionQuery;
            }

            Filter filter  = new QueryWrapperFilter(filterQuery);
            var    results = searcher.Search(query, filter: filter, n: numRecords, sort: new Sort(GetSortField(searchFilter)));

            if (results.TotalHits == 0 || searchFilter.CountOnly)
            {
                return(new SearchResults(results.TotalHits, timestamp));
            }

            var packages = results.ScoreDocs
                           .Skip(searchFilter.Skip)
                           .Select(sd => PackageFromDoc(searcher.Doc(sd.Doc)))
                           .ToList();

            return(new SearchResults(
                       results.TotalHits,
                       timestamp,
                       packages.AsQueryable()));
        }
            private readonly float[] vScores; // reused in score() to avoid allocating this array for each doc

            // constructor
            internal CustomScorer(CustomScoreQuery outerInstance, CustomScoreProvider provider, CustomWeight w,
                                  float qWeight, Scorer subQueryScorer, Scorer[] valSrcScorers)
                : base(w)
            {
                this.outerInstance  = outerInstance;
                this.qWeight        = qWeight;
                this.subQueryScorer = subQueryScorer;
                this.valSrcScorers  = valSrcScorers;
                this.vScores        = new float[valSrcScorers.Length];
                this.provider       = provider;
            }
 public CustomWeight(CustomScoreQuery outerInstance, IndexSearcher searcher)
 {
     this.outerInstance  = outerInstance;
     this.subQueryWeight = outerInstance.subQuery.CreateWeight(searcher);
     this.valSrcWeights  = new Weight[outerInstance.scoringQueries.Length];
     for (int i = 0; i < outerInstance.scoringQueries.Length; i++)
     {
         this.valSrcWeights[i] = outerInstance.scoringQueries[i].CreateWeight(searcher);
     }
     this.qStrict = outerInstance.strict;
 }
示例#4
0
        /// <summary>
        /// Perform search
        /// </summary>
        /// <param name="query"></param>
        /// <param name="startIndex"></param>
        /// <param name="blockSize"></param>
        /// <param name="indexDirEs"></param>
        /// <param name="indexDirEn"></param>
        /// <param name="sortBy"></param>
        /// <returns></returns>
        public List <IssueDocument> MedesSearch(Query query, int startIndex, int blockSize, Directory indexDirEs, Directory indexDirEn, Directory indexDirHe, string sortBy)
        {
#if DEBUG
            T.TraceMessage(string.Format("Begin search , query: '{0}'", query.ToString()));
#endif

            List <IssueDocument> result = new List <IssueDocument>();
            try
            {
                // build a multi searcher across the 2 indexes
                MultiSearcher mSearcher = CombineSearchers(indexDirEs, indexDirEn, indexDirHe);

                TopDocs tDocs       = null;
                int     iterateLast = startIndex + blockSize;


                string           customScoreField = "article_id";
                FieldScoreQuery  dateBooster      = new FieldScoreQuery(customScoreField, FieldScoreQuery.Type.FLOAT);
                CustomScoreQuery customQuery      = new CustomScoreQuery(query, dateBooster);

                tDocs = mSearcher.Search(customQuery, 1000);
                //ScoreDoc[] hits = tpDcs.scoreDocs;
                if (startIndex + blockSize > tDocs.TotalHits)
                {
                    iterateLast = tDocs.TotalHits;
                }

                for (int i = startIndex; i < iterateLast; i++)
                {
                    // Document hitDoc = mSearcher.Doc(hits[i].doc);

                    Document hitDoc = mSearcher.Doc(i);

                    result.Add(new IssueDocument()
                    {
                        Id = Int32.Parse(hitDoc.Get("issue_id").ToString())
                    });
                }

                // close the searcher and indexes
                mSearcher.Dispose();
                indexDirEs.Dispose();
                indexDirEn.Dispose();
                indexDirHe.Dispose();
            }
            catch (Exception ex)
            {
                T.TraceError("Error MedesSearch, query '{0}'", query.ToString());
                T.TraceError(ex);
                throw ex;
            }
            return(result);
        }
示例#5
0
        private IQueryable <Package> SearchCore(SearchFilter searchFilter, out int totalHits)
        {
            int numRecords = searchFilter.Skip + searchFilter.Take;

            var searcher = new IndexSearcher(_directory, readOnly: true);
            var query    = ParseQuery(searchFilter);

            // IF searching by relevance, boost scores by download count.
            if (searchFilter.SortProperty == SortProperty.Relevance)
            {
                var downloadCountBooster = new FieldScoreQuery("DownloadCount", FieldScoreQuery.Type.INT);
                query = new CustomScoreQuery(query, downloadCountBooster);
            }

            var   filterTerm  = searchFilter.IncludePrerelease ? "IsLatest" : "IsLatestStable";
            Query filterQuery = new TermQuery(new Term(filterTerm, Boolean.TrueString));

            if (searchFilter.CuratedFeedKey.HasValue)
            {
                var          feedFilterQuery  = new TermQuery(new Term("CuratedFeedKey", searchFilter.CuratedFeedKey.Value.ToString(CultureInfo.InvariantCulture)));
                BooleanQuery conjunctionQuery = new BooleanQuery();
                conjunctionQuery.Add(filterQuery, Occur.MUST);
                conjunctionQuery.Add(feedFilterQuery, Occur.MUST);
                filterQuery = conjunctionQuery;
            }

            Filter filter  = new QueryWrapperFilter(filterQuery);
            var    results = searcher.Search(query, filter: filter, n: numRecords, sort: new Sort(GetSortField(searchFilter)));

            totalHits = results.TotalHits;

            if (results.TotalHits == 0 || searchFilter.CountOnly)
            {
                return(Enumerable.Empty <Package>().AsQueryable());
            }

            var packages = results.ScoreDocs
                           .Skip(searchFilter.Skip)
                           .Select(sd => PackageFromDoc(searcher.Doc(sd.Doc)))
                           .ToList();

            return(packages.AsQueryable());
        }
        private SearchResults SearchCore(SearchFilter searchFilter)
        {
            // Get index timestamp
            DateTime timestamp = File.GetLastWriteTimeUtc(LuceneCommon.IndexMetadataPath);

            int numRecords = searchFilter.Skip + searchFilter.Take;

            var searcher = new IndexSearcher(_directory, readOnly: true);
            var query    = ParseQuery(searchFilter);

            // If searching by relevance, boost scores by download count.
            if (searchFilter.SortProperty == SortProperty.Relevance)
            {
                var downloadCountBooster = new FieldScoreQuery("DownloadCount", FieldScoreQuery.Type.INT);
                query = new CustomScoreQuery(query, downloadCountBooster);
            }

            var   filterTerm  = searchFilter.IncludePrerelease ? "IsLatest" : "IsLatestStable";
            Query filterQuery = new TermQuery(new Term(filterTerm, Boolean.TrueString));

            Filter filter  = new QueryWrapperFilter(filterQuery);
            var    results = searcher.Search(query, filter: filter, n: numRecords, sort: new Sort(GetSortField(searchFilter)));

            if (results.TotalHits == 0 || searchFilter.CountOnly)
            {
                return(new SearchResults(results.TotalHits, timestamp));
            }

            var packages = results.ScoreDocs
                           .Skip(searchFilter.Skip)
                           .Select(sd => PackageFromDoc(searcher.Doc(sd.Doc)))
                           .ToList();

            return(new SearchResults(
                       results.TotalHits,
                       timestamp,
                       packages.AsQueryable()));
        }
示例#7
0
        public virtual void TestRewrite()
        {
            IndexReader   r = DirectoryReader.Open(dir);
            IndexSearcher s = NewSearcher(r);

            Query            q         = new TermQuery(new Term(TEXT_FIELD, "first"));
            CustomScoreQuery original  = new CustomScoreQuery(q);
            CustomScoreQuery rewritten = (CustomScoreQuery)original.Rewrite(s.IndexReader);

            assertTrue("rewritten query should be identical, as TermQuery does not rewrite", original == rewritten);
            assertTrue("no hits for query", s.Search(rewritten, 1).TotalHits > 0);
            assertEquals(s.Search(q, 1).TotalHits, s.Search(rewritten, 1).TotalHits);

            q         = new TermRangeQuery(TEXT_FIELD, null, null, true, true); // everything
            original  = new CustomScoreQuery(q);
            rewritten = (CustomScoreQuery)original.Rewrite(s.IndexReader);
            assertTrue("rewritten query should not be identical, as TermRangeQuery rewrites", original != rewritten);
            assertTrue("no hits for query", s.Search(rewritten, 1).TotalHits > 0);
            assertEquals(s.Search(q, 1).TotalHits, s.Search(original, 1).TotalHits);
            assertEquals(s.Search(q, 1).TotalHits, s.Search(rewritten, 1).TotalHits);

            r.Dispose();
        }
示例#8
0
        private void DoTestCustomScore(ValueSource valueSource, double dboost)
        {
            float         boost         = (float)dboost;
            FunctionQuery functionQuery = new FunctionQuery(valueSource);
            IndexReader   r             = DirectoryReader.Open(dir);
            IndexSearcher s             = NewSearcher(r);

            // regular (boolean) query.
            BooleanQuery q1 = new BooleanQuery();

            q1.Add(new TermQuery(new Term(TEXT_FIELD, "first")), BooleanClause.Occur.SHOULD);
            q1.Add(new TermQuery(new Term(TEXT_FIELD, "aid")), BooleanClause.Occur.SHOULD);
            q1.Add(new TermQuery(new Term(TEXT_FIELD, "text")), BooleanClause.Occur.SHOULD);
            Log(q1);

            // custom query, that should score the same as q1.
            BooleanQuery q2CustomNeutral      = new BooleanQuery(true);
            Query        q2CustomNeutralInner = new CustomScoreQuery(q1);

            q2CustomNeutral.Add(q2CustomNeutralInner, BooleanClause.Occur.SHOULD);
            // a little tricky: we split the boost across an outer BQ and CustomScoreQuery
            // this ensures boosting is correct across all these functions (see LUCENE-4935)
            q2CustomNeutral.Boost      = (float)Math.Sqrt(dboost);
            q2CustomNeutralInner.Boost = (float)Math.Sqrt(dboost);
            Log(q2CustomNeutral);

            // custom query, that should (by default) multiply the scores of q1 by that of the field
            CustomScoreQuery q3CustomMul = new CustomScoreQuery(q1, functionQuery);

            q3CustomMul.Strict = true;
            q3CustomMul.Boost  = boost;
            Log(q3CustomMul);

            // custom query, that should add the scores of q1 to that of the field
            CustomScoreQuery q4CustomAdd = new CustomAddQuery(q1, functionQuery);

            q4CustomAdd.Strict = true;
            q4CustomAdd.Boost  = boost;
            Log(q4CustomAdd);

            // custom query, that multiplies and adds the field score to that of q1
            CustomScoreQuery q5CustomMulAdd = new CustomMulAddQuery(q1, functionQuery, functionQuery);

            q5CustomMulAdd.Strict = true;
            q5CustomMulAdd.Boost  = boost;
            Log(q5CustomMulAdd);

            // do al the searches
            TopDocs td1 = s.Search(q1, null, 1000);
            TopDocs td2CustomNeutral = s.Search(q2CustomNeutral, null, 1000);
            TopDocs td3CustomMul     = s.Search(q3CustomMul, null, 1000);
            TopDocs td4CustomAdd     = s.Search(q4CustomAdd, null, 1000);
            TopDocs td5CustomMulAdd  = s.Search(q5CustomMulAdd, null, 1000);

            // put results in map so we can verify the scores although they have changed
            IDictionary <int, float> h1 = TopDocsToMap(td1);
            IDictionary <int, float> h2CustomNeutral = TopDocsToMap(td2CustomNeutral);
            IDictionary <int, float> h3CustomMul     = TopDocsToMap(td3CustomMul);
            IDictionary <int, float> h4CustomAdd     = TopDocsToMap(td4CustomAdd);
            IDictionary <int, float> h5CustomMulAdd  = TopDocsToMap(td5CustomMulAdd);

            VerifyResults(boost, s, h1, h2CustomNeutral, h3CustomMul, h4CustomAdd, h5CustomMulAdd, q1, q2CustomNeutral, q3CustomMul, q4CustomAdd, q5CustomMulAdd);
            r.Dispose();
        }
示例#9
0
 private static void VisitQuery(CustomScoreQuery query, AzureQueryLogger.IndentedTextWriter writer)
 {
     writer.WriteLine("IsStrict: {0}", (query.IsStrict() ? 1 : 0));
     writer.WriteLine("Name: {0}", (object)query.Name());
 }
示例#10
0
 public virtual Query VisitCustomScoreQuery(CustomScoreQuery customScoreq)
 {
     throw new NotImplementedException();
 }
示例#11
0
 public virtual Query VisitCustomScoreQuery(CustomScoreQuery customScoreq)
 {
     throw new SnNotSupportedException();
 }