use of org.apache.lucene.search.spans.SpanTermQuery in project lucene-solr by apache.
the class TestPayloadScoreQuery method testNearQuery.
@Test
public void testNearQuery() throws IOException {
// 2 4
// twenty two
// 2 4 4 4
// one hundred twenty two
SpanNearQuery q = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term("field", "twenty")), new SpanTermQuery(new Term("field", "two")) }, 0, true);
checkQuery(q, new MaxPayloadFunction(), new int[] { 22, 122, 222 }, new float[] { 4.0f, 4.0f, 4.0f });
checkQuery(q, new MinPayloadFunction(), new int[] { 122, 222, 22 }, new float[] { 4.0f, 4.0f, 2.0f });
checkQuery(q, new AveragePayloadFunction(), new int[] { 122, 222, 22 }, new float[] { 4.0f, 4.0f, 3.0f });
}
use of org.apache.lucene.search.spans.SpanTermQuery in project lucene-solr by apache.
the class TestPayloadSpans method testShrinkToAfterShortestMatch2.
public void testShrinkToAfterShortestMatch2() throws IOException {
Directory directory = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), directory, newIndexWriterConfig(new TestPayloadAnalyzer()));
Document doc = new Document();
doc.add(new TextField("content", new StringReader("a b a d k f a h i k a k")));
writer.addDocument(doc);
IndexReader reader = writer.getReader();
IndexSearcher is = newSearcher(getOnlyLeafReader(reader), false);
writer.close();
SpanTermQuery stq1 = new SpanTermQuery(new Term("content", "a"));
SpanTermQuery stq2 = new SpanTermQuery(new Term("content", "k"));
SpanQuery[] sqs = { stq1, stq2 };
SpanNearQuery snq = new SpanNearQuery(sqs, 0, true);
VerifyingCollector collector = new VerifyingCollector();
Spans spans = snq.createWeight(is, false, 1f).getSpans(is.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS);
TopDocs topDocs = is.search(snq, 1);
Set<String> payloadSet = new HashSet<>();
for (int i = 0; i < topDocs.scoreDocs.length; i++) {
while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
collector.reset();
spans.collect(collector);
for (final BytesRef payload : collector.payloads) {
payloadSet.add(Term.toString(payload));
}
}
}
}
assertEquals(2, payloadSet.size());
assertTrue(payloadSet.contains("a:Noise:10"));
assertTrue(payloadSet.contains("k:Noise:11"));
reader.close();
directory.close();
}
use of org.apache.lucene.search.spans.SpanTermQuery in project lucene-solr by apache.
the class TestPayloadCheckQuery method testUnorderedPayloadChecks.
public void testUnorderedPayloadChecks() throws Exception {
SpanTermQuery term5 = new SpanTermQuery(new Term("field", "five"));
SpanTermQuery term100 = new SpanTermQuery(new Term("field", "hundred"));
SpanTermQuery term4 = new SpanTermQuery(new Term("field", "four"));
SpanNearQuery nearQuery = new SpanNearQuery(new SpanQuery[] { term5, term100, term4 }, 0, false);
List<BytesRef> payloads = new ArrayList<>();
payloads.add(new BytesRef("pos: " + 2));
payloads.add(new BytesRef("pos: " + 1));
payloads.add(new BytesRef("pos: " + 0));
SpanPayloadCheckQuery payloadQuery = new SpanPayloadCheckQuery(nearQuery, payloads);
checkHits(payloadQuery, new int[] { 405 });
payloads.clear();
payloads.add(new BytesRef("pos: " + 0));
payloads.add(new BytesRef("pos: " + 1));
payloads.add(new BytesRef("pos: " + 2));
payloadQuery = new SpanPayloadCheckQuery(nearQuery, payloads);
checkHits(payloadQuery, new int[] { 504 });
}
use of org.apache.lucene.search.spans.SpanTermQuery in project lucene-solr by apache.
the class TestPayloadCheckQuery method testEquality.
public void testEquality() {
SpanQuery sq1 = new SpanTermQuery(new Term("field", "one"));
SpanQuery sq2 = new SpanTermQuery(new Term("field", "two"));
BytesRef payload1 = new BytesRef("pay1");
BytesRef payload2 = new BytesRef("pay2");
SpanQuery query1 = new SpanPayloadCheckQuery(sq1, Collections.singletonList(payload1));
SpanQuery query2 = new SpanPayloadCheckQuery(sq2, Collections.singletonList(payload1));
SpanQuery query3 = new SpanPayloadCheckQuery(sq1, Collections.singletonList(payload2));
SpanQuery query4 = new SpanPayloadCheckQuery(sq2, Collections.singletonList(payload2));
SpanQuery query5 = new SpanPayloadCheckQuery(sq1, Collections.singletonList(payload1));
assertEquals(query1, query5);
assertFalse(query1.equals(query2));
assertFalse(query1.equals(query3));
assertFalse(query1.equals(query4));
assertFalse(query2.equals(query3));
assertFalse(query2.equals(query4));
assertFalse(query3.equals(query4));
}
use of org.apache.lucene.search.spans.SpanTermQuery 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 });
}
Aggregations