Search in sources :

Example 31 with SpanQuery

use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.

the class TestPayloadCheckQuery method testSpanPayloadCheck.

public void testSpanPayloadCheck() throws Exception {
    SpanQuery term1 = new SpanTermQuery(new Term("field", "five"));
    BytesRef pay = new BytesRef("pos: " + 5);
    SpanQuery query = new SpanPayloadCheckQuery(term1, Collections.singletonList(pay));
    checkHits(query, new int[] { 1125, 1135, 1145, 1155, 1165, 1175, 1185, 1195, 1225, 1235, 1245, 1255, 1265, 1275, 1285, 1295, 1325, 1335, 1345, 1355, 1365, 1375, 1385, 1395, 1425, 1435, 1445, 1455, 1465, 1475, 1485, 1495, 1525, 1535, 1545, 1555, 1565, 1575, 1585, 1595, 1625, 1635, 1645, 1655, 1665, 1675, 1685, 1695, 1725, 1735, 1745, 1755, 1765, 1775, 1785, 1795, 1825, 1835, 1845, 1855, 1865, 1875, 1885, 1895, 1925, 1935, 1945, 1955, 1965, 1975, 1985, 1995 });
    assertTrue(searcher.explain(query, 1125).getValue() > 0.0f);
    SpanTermQuery term2 = new SpanTermQuery(new Term("field", "hundred"));
    SpanNearQuery snq;
    SpanQuery[] clauses;
    List<BytesRef> list;
    BytesRef pay2;
    clauses = new SpanQuery[2];
    clauses[0] = term1;
    clauses[1] = term2;
    snq = new SpanNearQuery(clauses, 0, true);
    pay = new BytesRef("pos: " + 0);
    pay2 = new BytesRef("pos: " + 1);
    list = new ArrayList<>();
    list.add(pay);
    list.add(pay2);
    query = new SpanPayloadCheckQuery(snq, list);
    checkHits(query, new int[] { 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599 });
    clauses = new SpanQuery[3];
    clauses[0] = term1;
    clauses[1] = term2;
    clauses[2] = new SpanTermQuery(new Term("field", "five"));
    snq = new SpanNearQuery(clauses, 0, true);
    pay = new BytesRef("pos: " + 0);
    pay2 = new BytesRef("pos: " + 1);
    BytesRef pay3 = new BytesRef("pos: " + 2);
    list = new ArrayList<>();
    list.add(pay);
    list.add(pay2);
    list.add(pay3);
    query = new SpanPayloadCheckQuery(snq, list);
    checkHits(query, new int[] { 505 });
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) BytesRef(org.apache.lucene.util.BytesRef) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 32 with SpanQuery

use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.

the class TestPayloadExplanations method testPT5.

public void testPT5() throws Exception {
    for (PayloadFunction fn : functions) {
        SpanQuery q = pt("xx", fn);
        qtest(new SpanBoostQuery(q, 1000), new int[] { 2, 3 });
    }
}
Also used : SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 33 with SpanQuery

use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.

the class TestPayloadTermQuery method test.

public void test() throws IOException {
    SpanQuery query = new PayloadScoreQuery(new SpanTermQuery(new Term("field", "seventy")), new MaxPayloadFunction());
    TopDocs hits = searcher.search(query, 100);
    assertTrue("hits is null and it shouldn't be", hits != null);
    assertTrue("hits Size: " + hits.totalHits + " is not: " + 100, hits.totalHits == 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
    assertTrue(hits.getMaxScore() + " does not equal: " + 1, hits.getMaxScore() == 1);
    for (int i = 0; i < hits.scoreDocs.length; i++) {
        ScoreDoc doc = hits.scoreDocs[i];
        assertTrue(doc.score + " does not equal: " + 1, doc.score == 1);
    }
    CheckHits.checkExplanations(query, PayloadHelper.FIELD, searcher, true);
    Spans spans = query.createWeight(searcher, false, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
    assertTrue("spans is null and it shouldn't be", spans != null);
/*float score = hits.score(0);
    for (int i =1; i < hits.length(); i++)
    {
      assertTrue("scores are not equal and they should be", score == hits.score(i));
    }*/
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) SpanQuery(org.apache.lucene.search.spans.SpanQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) Spans(org.apache.lucene.search.spans.Spans)

Example 34 with SpanQuery

use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.

the class TestPayloadTermQuery method testNoMatch.

public void testNoMatch() throws Exception {
    SpanQuery query = new PayloadScoreQuery(new SpanTermQuery(new Term(PayloadHelper.FIELD, "junk")), new MaxPayloadFunction());
    TopDocs hits = searcher.search(query, 100);
    assertTrue("hits is null and it shouldn't be", hits != null);
    assertTrue("hits Size: " + hits.totalHits + " is not: " + 0, hits.totalHits == 0);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 35 with SpanQuery

use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.

the class TestPayloadTermQuery method testMultipleMatchesPerDoc.

public void testMultipleMatchesPerDoc() throws Exception {
    SpanQuery query = new PayloadScoreQuery(new SpanTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy")), new MaxPayloadFunction());
    TopDocs hits = searcher.search(query, 100);
    assertTrue("hits is null and it shouldn't be", hits != null);
    assertTrue("hits Size: " + hits.totalHits + " is not: " + 100, hits.totalHits == 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);
    assertTrue(hits.getMaxScore() + " does not equal: " + 4.0, hits.getMaxScore() == 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++;
            assertTrue(doc.score + " does not equal: " + 4.0, doc.score == 4.0);
        } else {
            assertTrue(doc.score + " does not equal: " + 2, doc.score == 2);
        }
    }
    assertTrue(numTens + " does not equal: " + 10, numTens == 10);
    CheckHits.checkExplanations(query, "field", searcher, true);
    Spans spans = query.createWeight(searcher, false, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
    assertTrue("spans is null and it shouldn't be", spans != null);
    //should be two matches per document
    int count = 0;
    //100 hits times 2 matches per hit, we should have 200 in count
    while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
        while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
            count++;
        }
    }
    assertTrue(count + " does not equal: " + 200, count == 200);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) SpanQuery(org.apache.lucene.search.spans.SpanQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) Spans(org.apache.lucene.search.spans.Spans)

Aggregations

SpanQuery (org.apache.lucene.search.spans.SpanQuery)316 Test (org.junit.Test)217 Term (org.apache.lucene.index.Term)127 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)126 KrillIndex (de.ids_mannheim.korap.KrillIndex)110 SpanQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)65 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)52 Result (de.ids_mannheim.korap.response.Result)48 QueryBuilder (de.ids_mannheim.korap.query.QueryBuilder)39 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)35 ArrayList (java.util.ArrayList)34 SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)32 SpanNextQuery (de.ids_mannheim.korap.query.SpanNextQuery)31 Query (org.apache.lucene.search.Query)30 IndexReader (org.apache.lucene.index.IndexReader)19 BooleanQuery (org.apache.lucene.search.BooleanQuery)19 IndexSearcher (org.apache.lucene.search.IndexSearcher)18 SpanBoostQuery (org.apache.lucene.search.spans.SpanBoostQuery)18 SpanMultiTermQueryWrapper (org.apache.lucene.search.spans.SpanMultiTermQueryWrapper)18 Document (org.apache.lucene.document.Document)17