Exemplo n.º 1
0
        public virtual void  TestIgnoreSpanScorer()
        {
            PayloadTermQuery query = new PayloadTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy"), new MaxPayloadFunction(), false);

            IndexSearcher theSearcher = new IndexSearcher(directory, true, null);

            theSearcher.Similarity = new FullSimilarity();
            TopDocs hits = searcher.Search(query, null, 100, null);

            Assert.IsTrue(hits != null, "hits is null and it shouldn't be");
            Assert.IsTrue(hits.TotalHits == 100, "hits Size: " + hits.TotalHits + " is not: " + 100);

            //they should all have the exact same score, because they all contain seventy once, and we set
            //all the other similarity factors to be 1

            //System.out.println("Hash: " + seventyHash + " Twice Hash: " + 2*seventyHash);
            Assert.IsTrue(hits.MaxScore == 4.0, hits.MaxScore + " does not equal: " + 4.0);
            //there should be exactly 10 items that score a 4, all the rest should score a 2
            //The 10 items are: 70 + i*100 where i in [0-9]
            int numTens = 0;

            for (int i = 0; i < hits.ScoreDocs.Length; i++)
            {
                ScoreDoc doc = hits.ScoreDocs[i];
                if (doc.Doc % 10 == 0)
                {
                    numTens++;
                    Assert.IsTrue(doc.Score == 4.0, doc.Score + " does not equal: " + 4.0);
                }
                else
                {
                    Assert.IsTrue(doc.Score == 2, doc.Score + " does not equal: " + 2);
                }
            }
            Assert.IsTrue(numTens == 10, numTens + " does not equal: " + 10);
            CheckHits.CheckExplanations(query, "field", searcher, true);
            Lucene.Net.Search.Spans.Spans spans = query.GetSpans(searcher.IndexReader, null);
            Assert.IsTrue(spans != null, "spans is null and it shouldn't be");
            Assert.IsTrue(spans is TermSpans, "spans is not an instanceof " + typeof(TermSpans));
            //should be two matches per document
            int count = 0;

            //100 hits times 2 matches per hit, we should have 200 in count
            while (spans.Next(null))
            {
                count++;
            }
        }
Exemplo n.º 2
0
        private void  GetPayloads(ICollection <byte[]> payloads, SpanQuery query)
        {
            Lucene.Net.Search.Spans.Spans spans = query.GetSpans(reader);

            while (spans.Next() == true)
            {
                if (spans.IsPayloadAvailable())
                {
                    //ICollection<byte[]> payload = spans.GetPayload();
                    ICollection <byte[]> payload = spans.GetPayload();
                    //IEnumerator<byte[]> it = payload.GetEnumerator();
                    foreach (byte[] bytes in payload)
                    {
                        payloads.Add(bytes);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public override SpanFilterResult BitSpans(IndexReader reader)
        {
            System.Collections.BitArray   bits  = new System.Collections.BitArray((reader.MaxDoc() % 64 == 0 ? reader.MaxDoc() / 64 : reader.MaxDoc() / 64 + 1) * 64);
            Lucene.Net.Search.Spans.Spans spans = query.GetSpans(reader);
            System.Collections.IList      tmp   = new System.Collections.ArrayList(20);
            int currentDoc = -1;

            SpanFilterResult.PositionInfo currentInfo = null;
            while (spans.Next())
            {
                int doc = spans.Doc();
                bits.Set(doc, true);
                if (currentDoc != doc)
                {
                    currentInfo = new SpanFilterResult.PositionInfo(doc);
                    tmp.Add(currentInfo);
                    currentDoc = doc;
                }
                currentInfo.AddPosition(spans.Start(), spans.End());
            }
            return(new SpanFilterResult(bits, tmp));
        }
Exemplo n.º 4
0
        public override SpanFilterResult BitSpans(IndexReader reader, IState state)
        {
            OpenBitSet bits = new OpenBitSet(reader.MaxDoc);

            Lucene.Net.Search.Spans.Spans         spans = internalQuery.GetSpans(reader, state);
            IList <SpanFilterResult.PositionInfo> tmp   = new List <SpanFilterResult.PositionInfo>(20);
            int currentDoc = -1;

            SpanFilterResult.PositionInfo currentInfo = null;
            while (spans.Next(state))
            {
                int doc = spans.Doc();
                bits.Set(doc);
                if (currentDoc != doc)
                {
                    currentInfo = new SpanFilterResult.PositionInfo(doc);
                    tmp.Add(currentInfo);
                    currentDoc = doc;
                }
                currentInfo.AddPosition(spans.Start(), spans.End());
            }
            return(new SpanFilterResult(bits, tmp));
        }
        public virtual void TestPayloadsPos0()
        {
            Directory   dir    = new MockRAMDirectory();
            IndexWriter writer = new IndexWriter(dir, new TestPayloadAnalyzer(), true,
                                                 IndexWriter.MaxFieldLength.LIMITED);
            Document doc = new Document();

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.IO.StreamWriter sw = new System.IO.StreamWriter(ms);
            sw.Write("a a b c d e a f g h i j a b k k");
            // flush to stream & reset it's position so it can be read
            sw.Flush();
            ms.Position = 0;
            doc.Add(new Field("content", new System.IO.StreamReader(ms)));
            writer.AddDocument(doc);

            IndexReader r = writer.GetReader();

            TermPositions tp    = r.TermPositions(new Term("content", "a"));
            int           count = 0;

            Assert.IsTrue(tp.Next());
            // "a" occurs 4 times
            Assert.AreEqual(4, tp.Freq);
            int expected = 0;

            Assert.AreEqual(expected, tp.NextPosition());
            Assert.AreEqual(1, tp.NextPosition());
            Assert.AreEqual(3, tp.NextPosition());
            Assert.AreEqual(6, tp.NextPosition());

            // only one doc has "a"
            Assert.IsFalse(tp.Next());

            IndexSearcher is_Renamed = new IndexSearcher(r);

            SpanTermQuery stq1 = new SpanTermQuery(new Term("content", "a"));
            SpanTermQuery stq2 = new SpanTermQuery(new Term("content", "k"));

            SpanQuery[]   sqs = new SpanQuery[] { stq1, stq2 };
            SpanNearQuery snq = new SpanNearQuery(sqs, 30, false);

            count = 0;
            bool sawZero = false;

            //System.out.println("\ngetPayloadSpans test");
            Lucene.Net.Search.Spans.Spans pspans = snq.GetSpans(is_Renamed.IndexReader);
            while (pspans.Next())
            {
                //System.out.println(pspans.doc() + " - " + pspans.start() + " - "+ pspans.end());
                System.Collections.Generic.ICollection <byte[]> payloads = pspans.GetPayload();
                sawZero |= pspans.Start() == 0;
                for (System.Collections.IEnumerator it = payloads.GetEnumerator(); it.MoveNext();)
                {
                    count++;
                    System.Object generatedAux2 = it.Current;
                    //System.out.println(new String((byte[]) it.next()));
                }
            }
            Assert.AreEqual(5, count);
            Assert.IsTrue(sawZero);

            //System.out.println("\ngetSpans test");
            Lucene.Net.Search.Spans.Spans spans = snq.GetSpans(is_Renamed.IndexReader);
            count   = 0;
            sawZero = false;
            while (spans.Next())
            {
                count++;
                sawZero |= spans.Start() == 0;
                //System.out.println(spans.doc() + " - " + spans.start() + " - " + spans.end());
            }
            Assert.AreEqual(4, count);
            Assert.IsTrue(sawZero);

            //System.out.println("\nPayloadSpanUtil test");

            sawZero = false;
            PayloadSpanUtil psu = new PayloadSpanUtil(is_Renamed.IndexReader);

            System.Collections.Generic.ICollection <byte[]> pls = psu.GetPayloadsForQuery(snq);
            count = pls.Count;
            for (System.Collections.IEnumerator it = pls.GetEnumerator(); it.MoveNext();)
            {
                System.String s = new System.String(System.Text.UTF8Encoding.UTF8.GetChars((byte[])it.Current));
                //System.out.println(s);
                sawZero |= s.Equals("pos: 0");
            }
            Assert.AreEqual(5, count);
            Assert.IsTrue(sawZero);
            writer.Close();
            is_Renamed.IndexReader.Close();
            dir.Close();
        }