Example #1
0
        public void elasticSearch() {


            var uri = new Uri("http://localhost:9200");
            var settings = new ConnectionSettings(uri).SetDefaultIndex("contacts");
            var client = new ElasticClient(settings);

            if (client.IndexExists("contacts").Exists)
            {

                UpsertContact(client, new Contacts(1,"Andrew Kagwa", "Uganda"));
                Console.WriteLine("Indexing Successfull");
            }
            else
            {

            }



            Console.ReadKey();

            QueryContainer query = new TermQuery
            {
                Field = "Name",
                Value = "Andrew",
                 
                
            };

            var searchReq = new SearchRequest
            {

                From = 0,
                Size = 10,
                Query = query,
            };

            var result = client.Search<Contacts>(searchReq);



        }
		public MoreLikeThisRequestTests()
		{
			QueryContainer query = new TermQuery()
			{
				Field = Property.Path<ElasticsearchProject>(p=>p.Name),
				Value = "value"
			} && new PrefixQuery()
			{
				Field = "prefix_field", 
				Value = "prefi", 
				Rewrite = RewriteMultiTerm.ConstantScoreBoolean
			};
			var search = new SearchRequest
			{
				From = 0,
				Size = 20,
				Query = query,
				Filter = new FilterContainer(new BoolFilter
				{
					Cache = true,
					Must = new FilterContainer[]
					{
						new TermFilter { Field = "value", Value = "asdasd"}
					}
				}),
				TrackScores = true,
				Explain = true,
				Sort = new List<KeyValuePair<PropertyPathMarker, ISort>>()
				{
					new KeyValuePair<PropertyPathMarker, ISort>("field", new Sort { Order = SortOrder.Ascending, Missing = "_first"})
				}
			};
			var request = new MoreLikeThisRequest("some-index", "the-type","document-id-21")
			{
				Search = search,
				MaxDocFreq = 2

			};
			var response = this._client.MoreLikeThis<ElasticsearchProject>(request);
			this._status = response.ConnectionStatus;
		}
		public SearchRequestUntypedTests()
		{
			QueryContainer query = new TermQuery()
			{
				Field = Property.Path<ElasticsearchProject>(p=>p.Name),
				Value = "value"
			} && new PrefixQuery()
			{
				Field = "prefix_field", 
				Value = "prefi", 
				Rewrite = RewriteMultiTerm.ConstantScoreBoolean
			};

			var request = new SearchRequest
			{
				From = 0,
				Size = 20,
				Query = query
				
			};
			var response = this._client.Search<ElasticsearchProject>(request);
			this._status = response.ConnectionStatus;
		}
		public CountRequestTests()
		{
			QueryContainer query = new TermQuery()
			{
				Field = Property.Path<ElasticsearchProject>(p=>p.Name),
				Value = "value"
			} && new PrefixQuery()
			{
				Field = "prefix_field", 
				Value = "prefi", 
				Rewrite = RewriteMultiTerm.ConstantScoreBoolean
			};

			var request = new CountRequest()
			{
				AllowNoIndices = true,
				ExpandWildcards = ExpandWildcards.Closed,
				MinScore = 0.6,
				Query = query
			};
			var response = this._client.Count<ElasticsearchProject>(request);
			this._status = response.ConnectionStatus;
		}
		public DeleteByQueryRequestTests()
		{
			QueryContainer query = new TermQuery()
			{
				Field = Property.Path<ElasticsearchProject>(p=>p.Name),
				Value = "value"
			} && new PrefixQuery()
			{
				Field = "prefix_field", 
				Value = "prefi", 
				Rewrite = RewriteMultiTerm.ConstantScoreBoolean
			};

			var request = new DeleteByQueryRequest()
			{
				AllIndices = true,
				AllowNoIndices = true,
				ExpandWildcards = ExpandWildcards.Closed,
				Query = query,
				DefaultOperator = DefaultOperator.And
			};
			var response = this._client.DeleteByQuery(request);
			this._status = response.ConnectionStatus;
		}
Example #6
0
        public void TestMaxSizeEndHighlight()
        {
            var helper = new TestHighlightRunner();
            helper.TestAction = () =>
                {
                    var stopWords = Support.Compatibility.SetFactory.CreateHashSet(new[] {"in", "it"});
                    TermQuery query = new TermQuery(new Term("text", "searchterm"));

                    String text = "this is a text with searchterm in it";
                    SimpleHTMLFormatter fm = new SimpleHTMLFormatter();
                    Highlighter hg = helper.GetHighlighter(query, "text",
                                                           new StandardAnalyzer(TEST_VERSION,
                                                                                stopWords).
                                                               TokenStream("text",
                                                                           new StringReader(text)),
                                                           fm);
                    hg.TextFragmenter = new NullFragmenter();
                    hg.MaxDocCharsToAnalyze = 36;
                    String match = hg.GetBestFragment(new StandardAnalyzer(TEST_VERSION, stopWords),
                                                      "text", text);
                    Assert.IsTrue(match.EndsWith("in it"),
                                  "Matched text should contain remainder of text after highlighted query ");
                };
            helper.Start();
        }
        public virtual void TestTwoFieldsTwoFormats()
        {
            Analyzer analyzer = new MockAnalyzer(Random());

            Directory directory = NewDirectory();
            // we don't use RandomIndexWriter because it might add more docvalues than we expect !!!!1
            IndexWriterConfig iwc = NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);
            DocValuesFormat fast = DocValuesFormat.ForName("Lucene45");
            DocValuesFormat slow = DocValuesFormat.ForName("Lucene45");
            iwc.SetCodec(new Lucene46CodecAnonymousInnerClassHelper(this, fast, slow));
            IndexWriter iwriter = new IndexWriter(directory, iwc);
            Document doc = new Document();
            string longTerm = "longtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongterm";
            string text = "this is the text to be indexed. " + longTerm;
            doc.Add(NewTextField("fieldname", text, Field.Store.YES));
            doc.Add(new NumericDocValuesField("dv1", 5));
            doc.Add(new BinaryDocValuesField("dv2", new BytesRef("hello world")));
            iwriter.AddDocument(doc);
            iwriter.Dispose();

            // Now search the index:
            IndexReader ireader = DirectoryReader.Open(directory); // read-only=true
            IndexSearcher isearcher = NewSearcher(ireader);

            Assert.AreEqual(1, isearcher.Search(new TermQuery(new Term("fieldname", longTerm)), 1).TotalHits);
            Query query = new TermQuery(new Term("fieldname", "text"));
            TopDocs hits = isearcher.Search(query, null, 1);
            Assert.AreEqual(1, hits.TotalHits);
            BytesRef scratch = new BytesRef();
            // Iterate through the results:
            for (int i = 0; i < hits.ScoreDocs.Length; i++)
            {
                Document hitDoc = isearcher.Doc(hits.ScoreDocs[i].Doc);
                Assert.AreEqual(text, hitDoc.Get("fieldname"));
                Debug.Assert(ireader.Leaves.Count == 1);
                NumericDocValues dv = ((AtomicReader)ireader.Leaves[0].Reader).GetNumericDocValues("dv1");
                Assert.AreEqual(5, dv.Get(hits.ScoreDocs[i].Doc));
                BinaryDocValues dv2 = ((AtomicReader)ireader.Leaves[0].Reader).GetBinaryDocValues("dv2");
                dv2.Get(hits.ScoreDocs[i].Doc, scratch);
                Assert.AreEqual(new BytesRef("hello world"), scratch);
            }

            ireader.Dispose();
            directory.Dispose();
        }
        public virtual void TestScoring()
        {
            // verify that drill-down queries do not modify scores
            IndexSearcher searcher = NewSearcher(reader);

            float[] scores = new float[reader.MaxDoc];

            Query q = new TermQuery(new Term("content", "foo"));
            TopDocs docs = searcher.Search(q, reader.MaxDoc); // fetch all available docs to this query
            foreach (ScoreDoc sd in docs.ScoreDocs)
            {
                scores[sd.Doc] = sd.Score;
            }

            // create a drill-down query with category "a", scores should not change
            DrillDownQuery q2 = new DrillDownQuery(config, q);
            q2.Add("a");
            docs = searcher.Search(q2, reader.MaxDoc); // fetch all available docs to this query
            foreach (ScoreDoc sd in docs.ScoreDocs)
            {
                Assert.AreEqual(scores[sd.Doc], sd.Score, 0f, "score of doc=" + sd.Doc + " modified");
            }
        }
Example #9
0
 public virtual void TestTerm2()
 {
     Query query = new TermQuery(new Term("field", "seventish"));
     CheckHits(query, new int[] { });
 }
Example #10
0
        /// <summary>
        /// Retrieve suggestions, specifying whether all terms
        ///  must match (<paramref name="allTermsRequired"/>) and whether the hits
        ///  should be highlighted (<paramref name="doHighlight"/>).
        /// </summary>
        public virtual IList <LookupResult> DoLookup(string key, IEnumerable <BytesRef> contexts, int num, bool allTermsRequired, bool doHighlight)
        {
            if (m_searcherMgr == null)
            {
                throw IllegalStateException.Create("suggester was not built");
            }

            Occur occur;

            if (allTermsRequired)
            {
                occur = Occur.MUST;
            }
            else
            {
                occur = Occur.SHOULD;
            }

            TokenStream  ts = null;
            BooleanQuery query;
            var          matchedTokens = new JCG.HashSet <string>();
            string       prefixToken   = null;

            try
            {
                ts = m_queryAnalyzer.GetTokenStream("", new StringReader(key));

                //long t0 = J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond; // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results
                ts.Reset();
                var    termAtt   = ts.AddAttribute <ICharTermAttribute>();
                var    offsetAtt = ts.AddAttribute <IOffsetAttribute>();
                string lastToken = null;
                query = new BooleanQuery();
                int maxEndOffset = -1;
                matchedTokens = new JCG.HashSet <string>();
                while (ts.IncrementToken())
                {
                    if (lastToken != null)
                    {
                        matchedTokens.Add(lastToken);
                        query.Add(new TermQuery(new Term(TEXT_FIELD_NAME, lastToken)), occur);
                    }
                    lastToken = termAtt.ToString();
                    if (lastToken != null)
                    {
                        maxEndOffset = Math.Max(maxEndOffset, offsetAtt.EndOffset);
                    }
                }
                ts.End();

                if (lastToken != null)
                {
                    Query lastQuery;
                    if (maxEndOffset == offsetAtt.EndOffset)
                    {
                        // Use PrefixQuery (or the ngram equivalent) when
                        // there was no trailing discarded chars in the
                        // string (e.g. whitespace), so that if query does
                        // not end with a space we show prefix matches for
                        // that token:
                        lastQuery   = GetLastTokenQuery(lastToken);
                        prefixToken = lastToken;
                    }
                    else
                    {
                        // Use TermQuery for an exact match if there were
                        // trailing discarded chars (e.g. whitespace), so
                        // that if query ends with a space we only show
                        // exact matches for that term:
                        matchedTokens.Add(lastToken);
                        lastQuery = new TermQuery(new Term(TEXT_FIELD_NAME, lastToken));
                    }
                    if (lastQuery != null)
                    {
                        query.Add(lastQuery, occur);
                    }
                }

                if (contexts != null)
                {
                    BooleanQuery sub = new BooleanQuery();
                    query.Add(sub, Occur.MUST);
                    foreach (BytesRef context in contexts)
                    {
                        // NOTE: we "should" wrap this in
                        // ConstantScoreQuery, or maybe send this as a
                        // Filter instead to search, but since all of
                        // these are MUST'd, the change to the score won't
                        // affect the overall ranking.  Since we indexed
                        // as DOCS_ONLY, the perf should be the same
                        // either way (no freq int[] blocks to decode):

                        // TODO: if we had a BinaryTermField we could fix
                        // this "must be valid ut8f" limitation:
                        sub.Add(new TermQuery(new Term(CONTEXTS_FIELD_NAME, context.Utf8ToString())), Occur.SHOULD);
                    }
                }
            }
            finally
            {
                IOUtils.DisposeWhileHandlingException(ts);
            }

            // TODO: we could allow blended sort here, combining
            // weight w/ score.  Now we ignore score and sort only
            // by weight:

            Query finalQuery = FinishQuery(query, allTermsRequired);

            //System.out.println("finalQuery=" + query);

            // Sort by weight, descending:
            TopFieldCollector c = TopFieldCollector.Create(SORT, num, true, false, false, false);

            // We sorted postings by weight during indexing, so we
            // only retrieve the first num hits now:
            ICollector           c2       = new EarlyTerminatingSortingCollector(c, SORT, num);
            IndexSearcher        searcher = m_searcherMgr.Acquire();
            IList <LookupResult> results  = null;

            try
            {
                //System.out.println("got searcher=" + searcher);
                searcher.Search(finalQuery, c2);

                TopFieldDocs hits = (TopFieldDocs)c.GetTopDocs();

                // Slower way if postings are not pre-sorted by weight:
                // hits = searcher.search(query, null, num, SORT);
                results = CreateResults(searcher, hits, num, key, doHighlight, matchedTokens, prefixToken);
            }
            finally
            {
                m_searcherMgr.Release(searcher);
            }

            //System.out.println(((J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond) - t0) + " msec for infix suggest"); // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results
            //System.out.println(results);

            return(results);
        }
        public virtual void TestDisableFuzziness()
        {
            Query expected = new TermQuery(new Term("field", "foo~1"));

            assertEquals(expected, ParseKeyword("foo~1", ~Operator.FUZZY_OPERATOR));
        }
            public override void Run()
            {
                try
                {
                    IndexReader lastReader = null;
                    IndexSearcher lastSearcher = null;

                    while (Operations.DecrementAndGet() >= 0)
                    {
                        // bias toward a recently changed doc
                        int id = rand.Next(100) < 25 ? OuterInstance.LastId : rand.Next(Ndocs);

                        // when indexing, we update the index, then the model
                        // so when querying, we should first check the model, and then the index

                        long val;
                        DirectoryReader r;
                        lock (OuterInstance)
                        {
                            val = OuterInstance.CommittedModel[id];
                            r = OuterInstance.Reader;
                            r.IncRef();
                        }

                        if (VERBOSE)
                        {
                            Console.WriteLine("TEST: " + Thread.CurrentThread.Name + ": s id=" + id + " val=" + val + " r=" + r.Version);
                        }

                        //  sreq = req("wt","json", "q","id:"+Integer.toString(id), "omitHeader","true");
                        IndexSearcher searcher;
                        if (r == lastReader)
                        {
                            // Just re-use lastSearcher, else
                            // newSearcher may create too many thread
                            // pools (ExecutorService):
                            searcher = lastSearcher;
                        }
                        else
                        {
                            searcher = OuterInstance.NewSearcher(r);
                            lastReader = r;
                            lastSearcher = searcher;
                        }
                        Query q = new TermQuery(new Term("id", Convert.ToString(id)));
                        TopDocs results = searcher.Search(q, 10);

                        if (results.TotalHits == 0 && Tombstones)
                        {
                            // if we couldn't find the doc, look for its tombstone
                            q = new TermQuery(new Term("id", "-" + Convert.ToString(id)));
                            results = searcher.Search(q, 1);
                            if (results.TotalHits == 0)
                            {
                                if (val == -1L)
                                {
                                    // expected... no doc was added yet
                                    r.DecRef();
                                    continue;
                                }
                                Assert.Fail("No documents or tombstones found for id " + id + ", expected at least " + val + " reader=" + r);
                            }
                        }

                        if (results.TotalHits == 0 && !Tombstones)
                        {
                            // nothing to do - we can't tell anything from a deleted doc without tombstones
                        }
                        else
                        {
                            // we should have found the document, or its tombstone
                            if (results.TotalHits != 1)
                            {
                                Console.WriteLine("FAIL: hits id:" + id + " val=" + val);
                                foreach (ScoreDoc sd in results.ScoreDocs)
                                {
                                    Document doc = r.Document(sd.Doc);
                                    Console.WriteLine("  docID=" + sd.Doc + " id:" + doc.Get("id") + " foundVal=" + doc.Get(OuterInstance.Field));
                                }
                                Assert.Fail("id=" + id + " reader=" + r + " totalHits=" + results.TotalHits);
                            }
                            Document doc_ = searcher.Doc(results.ScoreDocs[0].Doc);
                            long foundVal = Convert.ToInt64(doc_.Get(OuterInstance.Field));
                            if (foundVal < Math.Abs(val))
                            {
                                Assert.Fail("foundVal=" + foundVal + " val=" + val + " id=" + id + " reader=" + r);
                            }
                        }

                        r.DecRef();
                    }
                }
                catch (Exception e)
                {
                    Operations.Set((int)-1L);
                    Console.WriteLine(Thread.CurrentThread.Name + ": FAILED: unexpected exception");
                    Console.WriteLine(e.StackTrace);
                    throw new Exception(e.Message, e);
                }
            }
        public virtual void TestHeartRanking()
        {
            AssumeFalse("PreFlex codec does not support the stats necessary for this test!", "Lucene3x".Equals(Codec.Default.Name));

            Query q = new TermQuery(new Term(FIELD_BODY, "heart"));

            foreach (SimilarityBase sim in Sims)
            {
                Searcher.Similarity = sim;
                TopDocs topDocs = Searcher.Search(q, 1000);
                Assert.AreEqual("2", Reader.Document(topDocs.ScoreDocs[0].Doc).Get(FIELD_ID), "Failed: " + sim.ToString());
            }
        }
        public virtual void TestHeartList()
        {
            Query q = new TermQuery(new Term(FIELD_BODY, "heart"));

            foreach (SimilarityBase sim in Sims)
            {
                Searcher.Similarity = sim;
                TopDocs topDocs = Searcher.Search(q, 1000);
                Assert.AreEqual(3, topDocs.TotalHits, "Failed: " + sim.ToString());
            }
        }
        /// <summary>
        /// Adds a standard type clause to this instance
        /// </summary>
        /// <param name="term">Term to add to this query.</param>
        /// <param name="occurrence">Defines how the term is added to this query.</param>
        /// <param name="slop">The amount of allowed slop in a phrase query.</param>
        /// <remarks>
        /// Slop is the amount of movement each word is allowed in a non-exact phrase query.
        /// For instance if you search for "Adobe Systems Incorporated" and the slop is set to 0 then
        /// only results with that term is allowed. If you set the slop to 2 then two movements can be
        /// made, max, for each word. In the same example with slop set to 2 results would be returned 
        /// for "Adobe Systems Incorporated", "Adobe Incorporated Systems", "Systems Adobe Incorporated",
        /// and "Systems Incorporated Adobe". 
        /// </remarks>
        public void AddBooleanClause(SearchTerm term, ClauseOccurrence occurrence, int slop)
        {
            if (term == null)
                throw new ArgumentNullException("term", "term cannot be null");
            IncrementTotalClauses(1);

            if (term.IsPhrase) {
                PhraseQuery phraseQuery = new PhraseQuery();
                phraseQuery.Add(term.GetLuceneTerm());
                phraseQuery.SetSlop(slop);
                phraseQuery.SetBoost(term.Boost);
                this.luceneQuery.Add(phraseQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence));
                phraseQuery = null;
            }
            else {
                TermQuery termQuery = new TermQuery(term.GetLuceneTerm());
                termQuery.SetBoost(term.Boost);
                this.luceneQuery.Add(termQuery, TypeConverter.ConvertToLuceneClauseOccurrence(occurrence));
                termQuery = null;
            }
        }
Example #16
0
        public void TestOffByOne()
        {
            var helper = new TestHighlightRunner();
            helper.TestAction = () =>
                                    {
                                        TermQuery query = new TermQuery(new Term("data", "help"));
                                        Highlighter hg = new Highlighter(new SimpleHTMLFormatter(),
                                                                         new QueryTermScorer(query));
                                        hg.TextFragmenter = new NullFragmenter();

                                        String match = null;
                                        match = hg.GetBestFragment(analyzer, "data", "help me [54-65]");
                                        Assert.AreEqual(match, "<B>help</B> me [54-65]");
                                    };

            helper.Start();
        }
        public virtual void TestDisablePhrase()
        {
            Query expected = new TermQuery(new Term("field", "\"test\""));

            assertEquals(expected, ParseKeyword("\"test\"", ~Operator.PHRASE_OPERATOR));
        }
Example #18
0
        public virtual void TestGetValuesForIndexedDocument()
        {
            Directory dir = NewDirectory();
            RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, Similarity, TimeZone);
            writer.AddDocument(MakeDocumentWithFields());
            IndexReader reader = writer.Reader;

            IndexSearcher searcher = NewSearcher(reader);

            // search for something that does exists
            Query query = new TermQuery(new Term("keyword", "test1"));

            // ensure that queries return expected results without DateFilter first
            ScoreDoc[] hits = searcher.Search(query, null, 1000).ScoreDocs;
            Assert.AreEqual(1, hits.Length);

            DoAssert(searcher.Doc(hits[0].Doc), true);
            writer.Dispose();
            reader.Dispose();
            dir.Dispose();
        }
        public virtual void TestDisableNOT()
        {
            Query expected = new TermQuery(new Term("field", "-foo"));

            assertEquals(expected, ParseKeyword("-foo", ~Operator.NOT_OPERATOR));
        }
Example #20
0
        public virtual void TestFieldSetValue()
        {
            Field field = new StringField("id", "id1", Field.Store.YES);
            Documents.Document doc = new Documents.Document();
            doc.Add(field);
            doc.Add(new StringField("keyword", "test", Field.Store.YES));

            Directory dir = NewDirectory();
            RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, Similarity, TimeZone);
            writer.AddDocument(doc);
            field.StringValue = "id2";
            writer.AddDocument(doc);
            field.StringValue = "id3";
            writer.AddDocument(doc);

            IndexReader reader = writer.Reader;
            IndexSearcher searcher = NewSearcher(reader);

            Query query = new TermQuery(new Term("keyword", "test"));

            // ensure that queries return expected results without DateFilter first
            ScoreDoc[] hits = searcher.Search(query, null, 1000).ScoreDocs;
            Assert.AreEqual(3, hits.Length);
            int result = 0;
            for (int i = 0; i < 3; i++)
            {
                Documents.Document doc2 = searcher.Doc(hits[i].Doc);
                Field f = (Field)doc2.GetField("id");
                if (f.StringValue.Equals("id1"))
                {
                    result |= 1;
                }
                else if (f.StringValue.Equals("id2"))
                {
                    result |= 2;
                }
                else if (f.StringValue.Equals("id3"))
                {
                    result |= 4;
                }
                else
                {
                    Assert.Fail("unexpected id field");
                }
            }
            writer.Dispose();
            reader.Dispose();
            dir.Dispose();
            Assert.AreEqual(7, result, "did not see all IDs");
        }
Example #21
0
        private static Query BuildGeneralQuery(
            bool doExactId,
            string originalSearchText,
            Analyzer analyzer,
            IEnumerable <NuGetSearchTerm> generalTerms,
            IEnumerable <Query> generalQueries)
        {
            // All terms in the multi-term query appear in at least one of the target fields.
            var conjuctionQuery = new BooleanQuery();

            conjuctionQuery.Boost = 2.0f;

            // Some terms in the multi-term query appear in at least one of the target fields.
            var disjunctionQuery = new BooleanQuery();

            disjunctionQuery.Boost = 0.1f;

            // Suffix wildcard search e.g. jquer*
            var wildCardQuery = new BooleanQuery();

            wildCardQuery.Boost = 0.5f;

            string escapedExactId = originalSearchText.ToLowerInvariant();

            Query exactIdQuery    = null;
            Query wildCardIdQuery = null;

            if (doExactId)
            {
                exactIdQuery       = new TermQuery(new Term("Id-Exact", escapedExactId));
                exactIdQuery.Boost = 7.5f;

                wildCardIdQuery = new WildcardQuery(new Term("Id-Exact", "*" + escapedExactId + "*"));
            }

            Query nearlyExactIdQuery = null;

            if (generalTerms.Any())
            {
                string escapedApproximateId = string.Join(" ", generalTerms.Select(c => c.TermOrPhrase));
                nearlyExactIdQuery       = AnalysisHelper.GetFieldQuery(analyzer, "Id", escapedApproximateId);
                nearlyExactIdQuery.Boost = 2.0f;
            }

            foreach (var termQuery in generalQueries)
            {
                conjuctionQuery.Add(termQuery, Occur.MUST);
                disjunctionQuery.Add(termQuery, Occur.SHOULD);
            }

            var sanitizedTerms = generalTerms.Select(c => c.TermOrPhrase.ToLowerInvariant());

            foreach (var sanitizedTerm in sanitizedTerms)
            {
                foreach (var field in Fields)
                {
                    var wildCardTermQuery = new WildcardQuery(new Term(field, sanitizedTerm + "*"));
                    wildCardTermQuery.Boost = 0.7f;
                    wildCardQuery.Add(wildCardTermQuery, Occur.SHOULD);
                }
            }

            // OR of all the applicable queries
            var queries = new Query[]
            {
                exactIdQuery, wildCardIdQuery, nearlyExactIdQuery, conjuctionQuery, disjunctionQuery, wildCardQuery
            };

            var queriesToCombine = queries.Where(q => !IsDegenerateQuery(q));
            var query            = conjuctionQuery.Combine(queriesToCombine.ToArray());

            return(query);
        }
Example #22
0
        public FeedItem GetItem(string FeedItemId, long?ItemId, bool DirectRequest)
        {
            using (IndexSearcher _searcher = new IndexSearcher(_directory, false))
            {
                var       item = new FeedItem();
                TermQuery TermQuery;
                TermQuery = new TermQuery(new Term("FeedItemId", FeedItemId));
                var doc = _searcher.Search(TermQuery, 1).ScoreDocs.FirstOrDefault();
                if (doc != null)
                {
                    var tempdoc = _searcher.Doc(doc.Doc);
                    item.Id = Guid.Parse(tempdoc.Get("FeedItemId"));
                    //long temp = 0;
                    //if (long.TryParse(tempdoc.Get("ItemId"), out temp))
                    //    item.ItemId = temp;
                    item.Title = tempdoc.Get("Title");
                    //item.Description = tempdoc.Get("Description");
                    item.SiteTitle = tempdoc.Get("SiteTitle");
                    item.SiteUrl   = tempdoc.Get("SiteUrl");
                    //item.SiteId = long.Parse(tempdoc.Get("SiteId"));
                    item.PubDate         = DateTime.ParseExact(tempdoc.Get("PubDate"), "yyyyMMddHHmm", System.Globalization.CultureInfo.InvariantCulture);
                    item.Link            = tempdoc.Get("Link");
                    item.ShowContentType = !string.IsNullOrEmpty(tempdoc.Get("ShowContentType")) ? (ShowContent)Enum.Parse(typeof(ShowContent), tempdoc.Get("ShowContentType")) : ShowContent.Inner;
                    //try
                    //{
                    //    if (!string.IsNullOrEmpty(tempdoc.Get("Cats")))
                    //        item.Cats = tempdoc.Get("Cats").Split(' ').Select(c => int.Parse(c)).ToList();
                    //}
                    //catch
                    //{
                    //    if (!string.IsNullOrEmpty(tempdoc.Get("Cats")))
                    //        item.Cats = tempdoc.Get("Cats").Split(',').Select(c => int.Parse(c)).ToList();
                    //}
                }
                //else
                //{
                //    TazehaContext context = new TazehaContext();
                //    var items = context.ItemVisiteds.Where(i => i.FeedItemId == FeedItemId && !string.IsNullOrEmpty(i.Link)).
                //        Select(x => new FeedItem { FeedItemId = x.FeedItemId, Title = x.Title, Description = x.Title, Link = x.Link });
                //    if (items.Any())
                //        item = items.First();
                //    if (string.IsNullOrEmpty(item.SiteUrl))
                //        item.SiteUrl = "v";
                //}

                //if (DirectRequest && item != null)
                //if (!string.IsNullOrEmpty(HttpContext.Current.Request.UserAgent) && !HttpContext.Current.Request.UserAgent.Contains("bot"))
                //    IncreaseVisitCount(new ItemVisited()
                //    {
                //        CreationDate = item.CreateDate.Year > 1000 ? item.CreateDate : (item.PubDate.HasValue ? item.PubDate.Value : DateTime.Now),
                //        EntityCode = string.Empty,
                //        EntityRef = 0,
                //        FeedItemId = item.FeedItemId,
                //        Title = item.Title,
                //        VisitCount = 1,
                //        PubDate = item.PubDate,
                //        Link = item.Link,
                //        SiteId = item.SiteId,
                //        FeedId = item.FeedId,
                //    });
                return(item);
            }
        }
Example #23
0
        public virtual void TestTerm2()
        {
            Query query = new TermQuery(new Term("field", "seventish"));

            CheckHits(query, new int[] { });
        }
Example #24
0
        public List <Guid> Search(string searchQuery, int PageSize, int PageIndex, LuceneSearcherType SearchType, LuceneSortField SortField, bool hasPhoto)
        {
            try
            {
                var photoQuery = new TermQuery(new Term("HasPhoto", hasPhoto.ToString()));
                var results    = new List <ScoreDoc>();
                var res        = new List <Guid>();
                // validation
                if (string.IsNullOrEmpty(searchQuery) || string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", "")))
                {
                    return(new List <Guid>());
                }
                var Content = searchQuery.Trim();

                if (SortField != LuceneSortField.PubDate)
                {
                    sort = new Sort(new SortField(SortField.ToString(), System.Globalization.CultureInfo.CurrentCulture, true));
                }

                // set up lucene searcher
                //using (var _searcher = new IndexSearcher(_directory, false))
                //{
                var analyzer = new StandardAnalyzer(Currentversion);

                if (SearchType == LuceneSearcherType.Key)
                {
                    if (Content.Contains("|"))
                    {
                        var fields      = new[] { "Title", "Description" };
                        var queryParser = new MultiFieldQueryParser(Currentversion, fields, analyzer);
                        //var query = queryParser.Parse(Content.Replace('|', ' '));
                        var          query     = queryParser.Parse(Content.Replace(" ", "+").Replace('|', ' ') + (hasPhoto ? ",HasPhoto:True" : ""));
                        BooleanQuery innerExpr = new BooleanQuery();
                        innerExpr.Add(query, Occur.MUST);
                        if (hasPhoto)
                        {
                            innerExpr.Add(photoQuery, Occur.MUST);
                        }

                        results = _searcher.Search(innerExpr, null, (PageIndex + 1) * PageSize, sort).ScoreDocs.ToList();
                        if (results.Count < PageSize)
                        {
                            QueryParser oParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Title", analyzer);
                            string      desc = "", finalQuery = "";
                            desc       = " OR (Description:" + Content + ")";
                            finalQuery = "(" + Content + desc + ")";
                            query      = oParser.Parse(finalQuery);
                            results    = _searcher.Search(query, null, (PageIndex + 1) * PageSize, Sort.RELEVANCE).ScoreDocs.ToList();
                        }
                    }
                    else if (Content.Contains(" "))
                    {
                        var fields      = new[] { "Title", "Description" };
                        var queryParser = new MultiFieldQueryParser(Currentversion, fields, analyzer);
                        //var query = queryParser.Parse("\"" + Content + "\"");
                        var          query     = queryParser.Parse("\"" + Content + "\"");
                        BooleanQuery innerExpr = new BooleanQuery();
                        innerExpr.Add(query, Occur.MUST);
                        if (hasPhoto)
                        {
                            innerExpr.Add(photoQuery, Occur.MUST);
                        }

                        results = _searcher.Search(innerExpr, null, (PageIndex + 1) * PageSize, sort).ScoreDocs.ToList();
                        if (results.Count < PageSize)
                        {
                            QueryParser oParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Title", analyzer);
                            string      desc = "", finalQuery = "";
                            desc       = " OR (Description:" + Content + ")";
                            finalQuery = "(" + Content + desc + ")";
                            query      = oParser.Parse(finalQuery);
                            results    = _searcher.Search(query, null, (PageIndex + 1) * PageSize, Sort.RELEVANCE).ScoreDocs.ToList();
                        }
                    }
                    else
                    {
                        var          query     = new PrefixQuery(new Term("Title", Content.ToString()));
                        BooleanQuery innerExpr = new BooleanQuery();
                        innerExpr.Add(query, Occur.MUST);
                        if (hasPhoto)
                        {
                            innerExpr.Add(photoQuery, Occur.MUST);
                        }

                        results = _searcher.Search(innerExpr, null, (PageIndex + 1) * PageSize, sort).ScoreDocs.ToList();
                        //if (results.Count < (PageIndex + 1) * PageSize)
                        //    results = _searcher.Search(new PrefixQuery(new Term("Description", Content.ToString())), null, (PageIndex + 1) * PageSize, sort).ScoreDocs.ToList();
                    }
                }
                var resultsId = results.Skip(PageIndex * PageSize).Take(PageSize);
                foreach (var doc in resultsId)
                {
                    var tempdoc = _searcher.Doc(doc.Doc);
                    res.Add(Guid.Parse(tempdoc.Get("FeedItemId")));
                }

                return(res);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("write.lock"))
                {
                    try
                    {
                        var lockFilePath = Path.Combine(LuceneDir, "write.lock");
                        if (File.Exists(lockFilePath))
                        {
                            File.Delete(lockFilePath);
                        }
                    }
                    catch { }
                }
                return(new List <Guid>());
            }
        }
        public virtual void TestDifferentNumResults()
        {
            // test the collector w/ FacetRequests and different numResults
            DirectoryReader indexReader = DirectoryReader.Open(indexDir);
            var taxoReader = new DirectoryTaxonomyReader(taxoDir);
            IndexSearcher searcher = NewSearcher(indexReader);

            FacetsCollector sfc = new FacetsCollector();
            TermQuery q = new TermQuery(A);
            searcher.Search(q, sfc);
            Facets facets = GetTaxonomyFacetCounts(taxoReader, Config, sfc);
            FacetResult result = facets.GetTopChildren(NUM_CHILDREN_CP_A, CP_A);
            Assert.AreEqual(-1, (int)result.Value);
            foreach (LabelAndValue labelValue in result.LabelValues)
            {
                Assert.AreEqual(termExpectedCounts[CP_A + "/" + labelValue.label], labelValue.value);
            }
            result = facets.GetTopChildren(NUM_CHILDREN_CP_B, CP_B);
            Assert.AreEqual(termExpectedCounts[CP_B], result.Value);
            foreach (LabelAndValue labelValue in result.LabelValues)
            {
                Assert.AreEqual(termExpectedCounts[CP_B + "/" + labelValue.label], labelValue.value);
            }

            IOUtils.Close(indexReader, taxoReader);
        }
Example #26
0
        public static List <int> Search(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return(null);
            }
            key = key.Trim().ToLower();

            var rs = new List <int>();

            try
            {
                var indexDir = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "lucene");

                var VERSION = Lucene.Net.Util.LuceneVersion.LUCENE_48;

                if (System.IO.Directory.Exists(indexDir) == true)
                {
                    var reader = DirectoryReader.Open(FSDirectory.Open(new DirectoryInfo(indexDir)));
                    var search = new IndexSearcher(reader);

                    var directory = FSDirectory.Open(new DirectoryInfo(indexDir), NoLockFactory.GetNoLockFactory());
                    var reader2   = DirectoryReader.Open(directory);
                    var search2   = new IndexSearcher(reader2);

                    //var parser = new QueryParser(VERSION, "title", new JieBaAnalyzer(TokenizerMode.Search));
                    var booleanQuery = new BooleanQuery();

                    var list = CutKeyWord(key);
                    foreach (var word in list)
                    {
                        var query1 = new TermQuery(new Term("title", word));
                        var query2 = new TermQuery(new Term("summary", word));
                        booleanQuery.Add(query1, Occur.SHOULD);
                        booleanQuery.Add(query2, Occur.SHOULD);
                    }

                    var collector = TopScoreDocCollector.Create(1000, true);
                    search2.Search(booleanQuery, null, collector);
                    var docs = collector.GetTopDocs(0, collector.TotalHits).ScoreDocs;

                    foreach (var d in docs)
                    {
                        var num      = d.Doc;
                        var document = search.Doc(num);// 拿到指定的文档

                        var articleId = document.Get("articleId");
                        //var name = document.Get("title");

                        if (int.TryParse(articleId, out int mid) == true)
                        {
                            rs.Add(mid);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"SearchMerchs ex={ex}");
            }

            return(rs);
        }
Example #27
0
        // Test using various international locales with accented characters (which
        // sort differently depending on locale)
        //
        // Copied (and slightly modified) from
        // Lucene.Net.Search.TestSort.testInternationalSort()
        //
        // TODO: this test is really fragile. there are already 3 different cases,
        // depending upon unicode version.
        public virtual void TestCollationKeySort(Analyzer usAnalyzer, Analyzer franceAnalyzer, Analyzer swedenAnalyzer, Analyzer denmarkAnalyzer, string usResult, string frResult, string svResult, string dkResult)
        {
            Directory indexStore = NewDirectory();
            IndexWriter writer = new IndexWriter(indexStore, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random(), MockTokenizer.WHITESPACE, false)));

            // document data:
            // the tracer field is used to determine which document was hit
            string[][] sortData = new string[][] { new string[] { "A", "x", "p\u00EAche", "p\u00EAche", "p\u00EAche", "p\u00EAche" }, new string[] { "B", "y", "HAT", "HAT", "HAT", "HAT" }, new string[] { "C", "x", "p\u00E9ch\u00E9", "p\u00E9ch\u00E9", "p\u00E9ch\u00E9", "p\u00E9ch\u00E9" }, new string[] { "D", "y", "HUT", "HUT", "HUT", "HUT" }, new string[] { "E", "x", "peach", "peach", "peach", "peach" }, new string[] { "F", "y", "H\u00C5T", "H\u00C5T", "H\u00C5T", "H\u00C5T" }, new string[] { "G", "x", "sin", "sin", "sin", "sin" }, new string[] { "H", "y", "H\u00D8T", "H\u00D8T", "H\u00D8T", "H\u00D8T" }, new string[] { "I", "x", "s\u00EDn", "s\u00EDn", "s\u00EDn", "s\u00EDn" }, new string[] { "J", "y", "HOT", "HOT", "HOT", "HOT" } };

            FieldType customType = new FieldType();
            customType.Stored = true;

            for (int i = 0; i < sortData.Length; ++i)
            {
                Document doc = new Document();
                doc.Add(new Field("tracer", sortData[i][0], customType));
                doc.Add(new TextField("contents", sortData[i][1], Field.Store.NO));
                if (sortData[i][2] != null)
                {
                    doc.Add(new TextField("US", usAnalyzer.TokenStream("US", new StringReader(sortData[i][2]))));
                }
                if (sortData[i][3] != null)
                {
                    doc.Add(new TextField("France", franceAnalyzer.TokenStream("France", new StringReader(sortData[i][3]))));
                }
                if (sortData[i][4] != null)
                {
                    doc.Add(new TextField("Sweden", swedenAnalyzer.TokenStream("Sweden", new StringReader(sortData[i][4]))));
                }
                if (sortData[i][5] != null)
                {
                    doc.Add(new TextField("Denmark", denmarkAnalyzer.TokenStream("Denmark", new StringReader(sortData[i][5]))));
                }
                writer.AddDocument(doc);
            }
            writer.ForceMerge(1);
            writer.Dispose();
            IndexReader reader = DirectoryReader.Open(indexStore);
            IndexSearcher searcher = new IndexSearcher(reader);

            Sort sort = new Sort();
            Query queryX = new TermQuery(new Term("contents", "x"));
            Query queryY = new TermQuery(new Term("contents", "y"));

            sort.SetSort(new SortField("US", SortField.Type_e.STRING));
            AssertMatches(searcher, queryY, sort, usResult);

            sort.SetSort(new SortField("France", SortField.Type_e.STRING));
            AssertMatches(searcher, queryX, sort, frResult);

            sort.SetSort(new SortField("Sweden", SortField.Type_e.STRING));
            AssertMatches(searcher, queryY, sort, svResult);

            sort.SetSort(new SortField("Denmark", SortField.Type_e.STRING));
            AssertMatches(searcher, queryY, sort, dkResult);
            reader.Dispose();
            indexStore.Dispose();
        }
Example #28
0
        private void ExecuteRandomJoin(bool multipleValuesPerDocument, int maxIndexIter, int maxSearchIter,
                                       int numberOfDocumentsToIndex)
        {
            for (int indexIter = 1; indexIter <= maxIndexIter; indexIter++)
            {
                if (Verbose)
                {
                    Console.WriteLine("indexIter=" + indexIter);
                }
                Directory         dir = NewDirectory();
                RandomIndexWriter w   = new RandomIndexWriter(Random, dir,
                                                              NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random, MockTokenizer.KEYWORD, false))
                                                              .SetMergePolicy(NewLogMergePolicy()));
                bool scoreDocsInOrder         = TestJoinUtil.Random.NextBoolean();
                IndexIterationContext context = CreateContext(numberOfDocumentsToIndex, w, multipleValuesPerDocument,
                                                              scoreDocsInOrder);

                IndexReader topLevelReader = w.GetReader();
                w.Dispose();
                for (int searchIter = 1; searchIter <= maxSearchIter; searchIter++)
                {
                    if (Verbose)
                    {
                        Console.WriteLine("searchIter=" + searchIter);
                    }
                    IndexSearcher indexSearcher = NewSearcher(topLevelReader);

                    int         r              = Random.Next(context.RandomUniqueValues.Length);
                    bool        from           = context.RandomFrom[r];
                    string      randomValue    = context.RandomUniqueValues[r];
                    FixedBitSet expectedResult = CreateExpectedResult(randomValue, from, indexSearcher.IndexReader,
                                                                      context);

                    Query actualQuery = new TermQuery(new Term("value", randomValue));
                    if (Verbose)
                    {
                        Console.WriteLine("actualQuery=" + actualQuery);
                    }

                    var       scoreModeLength = Enum.GetNames(typeof(ScoreMode)).Length;
                    ScoreMode scoreMode       = (ScoreMode)Random.Next(scoreModeLength);
                    if (Verbose)
                    {
                        Console.WriteLine("scoreMode=" + scoreMode);
                    }

                    Query joinQuery;
                    if (from)
                    {
                        joinQuery = JoinUtil.CreateJoinQuery("from", multipleValuesPerDocument, "to", actualQuery,
                                                             indexSearcher, scoreMode);
                    }
                    else
                    {
                        joinQuery = JoinUtil.CreateJoinQuery("to", multipleValuesPerDocument, "from", actualQuery,
                                                             indexSearcher, scoreMode);
                    }
                    if (Verbose)
                    {
                        Console.WriteLine("joinQuery=" + joinQuery);
                    }

                    // Need to know all documents that have matches. TopDocs doesn't give me that and then I'd be also testing TopDocsCollector...
                    FixedBitSet          actualResult         = new FixedBitSet(indexSearcher.IndexReader.MaxDoc);
                    TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.Create(10, false);
                    indexSearcher.Search(joinQuery,
                                         new CollectorAnonymousClass2(scoreDocsInOrder, actualResult,
                                                                      topScoreDocCollector));
                    // Asserting bit set...
                    if (Verbose)
                    {
                        Console.WriteLine("expected cardinality:" + expectedResult.Cardinality);
                        DocIdSetIterator iterator = expectedResult.GetIterator();
                        for (int doc = iterator.NextDoc();
                             doc != DocIdSetIterator.NO_MORE_DOCS;
                             doc = iterator.NextDoc())
                        {
                            Console.WriteLine(string.Format("Expected doc[{0}] with id value {1}", doc, indexSearcher.Doc(doc).Get("id")));
                        }
                        Console.WriteLine("actual cardinality:" + actualResult.Cardinality);
                        iterator = actualResult.GetIterator();
                        for (int doc = iterator.NextDoc();
                             doc != DocIdSetIterator.NO_MORE_DOCS;
                             doc = iterator.NextDoc())
                        {
                            Console.WriteLine(string.Format("Actual doc[{0}] with id value {1}", doc, indexSearcher.Doc(doc).Get("id")));
                        }
                    }
                    assertEquals(expectedResult, actualResult);

                    // Asserting TopDocs...
                    TopDocs expectedTopDocs = CreateExpectedTopDocs(randomValue, from, scoreMode, context);
                    TopDocs actualTopDocs   = topScoreDocCollector.GetTopDocs();
                    assertEquals(expectedTopDocs.TotalHits, actualTopDocs.TotalHits);
                    assertEquals(expectedTopDocs.ScoreDocs.Length, actualTopDocs.ScoreDocs.Length);
                    if (scoreMode == ScoreMode.None)
                    {
                        continue;
                    }

                    assertEquals(expectedTopDocs.MaxScore, actualTopDocs.MaxScore, 0.0f);
                    for (int i = 0; i < expectedTopDocs.ScoreDocs.Length; i++)
                    {
                        if (Verbose)
                        {
                            Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "Expected doc: {0} | Actual doc: {1}\n", expectedTopDocs.ScoreDocs[i].Doc, actualTopDocs.ScoreDocs[i].Doc));
                            Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "Expected score: {0} | Actual score: {1}\n", expectedTopDocs.ScoreDocs[i].Score, actualTopDocs.ScoreDocs[i].Score));
                        }
                        assertEquals(expectedTopDocs.ScoreDocs[i].Doc, actualTopDocs.ScoreDocs[i].Doc);
                        assertEquals(expectedTopDocs.ScoreDocs[i].Score, actualTopDocs.ScoreDocs[i].Score, 0.0f);
                        Explanation explanation = indexSearcher.Explain(joinQuery, expectedTopDocs.ScoreDocs[i].Doc);
                        assertEquals(expectedTopDocs.ScoreDocs[i].Score, explanation.Value, 0.0f);
                    }
                }
                topLevelReader.Dispose();
                dir.Dispose();
            }
        }
        public Task<ISearchResponse<Book>> SearchBookWithAggregation_Aync(string criteria = "", string searchField = "", List<string> refinements = null, int count = 10)
        {
            Task<ISearchResponse<Book>> tSearch = Task.Factory.StartNew(() =>
            {

                QueryContainer _query;

                if (String.IsNullOrEmpty(criteria))
                {
                    _query = new MatchAllQuery();
                }
                else
                {
                    if (String.IsNullOrEmpty(searchField))
                    {
                        searchField = "_all";

                        _query = new MatchQuery()
                        {
                            Field = searchField,
                            Query = criteria
                        };
                    }
                    else
                    {
                        _query = new TermQuery()
                        {
                            Field = searchField,
                            Value = criteria
                        };
                    }

                }

                SearchRequest searchRequest = new SearchRequest
                {
                    From = 0,
                    Size = count,
                    Query = _query
                };

                if (refinements != null && refinements.Count > 0)
                {
                    var _aggregations = new Dictionary<string, IAggregationContainer>();

                    foreach (var field in refinements)
                    {
                        _aggregations.Add(field, new AggregationContainer
                        {
                            Terms = new TermsAggregator
                            {
                                Field = field
                            }
                        });
                    }

                    searchRequest.Aggregations = _aggregations;

                }

                var qResult = esClient.Search<Book>(searchRequest);

                return qResult;
            });

            return tSearch;
        }
Example #30
0
        public void TestLazy()
        {
            int         id     = Random().nextInt(NUM_DOCS);
            IndexReader reader = DirectoryReader.Open(dir);

            try
            {
                Query         q        = new TermQuery(new Term("docid", "" + id));
                IndexSearcher searcher = NewSearcher(reader);
                ScoreDoc[]    hits     = searcher.Search(q, 100).ScoreDocs;
                assertEquals("Too many docs", 1, hits.Length);
                LazyTestingStoredFieldVisitor visitor
                    = new LazyTestingStoredFieldVisitor(new LazyDocument(reader, hits[0].Doc),
                                                        FIELDS);
                reader.Document(hits[0].Doc, visitor);
                Document d = visitor.doc;

                int numFieldValues = 0;
                IDictionary <string, int> fieldValueCounts = new HashMap <string, int>();

                // at this point, all FIELDS should be Lazy and unrealized
                foreach (IndexableField f in d)
                {
                    numFieldValues++;
                    if (f.Name.equals("never_load"))
                    {
                        fail("never_load was loaded");
                    }
                    if (f.Name.equals("load_later"))
                    {
                        fail("load_later was loaded on first pass");
                    }
                    if (f.Name.equals("docid"))
                    {
                        assertFalse(f.Name, f is LazyDocument.LazyField);
                    }
                    else
                    {
                        int count = fieldValueCounts.ContainsKey(f.Name) ?
                                    fieldValueCounts[f.Name] : 0;
                        count++;
                        fieldValueCounts.Put(f.Name, count);
                        assertTrue(f.Name + " is " + f.GetType(),
                                   f is LazyDocument.LazyField);
                        LazyDocument.LazyField lf = (LazyDocument.LazyField)f;
                        assertFalse(f.Name + " is loaded", lf.HasBeenLoaded);
                    }
                }
                Console.WriteLine("numFieldValues == " + numFieldValues);
                assertEquals("numFieldValues", 1 + (NUM_VALUES * FIELDS.Length), // LUCENENET TODO: Failing here 1 too small, but what field is the + 1 here supposed to represent?
                             numFieldValues);

                foreach (string field in fieldValueCounts.Keys)
                {
                    assertEquals("fieldName count: " + field,
                                 NUM_VALUES, fieldValueCounts[field]);
                }

                // pick a single field name to load a single value
                string           fieldName   = FIELDS[Random().nextInt(FIELDS.Length)];
                IndexableField[] fieldValues = d.GetFields(fieldName);
                assertEquals("#vals in field: " + fieldName,
                             NUM_VALUES, fieldValues.Length);
                int valNum = Random().nextInt(fieldValues.Length);
                assertEquals(id + "_" + fieldName + "_" + valNum,
                             fieldValues[valNum].StringValue);

                // now every value of fieldName should be loaded
                foreach (IndexableField f in d)
                {
                    if (f.Name.equals("never_load"))
                    {
                        fail("never_load was loaded");
                    }
                    if (f.Name.equals("load_later"))
                    {
                        fail("load_later was loaded too soon");
                    }
                    if (f.Name.equals("docid"))
                    {
                        assertFalse(f.Name, f is LazyDocument.LazyField);
                    }
                    else
                    {
                        assertTrue(f.Name + " is " + f.GetType(),
                                   f is LazyDocument.LazyField);
                        LazyDocument.LazyField lf = (LazyDocument.LazyField)f;
                        assertEquals(f.Name + " is loaded?",
                                     lf.Name.equals(fieldName), lf.HasBeenLoaded);
                    }
                }

                // use the same LazyDoc to ask for one more lazy field
                visitor = new LazyTestingStoredFieldVisitor(new LazyDocument(reader, hits[0].Doc),
                                                            "load_later");
                reader.Document(hits[0].Doc, visitor);
                d = visitor.doc;

                // ensure we have all the values we expect now, and that
                // adding one more lazy field didn't "unload" the existing LazyField's
                // we already loaded.
                foreach (IndexableField f in d)
                {
                    if (f.Name.equals("never_load"))
                    {
                        fail("never_load was loaded");
                    }
                    if (f.Name.equals("docid"))
                    {
                        assertFalse(f.Name, f is LazyDocument.LazyField);
                    }
                    else
                    {
                        assertTrue(f.Name + " is " + f.GetType(),
                                   f is LazyDocument.LazyField);
                        LazyDocument.LazyField lf = (LazyDocument.LazyField)f;
                        assertEquals(f.Name + " is loaded?",
                                     lf.Name.equals(fieldName), lf.HasBeenLoaded);
                    }
                }

                // even the underlying doc shouldn't have never_load
                assertNull("never_load was loaded in wrapped doc",
                           visitor.lazyDoc.Document.GetField("never_load"));
            }
            finally
            {
                reader.Dispose();
            }
        }
Example #31
0
        public virtual void TestRandomSampling()
        {
            Directory dir     = NewDirectory();
            Directory taxoDir = NewDirectory();

            DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
            RandomIndexWriter       writer     = new RandomIndexWriter(
#if FEATURE_INSTANCE_TESTDATA_INITIALIZATION
                this,
#endif
                Util.LuceneTestCase.Random, dir);

            FacetsConfig config = new FacetsConfig();

            int numDocs = AtLeast(10000);

            for (int i = 0; i < numDocs; i++)
            {
                Document doc = new Document();
                doc.Add(new StringField("EvenOdd", (i % 2 == 0) ? "even" : "odd", Store.NO));
                doc.Add(new FacetField("iMod10", Convert.ToString(i % 10)));
                writer.AddDocument(config.Build(taxoWriter, doc));
            }
            Random random = Random;

            // NRT open
            IndexSearcher searcher   = NewSearcher(writer.GetReader());
            var           taxoReader = new DirectoryTaxonomyReader(taxoWriter);

            IOUtils.Dispose(writer, taxoWriter);

            // Test empty results
            RandomSamplingFacetsCollector collectRandomZeroResults = new RandomSamplingFacetsCollector(numDocs / 10, random.NextInt64());

            // There should be no divisions by zero
            searcher.Search(new TermQuery(new Term("EvenOdd", "NeverMatches")), collectRandomZeroResults);

            // There should be no divisions by zero and no null result
            Assert.NotNull(collectRandomZeroResults.GetMatchingDocs());

            // There should be no results at all
            foreach (MatchingDocs doc in collectRandomZeroResults.GetMatchingDocs())
            {
                Assert.AreEqual(0, doc.TotalHits);
            }

            // Now start searching and retrieve results.

            // Use a query to select half of the documents.
            TermQuery query = new TermQuery(new Term("EvenOdd", "even"));

            // there will be 5 facet values (0, 2, 4, 6 and 8), as only the even (i %
            // 10) are hits.
            // there is a REAL small chance that one of the 5 values will be missed when
            // sampling.
            // but is that 0.8 (chance not to take a value) ^ 2000 * 5 (any can be
            // missing) ~ 10^-193
            // so that is probably not going to happen.
            int maxNumChildren = 5;

            RandomSamplingFacetsCollector random100Percent = new RandomSamplingFacetsCollector(numDocs, random.NextInt64());      // no sampling
            RandomSamplingFacetsCollector random10Percent  = new RandomSamplingFacetsCollector(numDocs / 10, random.NextInt64()); // 10 % of total docs, 20% of the hits

            FacetsCollector fc = new FacetsCollector();

            searcher.Search(query, MultiCollector.Wrap(fc, random100Percent, random10Percent));

            FastTaxonomyFacetCounts random10FacetCounts  = new FastTaxonomyFacetCounts(taxoReader, config, random10Percent);
            FastTaxonomyFacetCounts random100FacetCounts = new FastTaxonomyFacetCounts(taxoReader, config, random100Percent);
            FastTaxonomyFacetCounts exactFacetCounts     = new FastTaxonomyFacetCounts(taxoReader, config, fc);

            FacetResult random10Result  = random10Percent.AmortizeFacetCounts(random10FacetCounts.GetTopChildren(10, "iMod10"), config, searcher);
            FacetResult random100Result = random100FacetCounts.GetTopChildren(10, "iMod10");
            FacetResult exactResult     = exactFacetCounts.GetTopChildren(10, "iMod10");

            Assert.AreEqual(random100Result, exactResult);

            // we should have five children, but there is a small chance we have less.
            // (see above).
            Assert.True(random10Result.ChildCount <= maxNumChildren);
            // there should be one child at least.
            Assert.True(random10Result.ChildCount >= 1);

            // now calculate some statistics to determine if the sampled result is 'ok'.
            // because random sampling is used, the results will vary each time.
            int sum = 0;

            foreach (LabelAndValue lav in random10Result.LabelValues)
            {
                sum += (int)lav.Value;
            }
            float mu = (float)sum / (float)maxNumChildren;

            float variance = 0;

            foreach (LabelAndValue lav in random10Result.LabelValues)
            {
                variance += (float)Math.Pow((mu - (int)lav.Value), 2);
            }
            variance = variance / maxNumChildren;
            float sigma = (float)Math.Sqrt(variance);

            // we query only half the documents and have 5 categories. The average
            // number of docs in a category will thus be the total divided by 5*2
            float targetMu = numDocs / (5.0f * 2.0f);

            // the average should be in the range and the standard deviation should not
            // be too great
            Assert.True(sigma < 200);
            Assert.True(targetMu - 3 * sigma < mu && mu < targetMu + 3 * sigma);

            IOUtils.Dispose(searcher.IndexReader, taxoReader, dir, taxoDir);
        }
Example #32
0
        public void TestMaxSizeHighlightTruncates()
        {
            var helper = new TestHighlightRunner();
            helper.TestAction = () =>
                                    {
                                        var goodWord = "goodtoken";
                                        var stopWords = Support.Compatibility.SetFactory.CreateHashSet(new[] { "stoppedtoken" });

                                        var query = new TermQuery(new Term("data", goodWord));

                                        string match;
                                        StringBuilder sb = new StringBuilder();
                                        sb.Append(goodWord);
                                        for (int i = 0; i < 10000; i++)
                                        {
                                            sb.Append(" ");
                                            // only one stopword
                                            sb.Append(stopWords.First());
                                        }
                                        SimpleHTMLFormatter fm = new SimpleHTMLFormatter();
                                        Highlighter hg = helper.GetHighlighter(query, "data",
                                                                               new StandardAnalyzer(TEST_VERSION,
                                                                                                    stopWords).
                                                                                   TokenStream(
                                                                                       "data",
                                                                                       new StringReader(sb.ToString())),
                                                                               fm); // new Highlighter(fm,
                                        // new
                                        // QueryTermScorer(query));
                                        hg.TextFragmenter = new NullFragmenter();
                                        hg.MaxDocCharsToAnalyze = 100;
                                        match = hg.GetBestFragment(new StandardAnalyzer(TEST_VERSION, stopWords), "data",
                                                                   sb.ToString());
                                        Assert.IsTrue(match.Length < hg.MaxDocCharsToAnalyze,
                                                      "Matched text should be no more than 100 chars in length ");

                                        // add another tokenized word to the overrall length - but set way
                                        // beyond
                                        // the length of text under consideration (after a large slug of stop
                                        // words
                                        // + whitespace)
                                        sb.Append(" ");
                                        sb.Append(goodWord);
                                        match = hg.GetBestFragment(new StandardAnalyzer(TEST_VERSION, stopWords), "data",
                                                                   sb.ToString());
                                        Assert.IsTrue(match.Length < hg.MaxDocCharsToAnalyze,
                                                      "Matched text should be no more than 100 chars in length ");
                                    };

            helper.Start();

        }
Example #33
0
        /// <summary>
        /// 搜索日志
        /// </summary>
        /// <param name="highlight"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="keyword"></param>
        /// <param name="logger_name"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public static async Task <PagerData <ESLogLine, QueryExtData> > Search(
            bool highlight = true,
            DateTime?start = null, DateTime?end       = null,
            string keyword = null, string logger_name = null,
            int page       = 1, int pagesize = 10)
        {
            var sd = new SearchDescriptor <ESLogLine>();

            sd = sd.Index(IndexName);

            var query = new QueryContainer();

            if (start != null)
            {
                query &= new DateRangeQuery()
                {
                    Field = nameof(temp.UpdateTime), GreaterThanOrEqualTo = start.Value
                };
            }
            if (end != null)
            {
                query &= new DateRangeQuery()
                {
                    Field = nameof(temp.UpdateTime), LessThan = end.Value
                };
            }
            if (ValidateHelper.IsPlumpString(keyword))
            {
                query &= new MatchQuery()
                {
                    Field              = nameof(temp.Message),
                    Query              = keyword,
                    Operator           = Operator.Or,
                    MinimumShouldMatch = "100%"
                };
            }
            if (ValidateHelper.IsPlumpString(logger_name))
            {
                query &= new TermQuery()
                {
                    Field = nameof(temp.LoggerName), Value = logger_name
                };
            }
            //查询条件
            sd = sd.Query(_ => query);
            //聚合
            sd = sd.Aggregations(x =>
                                 x.Terms(nameof(temp.LoggerName), av => av.Field(nameof(temp.LoggerName)).Size(1000))
                                 .Terms(nameof(temp.Level), av => av.Field(nameof(temp.Level)).Size(1000))
                                 .Terms(nameof(temp.Domain), av => av.Field(nameof(temp.Domain)).Size(1000)));
            //高亮
            if (highlight)
            {
                sd = sd.AddHighlightWrapper("<em class='kwd'>", "</em>", x => x.Field(nameof(temp.Message)));
            }
            //排序
            var sort = new SortDescriptor <ESLogLine>();

            sort = sort.Descending(x => x.UpdateTime);
            sort = sort.Descending(new Field("", boost: null));
            sd   = sd.Sort(_ => sort);

            //分页
            sd = sd.QueryPage_(page, pagesize);

            //请求服务器
            var client = new ElasticClient(ElasticsearchClientManager.Instance.DefaultClient);
            var re     = await client.SearchAsync <ESLogLine>(_ => sd);

            re.ThrowIfException();

            var data = new PagerData <ESLogLine, QueryExtData>();

            data.ItemCount = (int)re.Total;
            data.DataList  = re.Hits.Select(x => x.Source).ToList();
            //聚合数据
            data.ExtData = new QueryExtData();

            return(data);
        }
            public override Query Rewrite(IndexReader reader)
            {
                // ArrayList spanClauses = new ArrayList();
                if (contents is TermQuery)
                {
                    return(contents);
                }
                // Build a sequence of Span clauses arranged in a SpanNear - child
                // clauses can be complex
                // Booleans e.g. nots and ors etc
                int numNegatives = 0;

                if (!(contents is BooleanQuery))
                {
                    throw new ArgumentException("Unknown query type \""
                                                + contents.GetType().Name
                                                + "\" found in phrase query string \"" + phrasedQueryStringContents
                                                + "\"");
                }
                BooleanQuery bq = (BooleanQuery)contents;

                BooleanClause[] bclauses       = bq.GetClauses();
                SpanQuery[]     allSpanClauses = new SpanQuery[bclauses.Length];
                // For all clauses e.g. one* two~
                for (int i = 0; i < bclauses.Length; i++)
                {
                    // HashSet bclauseterms=new HashSet();
                    Query qc = bclauses[i].Query;
                    // Rewrite this clause e.g one* becomes (one OR onerous)
                    qc = qc.Rewrite(reader);
                    if (bclauses[i].Occur.Equals(Occur.MUST_NOT))
                    {
                        numNegatives++;
                    }

                    if (qc is BooleanQuery)
                    {
                        List <SpanQuery> sc = new List <SpanQuery>();
                        AddComplexPhraseClause(sc, (BooleanQuery)qc);
                        if (sc.Count > 0)
                        {
                            allSpanClauses[i] = sc[0];
                        }
                        else
                        {
                            // Insert fake term e.g. phrase query was for "Fred Smithe*" and
                            // there were no "Smithe*" terms - need to
                            // prevent match on just "Fred".
                            allSpanClauses[i] = new SpanTermQuery(new Term(field,
                                                                           "Dummy clause because no terms found - must match nothing"));
                        }
                    }
                    else
                    {
                        if (qc is TermQuery)
                        {
                            TermQuery tq = (TermQuery)qc;
                            allSpanClauses[i] = new SpanTermQuery(tq.Term);
                        }
                        else
                        {
                            throw new ArgumentException("Unknown query type \""
                                                        + qc.GetType().Name
                                                        + "\" found in phrase query string \""
                                                        + phrasedQueryStringContents + "\"");
                        }
                    }
                }
                if (numNegatives == 0)
                {
                    // The simple case - no negative elements in phrase
                    return(new SpanNearQuery(allSpanClauses, slopFactor, inOrder));
                }
                // Complex case - we have mixed positives and negatives in the
                // sequence.
                // Need to return a SpanNotQuery
                List <SpanQuery> positiveClauses = new List <SpanQuery>();

                for (int j = 0; j < allSpanClauses.Length; j++)
                {
                    if (!bclauses[j].Occur.Equals(Occur.MUST_NOT))
                    {
                        positiveClauses.Add(allSpanClauses[j]);
                    }
                }

                SpanQuery[] includeClauses = positiveClauses
                                             .ToArray();

                SpanQuery include = null;

                if (includeClauses.Length == 1)
                {
                    include = includeClauses[0]; // only one positive clause
                }
                else
                {
                    // need to increase slop factor based on gaps introduced by
                    // negatives
                    include = new SpanNearQuery(includeClauses, slopFactor + numNegatives,
                                                inOrder);
                }
                // Use sequence of positive and negative values as the exclude.
                SpanNearQuery exclude = new SpanNearQuery(allSpanClauses, slopFactor,
                                                          inOrder);
                SpanNotQuery snot = new SpanNotQuery(include, exclude);

                return(snot);
            }
        public virtual void TestTermInDisguise()
        {
            Query expected = new TermQuery(new Term("field", "st*ar\\*"));

            assertEquals(expected, Parse("sT*Ar\\\\\\*"));
        }
        public virtual void TestAfterClose()
        {
            Directory dir1 = GetAssertNoDeletesDirectory(NewDirectory());
            IndexWriter writer = new IndexWriter(dir1, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())));

            // create the index
            CreateIndexNoClose(false, "test", writer);

            DirectoryReader r = writer.Reader;
            writer.Dispose();

            TestUtil.CheckIndex(dir1);

            // reader should remain usable even after IndexWriter is closed:
            Assert.AreEqual(100, r.NumDocs());
            Query q = new TermQuery(new Term("indexname", "test"));
            IndexSearcher searcher = NewSearcher(r);
            Assert.AreEqual(100, searcher.Search(q, 10).TotalHits);
            try
            {
                DirectoryReader.OpenIfChanged(r);
                Assert.Fail("failed to hit AlreadyClosedException");
            }
            catch (AlreadyClosedException ace)
            {
                // expected
            }
            r.Dispose();
            dir1.Dispose();
        }
        public virtual void TestDisablePrefix()
        {
            Query expected = new TermQuery(new Term("field", "test*"));

            assertEquals(expected, ParseKeyword("test*", ~Operator.PREFIX_OPERATOR));
        }
        public virtual void TestDuringAddIndexes()
        {
            Directory dir1 = GetAssertNoDeletesDirectory(NewDirectory());
            IndexWriter writer = new IndexWriter(dir1, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())).SetMergePolicy(NewLogMergePolicy(2)));

            // create the index
            CreateIndexNoClose(false, "test", writer);
            writer.Commit();

            Directory[] dirs = new Directory[10];
            for (int i = 0; i < 10; i++)
            {
                dirs[i] = new MockDirectoryWrapper(Random(), new RAMDirectory(dir1, NewIOContext(Random())));
            }

            DirectoryReader r = writer.Reader;

            const float SECONDS = 0.5f;

            long endTime = (long)(DateTime.Now.Millisecond + 1000.0 * SECONDS);
            IList<Exception> excs = new ConcurrentList<Exception>(new List<Exception>());

            // Only one thread can addIndexes at a time, because
            // IndexWriter acquires a write lock in each directory:
            ThreadClass[] threads = new ThreadClass[1];
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new ThreadAnonymousInnerClassHelper(this, writer, dirs, endTime, excs);
                threads[i].SetDaemon(true);
                threads[i].Start();
            }

            int lastCount = 0;
            while (DateTime.Now.Millisecond < endTime)
            {
                DirectoryReader r2 = DirectoryReader.OpenIfChanged(r);
                if (r2 != null)
                {
                    r.Dispose();
                    r = r2;
                }
                Query q = new TermQuery(new Term("indexname", "test"));
                IndexSearcher searcher = NewSearcher(r);
                int count = searcher.Search(q, 10).TotalHits;
                Assert.IsTrue(count >= lastCount);
                lastCount = count;
            }

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }
            // final check
            DirectoryReader dr2 = DirectoryReader.OpenIfChanged(r);
            if (dr2 != null)
            {
                r.Dispose();
                r = dr2;
            }
            Query q2 = new TermQuery(new Term("indexname", "test"));
            IndexSearcher searcher_ = NewSearcher(r);
            int count_ = searcher_.Search(q2, 10).TotalHits;
            Assert.IsTrue(count_ >= lastCount);

            Assert.AreEqual(0, excs.Count);
            r.Dispose();
            if (dir1 is MockDirectoryWrapper)
            {
                ICollection<string> openDeletedFiles = ((MockDirectoryWrapper)dir1).OpenDeletedFiles;
                Assert.AreEqual(0, openDeletedFiles.Count, "openDeleted=" + openDeletedFiles);
            }

            writer.Dispose();

            dir1.Dispose();
        }
        public virtual void TestTerm()
        {
            Query expected = new TermQuery(new Term("field", "foobar"));

            assertEquals(expected, Parse("foobar"));
        }
        public virtual void TestDuringAddDelete()
        {
            Directory dir1 = NewDirectory();
            IndexWriter writer = new IndexWriter(dir1, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())).SetMergePolicy(NewLogMergePolicy(2)));

            // create the index
            CreateIndexNoClose(false, "test", writer);
            writer.Commit();

            DirectoryReader r = writer.Reader;

            const float SECONDS = 0.5f;

            long endTime = (long)(DateTime.Now.Millisecond + 1000.0 * SECONDS);
            IList<Exception> excs = new ConcurrentList<Exception>(new List<Exception>());

            ThreadClass[] threads = new ThreadClass[NumThreads];
            for (int i = 0; i < NumThreads; i++)
            {
                threads[i] = new ThreadAnonymousInnerClassHelper2(this, writer, r, endTime, excs);
                threads[i].SetDaemon(true);
                threads[i].Start();
            }

            int sum = 0;
            while (DateTime.Now.Millisecond < endTime)
            {
                DirectoryReader r2 = DirectoryReader.OpenIfChanged(r);
                if (r2 != null)
                {
                    r.Dispose();
                    r = r2;
                }
                Query q = new TermQuery(new Term("indexname", "test"));
                IndexSearcher searcher = NewSearcher(r);
                sum += searcher.Search(q, 10).TotalHits;
            }

            for (int i = 0; i < NumThreads; i++)
            {
                threads[i].Join();
            }
            // at least search once
            DirectoryReader dr2 = DirectoryReader.OpenIfChanged(r);
            if (dr2 != null)
            {
                r.Dispose();
                r = dr2;
            }
            Query q2 = new TermQuery(new Term("indexname", "test"));
            IndexSearcher indSearcher = NewSearcher(r);
            sum += indSearcher.Search(q2, 10).TotalHits;
            Assert.IsTrue(sum > 0, "no documents found at all");

            Assert.AreEqual(0, excs.Count);
            writer.Dispose();

            r.Dispose();
            dir1.Dispose();
        }
        public static QueryBase GetDefaultQuery(this TermNode node, IQueryVisitorContext context)
        {
            if (!(context is IElasticQueryVisitorContext elasticContext))
            {
                throw new ArgumentException("Context must be of type IElasticQueryVisitorContext", nameof(context));
            }

            QueryBase query;
            string    nodeFullName = node.GetFullName();

            if (elasticContext.IsPropertyAnalyzed(nodeFullName))
            {
                var fields = !String.IsNullOrEmpty(nodeFullName) ? new[] { nodeFullName } : elasticContext.DefaultFields;

                if (!node.IsQuotedTerm && node.UnescapedTerm.EndsWith("*"))
                {
                    query = new QueryStringQuery {
                        Fields = fields,
                        AllowLeadingWildcard = false,
                        AnalyzeWildcard      = true,
                        Query = node.UnescapedTerm
                    };
                }
                else
                {
                    if (fields.Length == 1)
                    {
                        if (node.IsQuotedTerm)
                        {
                            query = new MatchPhraseQuery {
                                Field = fields[0],
                                Query = node.UnescapedTerm
                            };
                        }
                        else
                        {
                            query = new MatchQuery {
                                Field = fields[0],
                                Query = node.UnescapedTerm
                            };
                        }
                    }
                    else
                    {
                        query = new MultiMatchQuery {
                            Fields = fields,
                            Query  = node.UnescapedTerm
                        };
                        if (node.IsQuotedTerm)
                        {
                            (query as MultiMatchQuery).Type = TextQueryType.Phrase;
                        }
                    }
                }
            }
            else
            {
                query = new TermQuery {
                    Field = nodeFullName,
                    Value = node.UnescapedTerm
                };
            }

            return(query);
        }
Example #42
0
        public virtual PagedList <Models.ResultObject> Search(string key, int pageIndex, int pageSize, params string[] folders)
        {
            var indexDirectory = FSDirectory.Open(new DirectoryInfo(indexDir));

            if (!IndexReader.IndexExists(indexDirectory) || string.IsNullOrEmpty(key) && (folders == null || folders.Length == 0))
            {
                return(new PagedList <ResultObject>(new ResultObject[0], pageIndex, pageSize, 0));
            }

            var query = new BooleanQuery();

            key = QueryParser.Escape(key.Trim().ToLower());

            if (string.IsNullOrEmpty(key))
            {
                key = "*:*";
            }

            QueryParser titleParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, Converter.TitleFieldName, this.Analyzer);
            var         titleQuery  = titleParser.Parse(key);

            titleQuery.Boost = 2;
            query.Add(new BooleanClause(titleQuery, Occur.SHOULD));

            QueryParser bodyParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, Converter.BodyFieldName, this.Analyzer);
            var         bodyQuery  = bodyParser.Parse(key);

            bodyQuery.Boost = 1;
            query.Add(new BooleanClause(bodyQuery, Occur.SHOULD));

            QueryWrapperFilter filter = null;

            if (folders != null && folders.Length > 0)
            {
                var folderQuery = new BooleanQuery();
                //QueryParser folderParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "FolderName", this.Analyzer);
                foreach (var folder in folders)
                {
                    var termQuery = new TermQuery(new Term("FolderName", folder));
                    termQuery.Boost = 3;
                    folderQuery.Add(new BooleanClause(termQuery, Occur.SHOULD));
                }

                filter = new QueryWrapperFilter(folderQuery);
            }

            var searcher   = new IndexSearcher(indexDirectory, true);
            var collecltor = TopScoreDocCollector.Create(searcher.MaxDoc, false);

            if (filter == null)
            {
                searcher.Search(query, collecltor);
            }
            else
            {
                searcher.Search(query, filter, collecltor);
            }


            var lighter =
                new Highlighter(new SimpleHTMLFormatter("<strong class='highlight'>", "</strong>"), new Lucene.Net.Search.Highlight.QueryScorer((Query)query));


            var startIndex = (pageIndex - 1) * pageSize;

            List <ResultObject> results = new List <ResultObject>();

            foreach (var doc in collecltor.TopDocs(startIndex, pageSize).ScoreDocs)
            {
                var          document = searcher.Doc(doc.Doc);
                ResultObject result   = Converter.ToResultObject(lighter, document);
                if (result != null)
                {
                    results.Add(result);
                }
            }

            return(new PagedList <ResultObject>(results, pageIndex, pageSize, collecltor.TotalHits));
        }
        public static SearchResultDataSet <Article> Search(ArticleQuery query)
        {
            //触发事件
            GlobalEvents.UserSearch(query.Title);

            //索引文件不存在时,返回null
            if (!GlobalSettings.CheckFileExist(PhysicalIndexDirectory))
            {
                return(new SearchResultDataSet <Article>());
            }
            DateTime     startTime    = DateTime.Now;
            BooleanQuery currentQuery = new BooleanQuery();

            //CategoryID
            if (query.CategoryID.HasValue && query.CategoryID.Value != 0)
            {
                Term  categoryIDTearm = new Term(NewsIndexField.CategoryID, query.CategoryID.ToString());
                Query categoryIDQuery = new TermQuery(categoryIDTearm);
                currentQuery.Add(categoryIDQuery, BooleanClause.Occur.MUST);
            }

            //KeyWord
            if (!string.IsNullOrEmpty(query.Title))
            {
                query.Title = SearchHelper.LuceneKeywordsScrubber(query.Title);
                if (!string.IsNullOrEmpty(query.Title))
                {
                    string[] searchFieldsForKeyword = new string[4];
                    searchFieldsForKeyword[0] = NewsIndexField.Title;
                    searchFieldsForKeyword[1] = NewsIndexField.SubTitle;
                    searchFieldsForKeyword[2] = NewsIndexField.Abstract;
                    searchFieldsForKeyword[3] = NewsIndexField.Keywords;

                    MultiFieldQueryParser articleWordQueryParser = new MultiFieldQueryParser(searchFieldsForKeyword, SearchHelper.GetChineseAnalyzer());
                    articleWordQueryParser.SetLowercaseExpandedTerms(true);
                    articleWordQueryParser.SetDefaultOperator(QueryParser.OR_OPERATOR);

                    string keyWordsSplit    = SearchHelper.SplitKeywordsBySpace(query.Title);
                    Query  articleWordQuery = articleWordQueryParser.Parse(keyWordsSplit);
                    currentQuery.Add(articleWordQuery, BooleanClause.Occur.MUST);
                }
            }

            //Search
            IndexSearcher searcher = new IndexSearcher(PhysicalIndexDirectory);
            Hits          hits     = searcher.Search(currentQuery);
            SearchResultDataSet <Article> articles = new SearchResultDataSet <Article>();
            int pageLowerBound = query.PageIndex * query.PageSize;
            int pageUpperBound = pageLowerBound + query.PageSize;

            if (pageUpperBound > hits.Length())
            {
                pageUpperBound = hits.Length();
            }

            //HighLight
            PanGu.HighLight.Highlighter highlighter = null;
            if (!string.IsNullOrEmpty(query.Title))
            {
                highlighter = new PanGu.HighLight.Highlighter(new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"#c60a00\">", "</font>"), new PanGu.Segment());
                highlighter.FragmentSize = 100;
            }
            for (int i = pageLowerBound; i < pageUpperBound; i++)
            {
                Article item = ConvertDocumentToArticle(hits.Doc(i));
                if (!string.IsNullOrEmpty(query.Title))
                {
                    string bestBody = null;
                    if (!string.IsNullOrEmpty(item.Abstract) && item.Abstract.Length > MaxNumFragmentsRequired)
                    {
                        bestBody = highlighter.GetBestFragment(query.Title, item.Abstract);
                    }

                    if (!string.IsNullOrEmpty(bestBody))
                    {
                        item.Abstract = bestBody;
                    }
                    else
                    {
                        item.Abstract = HtmlHelper.TrimHtml(item.Abstract, 100);
                    }

                    string bestSubject = null;
                    if (!string.IsNullOrEmpty(item.Title) && item.Title.Length > MaxNumFragmentsRequired)
                    {
                        bestSubject = highlighter.GetBestFragment(query.Title, item.Title);
                    }

                    if (!string.IsNullOrEmpty(bestSubject))
                    {
                        item.Title = bestSubject;
                    }
                }
                articles.Records.Add(item);
            }
            searcher.Close();
            articles.TotalRecords = hits.Length();

            DateTime endTime = DateTime.Now;

            articles.SearchDuration = (endTime.Ticks - startTime.Ticks) / 1E7f;
            articles.PageIndex      = query.PageIndex;
            articles.PageSize       = query.PageSize;

            return(articles);
        }
Example #44
0
        public List <Triathlete> SearchByDuration(TimeSpan swimL, TimeSpan swimH,
                                                  TimeSpan bikeL, TimeSpan bikeH,
                                                  TimeSpan runL, TimeSpan runH,
                                                  TimeSpan finishL, TimeSpan finishH,
                                                  List <int> requestIds)
        {
            QueryContainer orQuery = null;

            foreach (var id in requestIds)
            {
                orQuery |= new TermQuery
                {
                    Field = "requestContextId",  //case sensitive!! found out the hard way
                    Value = id
                };
            }

            var client = SetupElasticSearch();

            var response = client.Search <Triathlete>(s => s
                                                      .Query(q =>
                                                             q.Bool(b => b.Must(orQuery))
                                                             &&
                                                             q.Range(c => c
                                                                     .Name("swim_query")
                                                                     .Field("swim")
                                                                     .GreaterThan(swimL.Ticks)
                                                                     .LessThan(swimH.Ticks))
                                                             &&
                                                             q.Range(c => c
                                                                     .Name("bike_query")
                                                                     .Field("bike")
                                                                     .GreaterThan(bikeL.Ticks)
                                                                     .LessThan(bikeH.Ticks))

                                                             &&
                                                             q.Range(c => c
                                                                     .Name("run_query")
                                                                     .Field("run")
                                                                     .GreaterThan(runL.Ticks)
                                                                     .LessThan(runH.Ticks))

                                                             &&
                                                             q.Range(c => c
                                                                     .Name("finish_query")
                                                                     .Field("finish")
                                                                     .GreaterThan(finishL.Ticks)
                                                                     .LessThan(finishH.Ticks))
                                                             ));

            /*****
             * var response = client.Search<Triathlete>(s => s
             *  .Query(q => q
             *      .Range(c => c
             *         .Name("swim_query")
             *             .Field("swim")
             *                 .LessThan(swim.Ticks))
             *
             *  ));
             ****/

            var athletes = response.Documents.ToList();

            return(athletes);
        }
Example #45
0
        public virtual void TestTerm()
        {
            Query query = new TermQuery(new Term("field", "seventy"));

            CheckHits(query, new int[] { 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 1070, 1071, 1072, 1073, 1074, 1075, 1076, 1077, 1078, 1079, 1170, 1171, 1172, 1173, 1174, 1175, 1176, 1177, 1178, 1179, 1270, 1271, 1272, 1273, 1274, 1275, 1276, 1277, 1278, 1279, 1370, 1371, 1372, 1373, 1374, 1375, 1376, 1377, 1378, 1379, 1470, 1471, 1472, 1473, 1474, 1475, 1476, 1477, 1478, 1479, 1570, 1571, 1572, 1573, 1574, 1575, 1576, 1577, 1578, 1579, 1670, 1671, 1672, 1673, 1674, 1675, 1676, 1677, 1678, 1679, 1770, 1771, 1772, 1773, 1774, 1775, 1776, 1777, 1778, 1779, 1870, 1871, 1872, 1873, 1874, 1875, 1876, 1877, 1878, 1879, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979 });
        }
Example #46
0
        private static Query ParseQuery(SearchFilter searchFilter)
        {
            if (String.IsNullOrWhiteSpace(searchFilter.SearchTerm))
            {
                return(new MatchAllDocsQuery());
            }

            var fields      = new[] { "Id", "Title", "Tags", "Description", "Author" };
            var analyzer    = new StandardAnalyzer(LuceneCommon.LuceneVersion);
            var queryParser = new MultiFieldQueryParser(LuceneCommon.LuceneVersion, fields, analyzer);

            // All terms in the multi-term query appear in at least one of the fields.
            var conjuctionQuery = new BooleanQuery();

            conjuctionQuery.Boost = 2.0f;

            // Some terms in the multi-term query appear in at least one of the fields.
            var disjunctionQuery = new BooleanQuery();

            disjunctionQuery.Boost = 0.1f;

            // Escape the entire term we use for exact searches.
            var escapedSearchTerm = Escape(searchFilter.SearchTerm).Replace("id\\:", string.Empty).Replace("author\\:", string.Empty).Replace("tag\\:", string.Empty);

            var exactIdQuery = new TermQuery(new Term("Id-Exact", escapedSearchTerm));

            exactIdQuery.Boost = 7.0f;
            var relatedIdQuery = new WildcardQuery(new Term("Id-Exact", escapedSearchTerm + ".*"));

            relatedIdQuery.Boost = 6.5f;
            var startsIdQuery = new WildcardQuery(new Term("Id-Exact", escapedSearchTerm + "*"));

            startsIdQuery.Boost = 6.0f;
            var wildCardIdQuery = new WildcardQuery(new Term("Id-Exact", "*" + escapedSearchTerm + "*"));

            wildCardIdQuery.Boost = 3.0f;

            var exactTitleQuery = new TermQuery(new Term("Title-Exact", escapedSearchTerm));

            exactTitleQuery.Boost = 6.5f;
            var startsTitleQuery = new WildcardQuery(new Term("Title-Exact", escapedSearchTerm + "*"));

            startsTitleQuery.Boost = 5.5f;
            var wildCardTitleQuery = new WildcardQuery(new Term("Title-Exact", "*" + escapedSearchTerm + "*"));

            wildCardTitleQuery.Boost = 2.5f;

            // Suffix wildcard search e.g. jquer*
            var wildCardQuery = new BooleanQuery();

            wildCardQuery.Boost = 0.5f;

            var  terms               = GetSearchTerms(searchFilter.SearchTerm).ToList();
            bool onlySearchById      = searchFilter.ByIdOnly || searchFilter.ExactIdOnly || terms.AnySafe(t => t.StartsWith("id\\:"));
            bool onlySearchByExactId = searchFilter.ExactIdOnly;
            bool onlySearchByAuthor  = terms.AnySafe(t => t.StartsWith("author\\:"));
            bool onlySearchByTag     = terms.AnySafe(t => t.StartsWith("tag\\:"));
            bool searchLimiter       = onlySearchById || onlySearchByAuthor || onlySearchByTag;

            foreach (var term in terms)
            {
                var localTerm = Escape(term).Replace("id\\:", string.Empty).Replace("author\\:", string.Empty).Replace("tag\\:", string.Empty);
                var termQuery = queryParser.Parse(localTerm);
                conjuctionQuery.Add(termQuery, Occur.MUST);
                disjunctionQuery.Add(termQuery, Occur.SHOULD);

                foreach (var field in fields)
                {
                    if (onlySearchById && field != "Id")
                    {
                        continue;
                    }
                    if (onlySearchByAuthor && field != "Author")
                    {
                        continue;
                    }
                    if (onlySearchByTag && field != "Tags")
                    {
                        continue;
                    }

                    var wildCardTermQuery = new WildcardQuery(new Term(field, localTerm + "*"));
                    wildCardTermQuery.Boost = searchLimiter ? 7.0f : 0.7f;
                    wildCardQuery.Add(wildCardTermQuery, Occur.MUST);
                }
            }

            // Create an OR of all the queries that we have
            var combinedQuery = conjuctionQuery.Combine(new Query[] { exactIdQuery, relatedIdQuery, exactTitleQuery, startsIdQuery, startsTitleQuery, wildCardIdQuery, wildCardTitleQuery, conjuctionQuery, wildCardQuery });

            if (onlySearchByExactId)
            {
                combinedQuery = conjuctionQuery.Combine(new Query[] { exactIdQuery });
            }
            else if (onlySearchById)
            {
                combinedQuery = conjuctionQuery.Combine(new Query[] { exactIdQuery, relatedIdQuery, startsIdQuery, wildCardIdQuery, wildCardQuery });
            }
            else if (onlySearchByAuthor || onlySearchByTag)
            {
                combinedQuery = conjuctionQuery.Combine(new Query[] { wildCardQuery });
            }

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

            return(combinedQuery);
        }
Example #47
0
 public virtual void TestTerm()
 {
     Query query = new TermQuery(new Term("field", "seventy"));
     CheckHits(query, new int[] { 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 1070, 1071, 1072, 1073, 1074, 1075, 1076, 1077, 1078, 1079, 1170, 1171, 1172, 1173, 1174, 1175, 1176, 1177, 1178, 1179, 1270, 1271, 1272, 1273, 1274, 1275, 1276, 1277, 1278, 1279, 1370, 1371, 1372, 1373, 1374, 1375, 1376, 1377, 1378, 1379, 1470, 1471, 1472, 1473, 1474, 1475, 1476, 1477, 1478, 1479, 1570, 1571, 1572, 1573, 1574, 1575, 1576, 1577, 1578, 1579, 1670, 1671, 1672, 1673, 1674, 1675, 1676, 1677, 1678, 1679, 1770, 1771, 1772, 1773, 1774, 1775, 1776, 1777, 1778, 1779, 1870, 1871, 1872, 1873, 1874, 1875, 1876, 1877, 1878, 1879, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979 });
 }
Example #48
0
        /// <summary>
        /// 搜索文件
        /// </summary>
        public void SearchFile()
        {
            this.isStopSearch = false;
            this.dtStart      = DateTime.Now;//开始时间

            if (this.indexSearcher == null)
            {
                this.NullResult(); //空结果,修改主窗的status状态。
                this.CallBack();   //线程执行完,修改主窗的相关属性。
                return;
            }

            Query query;

            if (this.keyword.Name != "")
            {
                try
                {
                    QueryParser parser = new QueryParser("Name", this.analyzer);
                    query = parser.Parse(this.keyword.Name);
                    boolQuery.Add(query, BooleanClause.Occur.MUST);
                }
                catch (Exception)
                { }
            }

            if (this.keyword.Directory != null && this.keyword.Directory != "")
            {
                PrefixQuery pq = new PrefixQuery(new Term("Directory", this.keyword.Directory));
                boolQuery.Add(pq, BooleanClause.Occur.MUST);
            }

            if (this.keyword.Extension != null && this.keyword.Extension != "")
            {
                TermQuery termQuery = new TermQuery(new Term("Extension", this.keyword.Extension));
                boolQuery.Add(termQuery, BooleanClause.Occur.MUST);
            }

            try
            {
                //搜索前Static.PageItems项
                this.topDocs = this.indexSearcher.Search(boolQuery, Static.PageItems);
            }
            catch (Exception)
            {
                this.NullResult(); //空结果,修改主窗的status状态。
                this.CallBack();   //线程执行完,修改主窗的相关属性。
                return;
            }

            this.dtStop = DateTime.Now;//完成时间
            if (this.topDocs.totalHits > 0)
            {
                CrossThreadOperateControl CrossUpdateStatus = delegate()
                {
                    lock (this.form.tsslStatus)
                    {
                        this.form.OrginalStatusText = Deal.ToEnglishNumString(this.topDocs.totalHits)
                                                      + " 个对象     用时:" + (this.dtStop - this.dtStart).TotalSeconds + "秒";
                        this.form.tsslStatus.Text = Deal.LimitStringLength(this.form.OrginalStatusText,
                                                                           this.form.TsslLength);
                    }
                };
                this.form.Invoke(CrossUpdateStatus);
            }
            else
            {
                this.NullResult(); //空结果,修改主窗的status状态。
                this.CallBack();   //线程执行完,修改主窗的相关属性。
                return;
            }

            this.pageNums    = topDocs.totalHits / Static.PageItems + 1; //总的页数
            this.currentPage = 1;                                        //每次搜索时当前页数为1

            UpdateLVMethod method = new UpdateLVMethod(this.UpdateFileListView);

            this.UpdateListView(method, this.currentPage);
            this.CallBack();//线程执行完,修改主窗的相关属性。
        }
Example #49
0
 public IndexQueryProvider()
 {
     Terms = new TermQuery(this);
 }
        //public ExtendedSearchResultFilterData searchStoryTeller2(ELModels.MatchDetail _objMatchDetail, QueryContainer _objNestedQuery, dynamic _objS1Data, Dictionary<string, object> ObjectArray, IEnumerable<SearchResultFilterData> obj)
        //{
        //    ExtendedSearchResultFilterData _objSearchResults = new ExtendedSearchResultFilterData();
        //    _objSearchResults.ResultData = new List<SearchResultFilterData>();
        //    _objSearchResults.Master.MasterData = new Dictionary<string, object>();
        //    searchcricket sc = new searchcricket();
        //    SportType = sc.getType(_objMatchDetail.SportID);
        //    if ("CRICKET" == SportType)
        //    {

        //        string ReqShotType = _objS1Data["ShotType"]; string ReqDeliveryType = _objS1Data["DeliveryType"];
        //        string[] _objReqShotType = ReqShotType.Contains(",") ? _objReqShotType = ReqShotType.Split(",") : _objReqShotType = new string[] { _objS1Data["ShotType"] };
        //        string[] _objReqDeliveryType = ReqDeliveryType.Contains(",") ? _objReqDeliveryType = ReqDeliveryType.Split(",") : _objReqDeliveryType = new string[] { _objS1Data["DeliveryType"] };
        //        ObjectArray = GetDropdowns(_objNestedQuery, ObjectArray, EsClient, "cricket", GetColumnForEntity(13), _objReqShotType);
        //        ObjectArray = GetDropdowns(_objNestedQuery, ObjectArray, EsClient, "cricket", GetColumnForEntity(32), _objReqDeliveryType);
        //        obj = returnSportResult(EsClient, _objNestedQuery, "cricket");
        //        _objSearchResults.ResultData = obj;
        //        _objSearchResults.Master.MasterData = ObjectArray;
        //    }
        //    return _objSearchResults;
        //    //throw new NotImplementedException();
        //}


        //public override Dictionary<string, object> fetchDropdowns(QueryContainer _objNestedQuery, Dictionary<string, object> ObjectArray, ElasticClient EsClient, string IndexName, Dictionary<string, string> _columns, string[] sFilterArray)
        //{
        //    List<SearchResultFilterData> _objSearchResultFilterData = new List<SearchResultFilterData>();
        //    List<FilteredEntityData> obj = new List<FilteredEntityData>();

        //    if (_columns != null && _columns.Count > 0)
        //    {
        //        KeyValuePair<string, string> _column = _columns.FirstOrDefault();
        //        List<string> EntityIds = new List<string>();
        //        List<string> EntityNames = new List<string>();
        //        foreach (var col in _columns)
        //        {
        //            EntityIds.Add(col.Key);
        //            EntityNames.Add(col.Value);

        //        }
        //        var result = EsClient.Search<SearchCricketData>(a => a.Index(IndexName).Size(0).Query(s => _objNestedQuery)
        //        .Aggregations(a1 => a1.Terms("terms_agg", t => t.Script(t1 => t1.Source("doc['"+ EntityNames.ElementAt(0) + ".keyword'].value + '|' + doc['"+ EntityIds.ElementAt(0) + ".keyword'].value")).Size(802407))
        //        )
        //       );
        //        var agg = result.Aggregations.Terms("terms_agg").Buckets;
        //        foreach (var items in agg)
        //        {
        //            obj.Add(new FilteredEntityData
        //            {
        //                EntityId = items.Key.ToString().Split("|")[1],
        //                EntityName = items.Key.ToString().Split("|")[0],
        //                IsSelectedEntity = sFilterArray.Contains(items.Key.ToString().Split("|")[1]) ? 1 : 0
        //            });
        //        }
        //        ObjectArray.Add(EntityNames.ElementAt(0), obj);

        //    }
        //    return ObjectArray;

        //}

        //public override Dictionary<string,object> fetchDropDownForMatch(Dictionary<string, object> ObjectArray, string[] sInnings) {
        //    List<FilteredEntityData> obj2 = new List<FilteredEntityData>();
        //    for (int i = 1; i <= 2; i++)
        //    {
        //        obj2.Add(new FilteredEntityData
        //        {
        //            EntityId = i.ToString(),
        //            EntityName = i.ToString(),
        //            IsSelectedEntity = sInnings.Contains(i.ToString()) ? 1 : 0
        //        });
        //    }
        //    ObjectArray.Add("Innings", obj2);
        //    return ObjectArray;
        //}

        public QueryContainer GetS2MomentDetailQueryForST(MatchDetail _objMatchDetail, QueryContainer _objNestedQuery, Moments _objMomentData)
        {
            //List<QueryContainer> myDynamicShouldQuery = new List<QueryContainer>();
            //List<QueryContainer> myDynamicMustQuery = new List<QueryContainer>();
            QueryContainer qShould = new QueryContainer();

            if (_objMomentData != null)
            {
                if (!string.IsNullOrEmpty(_objMomentData.Entities))
                {
                    if (_objMomentData.Entities.Contains(","))
                    {
                        string[] arrEntities = _objMomentData.Entities.Split(',');
                        for (int iEntCtr = 0; iEntCtr < arrEntities.Length; iEntCtr++)
                        {
                            if (!string.IsNullOrEmpty(arrEntities[iEntCtr]))
                            {
                                QueryContainer q1 = new TermQuery {
                                    Field = "EntityId_1", Value = arrEntities[iEntCtr]
                                };
                                QueryContainer q2 = new TermQuery {
                                    Field = "EntityId_2", Value = arrEntities[iEntCtr]
                                };
                                QueryContainer q3 = new TermQuery {
                                    Field = "EntityId_3", Value = arrEntities[iEntCtr]
                                };
                                QueryContainer q4 = new TermQuery {
                                    Field = "EntityId_4", Value = arrEntities[iEntCtr]
                                };
                                QueryContainer q5 = new TermQuery {
                                    Field = "EntityId_5", Value = arrEntities[iEntCtr]
                                };
                                //myDynamicShouldQuery.Add(q1);
                                //myDynamicShouldQuery.Add(q2);
                                //myDynamicShouldQuery.Add(q3);
                                //myDynamicShouldQuery.Add(q4);
                                qShould         |= q1 |= q2 |= q3 |= q4 |= q5;
                                _objNestedQuery &= qShould;
                            }
                        }
                    }
                    else
                    {
                        QueryContainer q12 = new TermQuery {
                            Field = "EntityId_1", Value = _objMomentData.Entities
                        };

                        _objNestedQuery &= q12;
                    }
                }

                if (_objMomentData.IsBigMoment || _objMomentData.IsFunnyMoment || _objMomentData.IsAudioPiece)
                {
                    if (_objMomentData.IsBigMoment)
                    {
                        QueryContainer q6 = new TermQuery {
                            Field = "IsBigMoment", Value = "1"
                        };
                        //myDynamicMustQuery.Add(q6);
                        _objNestedQuery &= q6;
                    }
                    if (_objMomentData.IsFunnyMoment)
                    {
                        QueryContainer q7 = new TermQuery {
                            Field = "IsFunnyMoment", Value = "1"
                        };
                        //myDynamicMustQuery.Add(q7);
                        _objNestedQuery &= q7;
                    }
                    if (_objMomentData.IsAudioPiece)
                    {
                        QueryContainer q8 = new TermQuery {
                            Field = "IsAudioPiece", Value = "1"
                        };
                        //myDynamicMustQuery.Add(q8);
                        _objNestedQuery &= q8;
                    }
                }
                else
                {
                    QueryContainer q9 = new TermQuery {
                        Field = "IsBigMoment", Value = "1"
                    };
                    QueryContainer q10 = new TermQuery {
                        Field = "IsFunnyMoment", Value = "1"
                    };
                    QueryContainer q11 = new TermQuery {
                        Field = "IsAudioPiece", Value = "1"
                    };
                    //myDynamicShouldQuery.Add(q9);
                    //myDynamicShouldQuery.Add(q10);
                    //myDynamicShouldQuery.Add(q11);
                    qShould         |= q9 |= q10 |= q11;
                    _objNestedQuery &= qShould;

                    //Bq.Add(bq, Occur.MUST);
                }
            }
            return(_objNestedQuery);
        }
 public virtual void TestRandomSearchPerformance()
 {
     IndexSearcher searcher = new IndexSearcher(Reader);
     foreach (Term t in SampleTerms)
     {
         TermQuery query = new TermQuery(t);
         TopDocs topDocs = searcher.Search(query, 10);
         Assert.IsTrue(topDocs.TotalHits > 0);
     }
 }
        public SearchResults Search(SearchRequest request)
        {
            if (!OpenReader())
            {
                return(new SearchResults {
                    Query = request.Query
                });
            }

            if (!reader.IsCurrent())
            {
                reader = reader.Reopen();
            }

            var take = request.Take > 0 ? request.Take : SearchModuleConstants.DefaultSearchResultsCount;
            var skip = request.Skip > 0 ? request.Skip : 0;

            var result   = new List <SearchResultItem>();
            var searcher = new IndexSearcher(reader);
            TopScoreDocCollector collector = TopScoreDocCollector.Create(take + skip, true);

            var   searchQuery = request.Query;
            Query query;

            try
            {
                query = parser.Parse(searchQuery);
            }
            catch (ParseException)
            {
                try
                {
                    searchQuery = QueryParser.Escape(searchQuery);
                    query       = parser.Parse(searchQuery);
                }
                catch (ParseException exc)
                {
                    throw new ValidationException(() => exc.Message, exc.Message, exc);
                }
            }

            if (!RetrieveUnpublishedPages())
            {
                // Exclude unpublished pages
                var    isPublishedQuery  = new TermQuery(new Term(LuceneIndexDocumentKeys.IsPublished, "true"));
                Filter isPublishedFilter = new QueryWrapperFilter(isPublishedQuery);

                searcher.Search(query, isPublishedFilter, collector);
            }
            else
            {
                // Search within all the pages
                searcher.Search(query, collector);
            }

            ScoreDoc[] hits = collector.TopDocs(skip, take).ScoreDocs;

            for (int i = 0; i < hits.Length; i++)
            {
                int      docId = hits[i].Doc;
                Document d     = searcher.Doc(docId);
                result.Add(new SearchResultItem
                {
                    FormattedUrl = d.Get(LuceneIndexDocumentKeys.Path),
                    Link         = d.Get(LuceneIndexDocumentKeys.Path),
                    Title        = d.Get(LuceneIndexDocumentKeys.Title),
                    Snippet      = GetSnippet(d.Get(LuceneIndexDocumentKeys.Content), request.Query)
                });
            }

            CheckAvailability(result);

            return(new SearchResults
            {
                Items = result,
                Query = request.Query,
                TotalResults = collector.TotalHits
            });
        }
Example #53
0
 public override void Run()
 {
     IndexReader reader = null;
     IndexSearcher searcher = null;
     Query query = new TermQuery(new Term("content", "aaa"));
     for (int i = 0; i < this.NumIteration; i++)
     {
         try
         {
             reader = DirectoryReader.Open(Dir);
             searcher = NewSearcher(reader);
         }
         catch (Exception e)
         {
             HitException = true;
             Console.WriteLine("Stress Test Index Searcher: create hit unexpected exception: " + e.ToString());
             Console.Out.Write(e.StackTrace);
             break;
         }
         try
         {
             searcher.Search(query, null, 1000);
         }
         catch (IOException e)
         {
             HitException = true;
             Console.WriteLine("Stress Test Index Searcher: search hit unexpected exception: " + e.ToString());
             Console.Out.Write(e.StackTrace);
             break;
         }
         // System.out.println(hits.Length() + " total results");
         try
         {
             reader.Dispose();
         }
         catch (IOException e)
         {
             HitException = true;
             Console.WriteLine("Stress Test Index Searcher: close hit unexpected exception: " + e.ToString());
             Console.Out.Write(e.StackTrace);
             break;
         }
     }
 }
Example #54
0
        private Document GetSingleDocumentForSearchTerm(string termField, string termValue)
        {
            var query = new TermQuery(new Term(termField, termValue));

            return(this.GetSingleDocumentForQuery(query));
        }
Example #55
0
        public virtual void TestFarsiRangeFilterCollating(Analyzer analyzer, BytesRef firstBeg, BytesRef firstEnd, BytesRef secondBeg, BytesRef secondEnd)
        {
            Directory dir = NewDirectory();
            IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
            Document doc = new Document();
            doc.Add(new TextField("content", "\u0633\u0627\u0628", Field.Store.YES));
            doc.Add(new StringField("body", "body", Field.Store.YES));
            writer.AddDocument(doc);
            writer.Dispose();
            IndexReader reader = DirectoryReader.Open(dir);
            IndexSearcher searcher = new IndexSearcher(reader);
            Query query = new TermQuery(new Term("body", "body"));

            // Unicode order would include U+0633 in [ U+062F - U+0698 ], but Farsi
            // orders the U+0698 character before the U+0633 character, so the single
            // index Term below should NOT be returned by a TermRangeFilter with a Farsi
            // Collator (or an Arabic one for the case when Farsi searcher not
            // supported).
            ScoreDoc[] result = searcher.Search(query, new TermRangeFilter("content", firstBeg, firstEnd, true, true), 1).ScoreDocs;
            Assert.AreEqual(0, result.Length, "The index Term should not be included.");

            result = searcher.Search(query, new TermRangeFilter("content", secondBeg, secondEnd, true, true), 1).ScoreDocs;
            Assert.AreEqual(1, result.Length, "The index Term should be included.");

            reader.Dispose();
            dir.Dispose();
        }
Example #56
0
        public virtual void TestKeepLastNDeletionPolicyWithCreates()
        {
            const int N = 10;

            for (int pass = 0; pass < 2; pass++)
            {
                bool useCompoundFile = (pass % 2) != 0;

                Directory         dir  = NewDirectory();
                IndexWriterConfig conf = (IndexWriterConfig)NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)).SetOpenMode(OpenMode.CREATE).SetIndexDeletionPolicy(new KeepLastNDeletionPolicy(this, N)).SetMaxBufferedDocs(10);
                MergePolicy       mp   = conf.MergePolicy;
                mp.NoCFSRatio = useCompoundFile ? 1.0 : 0.0;
                IndexWriter             writer = new IndexWriter(dir, conf);
                KeepLastNDeletionPolicy policy = (KeepLastNDeletionPolicy)writer.Config.IndexDeletionPolicy;
                writer.Dispose();
                Term  searchTerm = new Term("content", "aaa");
                Query query      = new TermQuery(searchTerm);

                for (int i = 0; i < N + 1; i++)
                {
                    conf          = (IndexWriterConfig)NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)).SetOpenMode(OpenMode.APPEND).SetIndexDeletionPolicy(policy).SetMaxBufferedDocs(10);
                    mp            = conf.MergePolicy;
                    mp.NoCFSRatio = useCompoundFile ? 1.0 : 0.0;
                    writer        = new IndexWriter(dir, conf);
                    policy        = (KeepLastNDeletionPolicy)writer.Config.IndexDeletionPolicy;
                    for (int j = 0; j < 17; j++)
                    {
                        AddDocWithID(writer, i * (N + 1) + j);
                    }
                    // this is a commit
                    writer.Dispose();
                    conf   = (new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random))).SetIndexDeletionPolicy(policy).SetMergePolicy(NoMergePolicy.COMPOUND_FILES);
                    writer = new IndexWriter(dir, conf);
                    policy = (KeepLastNDeletionPolicy)writer.Config.IndexDeletionPolicy;
                    writer.DeleteDocuments(new Term("id", "" + (i * (N + 1) + 3)));
                    // this is a commit
                    writer.Dispose();
                    IndexReader   reader   = DirectoryReader.Open(dir);
                    IndexSearcher searcher = NewSearcher(reader);
                    ScoreDoc[]    hits     = searcher.Search(query, null, 1000).ScoreDocs;
                    Assert.AreEqual(16, hits.Length);
                    reader.Dispose();

                    writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)).SetOpenMode(OpenMode.CREATE).SetIndexDeletionPolicy(policy));
                    policy = (KeepLastNDeletionPolicy)writer.Config.IndexDeletionPolicy;
                    // this will not commit: there are no changes
                    // pending because we opened for "create":
                    writer.Dispose();
                }

                Assert.AreEqual(3 * (N + 1) + 1, policy.numOnInit);
                Assert.AreEqual(3 * (N + 1) + 1, policy.numOnCommit);

                IndexReader   rwReader  = DirectoryReader.Open(dir);
                IndexSearcher searcher_ = NewSearcher(rwReader);
                ScoreDoc[]    hits_     = searcher_.Search(query, null, 1000).ScoreDocs;
                Assert.AreEqual(0, hits_.Length);

                // Simplistic check: just verify only the past N segments_N's still
                // exist, and, I can open a reader on each:
                long gen = SegmentInfos.GetLastCommitGeneration(dir);

                dir.DeleteFile(IndexFileNames.SEGMENTS_GEN);
                int expectedCount = 0;

                rwReader.Dispose();

                for (int i = 0; i < N + 1; i++)
                {
                    try
                    {
                        IndexReader reader = DirectoryReader.Open(dir);

                        // Work backwards in commits on what the expected
                        // count should be.
                        searcher_ = NewSearcher(reader);
                        hits_     = searcher_.Search(query, null, 1000).ScoreDocs;
                        Assert.AreEqual(expectedCount, hits_.Length);
                        if (expectedCount == 0)
                        {
                            expectedCount = 16;
                        }
                        else if (expectedCount == 16)
                        {
                            expectedCount = 17;
                        }
                        else if (expectedCount == 17)
                        {
                            expectedCount = 0;
                        }
                        reader.Dispose();
                        if (i == N)
                        {
                            Assert.Fail("should have failed on commits before last " + N);
                        }
                    }
                    catch (Exception e) when(e.IsIOException())
                    {
                        if (i != N)
                        {
                            throw; // LUCENENET: CA2200: Rethrow to preserve stack details (https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2200-rethrow-to-preserve-stack-details)
                        }
                    }
                    if (i < N)
                    {
                        dir.DeleteFile(IndexFileNames.FileNameFromGeneration(IndexFileNames.SEGMENTS, "", gen));
                    }
                    gen--;
                }

                dir.Dispose();
            }
        }
        public ISearchResponse<Book> SearchBookWithAggregationFilters(string criteria = "", string searchField = "", 
            List<string> refinements = null,
            Dictionary<string,string> SearchFilters = null,
            int count = 10)
        {
            QueryContainer _query;

            if (String.IsNullOrEmpty(criteria))
            {
                _query = new MatchAllQuery();
            }
            else
            {
                if (String.IsNullOrEmpty(searchField))
                {
                    searchField = "_all";

                    _query = new MatchQuery()
                    {
                        Field = searchField,
                        Query = criteria
                    };
                }
                else
                {
                    _query = new TermQuery()
                    {
                        Field = searchField,
                        Value = criteria
                    };
                }

            }

            SearchRequest searchRequest = new SearchRequest
            {

                From = 0,
                Size = count,
                Query = _query
            };

            if (refinements != null && refinements.Count > 0)
            {
                var _aggregations = new Dictionary<string, IAggregationContainer>();

                foreach (var field in refinements)
                {
                    _aggregations.Add(field, new AggregationContainer
                    {
                        Terms = new TermsAggregator
                        {
                            Field = field
                        }
                    });
                }

                searchRequest.Aggregations = _aggregations;

            }

            if (SearchFilters != null && SearchFilters.Count > 0)
            {
                var searchFilterConfig = new FilterContainer();

                foreach(var sf in SearchFilters)
                {
                    searchFilterConfig = Filter<Book>.Term( sf.Key, sf.Value );
                }

                searchRequest.Filter = searchFilterConfig;
            }

            return esClient.Search<Book>(searchRequest);
        }
Example #58
0
        public virtual void TestBasic()
        {
            Directory   dir      = NewDirectory();
            Analyzer    analyzer = new MockAnalyzer(Random);
            IndexWriter writer   = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer).SetMaxBufferedDocs(2).SetSimilarity(new SimpleSimilarity()).SetMergePolicy(NewLogMergePolicy(2)));

            StringBuilder sb   = new StringBuilder(265);
            string        term = "term";

            for (int i = 0; i < 30; i++)
            {
                Document doc = new Document();
                sb.Append(term).Append(" ");
                string content = sb.ToString();
                Field  noTf    = NewField("noTf", content + (i % 2 == 0 ? "" : " notf"), OmitType);
                doc.Add(noTf);

                Field tf = NewField("tf", content + (i % 2 == 0 ? " tf" : ""), NormalType);
                doc.Add(tf);

                writer.AddDocument(doc);
                //System.out.println(d);
            }

            writer.ForceMerge(1);
            // flush
            writer.Dispose();

            /*
             * Verify the index
             */
            IndexReader   reader   = DirectoryReader.Open(dir);
            IndexSearcher searcher = NewSearcher(reader);

            searcher.Similarity = new SimpleSimilarity();

            Term      a  = new Term("noTf", term);
            Term      b  = new Term("tf", term);
            Term      c  = new Term("noTf", "notf");
            Term      d  = new Term("tf", "tf");
            TermQuery q1 = new TermQuery(a);
            TermQuery q2 = new TermQuery(b);
            TermQuery q3 = new TermQuery(c);
            TermQuery q4 = new TermQuery(d);

            PhraseQuery pq = new PhraseQuery();

            pq.Add(a);
            pq.Add(c);
            try
            {
                searcher.Search(pq, 10);
                Assert.Fail("did not hit expected exception");
            }
            catch (Exception e)
            {
                Exception cause = e;
                // If the searcher uses an executor service, the IAE is wrapped into other exceptions
                while (cause.InnerException != null)
                {
                    cause = cause.InnerException;
                }
                if (!(cause is InvalidOperationException))
                {
                    throw new InvalidOperationException("Expected an IAE", e);
                } // else OK because positions are not indexed
            }

            searcher.Search(q1, new CountingHitCollectorAnonymousInnerClassHelper(this));
            //System.out.println(CountingHitCollector.getCount());

            searcher.Search(q2, new CountingHitCollectorAnonymousInnerClassHelper2(this));
            //System.out.println(CountingHitCollector.getCount());

            searcher.Search(q3, new CountingHitCollectorAnonymousInnerClassHelper3(this));
            //System.out.println(CountingHitCollector.getCount());

            searcher.Search(q4, new CountingHitCollectorAnonymousInnerClassHelper4(this));
            //System.out.println(CountingHitCollector.getCount());

            BooleanQuery bq = new BooleanQuery();

            bq.Add(q1, Occur.MUST);
            bq.Add(q4, Occur.MUST);

            searcher.Search(bq, new CountingHitCollectorAnonymousInnerClassHelper5(this));
            Assert.AreEqual(15, CountingHitCollector.Count);

            reader.Dispose();
            dir.Dispose();
        }
		public SearchRequestTests()
		{
			QueryContainer query = new TermQuery()
			{
				Field = Property.Path<ElasticsearchProject>(p=>p.Name),
				Value = "value"
			} && new PrefixQuery()
			{
				Field = "prefix_field", 
				Value = "prefi", 
				Rewrite = RewriteMultiTerm.ConstantScoreBoolean
			};

			var request = new SearchRequest<ElasticsearchProject>
			{
				From = 0,
				Size = 20,
				Explain = true,
				TrackScores = true,
				MinScore = 2.1,
				IndicesBoost = new Dictionary<IndexNameMarker, double>
				{
					{ Infer.Index<ElasticsearchProject>(), 2.3 }
				},
				Sort = new List<KeyValuePair<PropertyPathMarker, ISort>>()
				{
					new KeyValuePair<PropertyPathMarker, ISort>("field", new Sort { Order = SortOrder.Ascending, Missing = "_first"})
				},
				Facets = new Dictionary<PropertyPathMarker, IFacetContainer>()
				{
					{ "name", new FacetContainer
					{
						Terms = new TermFacetRequest
						{
							Field = "field",
							Size = 10,
							FacetFilter = new TermFilter()
							{
								Field = "other_field",
								Value = "term"
							}.ToContainer()
						}
					}
					}
				},
				Suggest = new Dictionary<string, ISuggestBucket>
				{
					{
						"suggestion", new SuggestBucket
						{
							Text = "suggest me",
							Completion = new CompletionSuggester
							{
								Analyzer = "standard",
								Field = Property.Path<ElasticsearchProject>(p=>p.Content),
								Size = 4,
								ShardSize = 10,
								Fuzzy = new FuzzySuggester
								{
									Fuzziness = Fuzziness.Ratio(0.3),
									PrefixLength = 4
								}

							}
						}
					}
				},
				Rescore = new Rescore
				{
					WindowSize = 10,
					Query = new RescoreQuery
					{
						Query = new TermQuery() { }.ToContainer(),
						QueryWeight = 1.2,
						RescoreQueryWeight = 2.1
					}
				},
				Fields = new[]
				{
					"field",
					Property.Path<ElasticsearchProject>(p=>p.Name)
				},
				ScriptFields = new FluentDictionary<string, IScriptFilter>()
					.Add("script_field_name", new ScriptFilter
					{
						Script = "doc['loc'].value * multiplier",
						Params = new Dictionary<string, object>
						{
							{"multiplier", 4}
						}
					}),
				Source = new SourceFilter
				{
					Include = new PropertyPathMarker[]
					{
						"na*"
					}
				},
				Aggregations = new Dictionary<string, IAggregationContainer>
				{
					{ "my_agg", new AggregationContainer
					{
						Terms = new TermsAggregator
						{
							Field = Property.Path<ElasticsearchProject>(p=>p.Name),
							Size = 10,
							ExecutionHint = TermsAggregationExecutionHint.GlobalOrdinals,
						},
						Aggregations = new Dictionary<string, IAggregationContainer>
						{
							{ "max_count", new AggregationContainer()
							{
								Max = new MaxAggregator()
								{
									Field = "loc"
								}
							}
							}
						}
					}}
				},
				Query = query,
				Filter = new FilterContainer(new BoolFilter
				{
					Cache = true,
					Must = new FilterContainer[]
					{
						new TermFilter { Field = "value", Value = "asdasd"}
					}
				})
			};
			var response = this._client.Search<ElasticsearchProject>(request);
			this._status = response.ConnectionStatus;
		}
Example #60
0
        public IList <SearchResult> SearchTerm(string searchTerm, FullTextFieldType field)
        {
            Query query = new TermQuery(new Term(field.ToString(), searchTerm));

            return(this.RunQuery(query));
        }