use of org.apache.lucene.search.spans.SpanTermQuery in project lucene-solr by apache.
the class TestSpanQueryParserSimpleSample method testBasicDemo.
public void testBasicDemo() throws Exception {
SyntaxParser queryParser = new StandardSyntaxParser();
// convert the CharSequence into a QueryNode tree
QueryNode queryTree = queryParser.parse("body:text", null);
// create a config handler with a attribute used in
// UniqueFieldQueryNodeProcessor
QueryConfigHandler spanQueryConfigHandler = new SpansQueryConfigHandler();
spanQueryConfigHandler.set(SpansQueryConfigHandler.UNIQUE_FIELD, "index");
// set up the processor pipeline with the ConfigHandler
// and create the pipeline for this simple demo
QueryNodeProcessorPipeline spanProcessorPipeline = new QueryNodeProcessorPipeline(spanQueryConfigHandler);
// @see SpansValidatorQueryNodeProcessor
spanProcessorPipeline.add(new SpansValidatorQueryNodeProcessor());
// @see UniqueFieldQueryNodeProcessor
spanProcessorPipeline.add(new UniqueFieldQueryNodeProcessor());
// print to show out the QueryNode tree before being processed
if (VERBOSE)
System.out.println(queryTree);
// Process the QueryTree using our new Processors
queryTree = spanProcessorPipeline.process(queryTree);
// print to show out the QueryNode tree after being processed
if (VERBOSE)
System.out.println(queryTree);
// create a instance off the Builder
SpansQueryTreeBuilder spansQueryTreeBuilder = new SpansQueryTreeBuilder();
// convert QueryNode tree to span query Objects
SpanQuery spanquery = spansQueryTreeBuilder.build(queryTree);
assertTrue(spanquery instanceof SpanTermQuery);
assertEquals(spanquery.toString(), "index:text");
}
use of org.apache.lucene.search.spans.SpanTermQuery in project lucene-solr by apache.
the class TestPositionIncrement method testPayloadsPos0.
public void testPayloadsPos0() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir, new MockPayloadAnalyzer());
Document doc = new Document();
doc.add(new TextField("content", new StringReader("a a b c d e a f g h i j a b k k")));
writer.addDocument(doc);
final IndexReader readerFromWriter = writer.getReader();
LeafReader r = getOnlyLeafReader(readerFromWriter);
PostingsEnum tp = r.postings(new Term("content", "a"), PostingsEnum.ALL);
int count = 0;
assertTrue(tp.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
// "a" occurs 4 times
assertEquals(4, tp.freq());
assertEquals(0, tp.nextPosition());
assertEquals(1, tp.nextPosition());
assertEquals(3, tp.nextPosition());
assertEquals(6, tp.nextPosition());
// only one doc has "a"
assertEquals(DocIdSetIterator.NO_MORE_DOCS, tp.nextDoc());
IndexSearcher is = newSearcher(getOnlyLeafReader(readerFromWriter));
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, 30, false);
count = 0;
boolean sawZero = false;
if (VERBOSE) {
System.out.println("\ngetPayloadSpans test");
}
PayloadSpanCollector collector = new PayloadSpanCollector();
Spans pspans = snq.createWeight(is, false, 1f).getSpans(is.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS);
while (pspans.nextDoc() != Spans.NO_MORE_DOCS) {
while (pspans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
if (VERBOSE) {
System.out.println("doc " + pspans.docID() + ": span " + pspans.startPosition() + " to " + pspans.endPosition());
}
collector.reset();
pspans.collect(collector);
sawZero |= pspans.startPosition() == 0;
for (BytesRef payload : collector.payloads) {
count++;
if (VERBOSE) {
System.out.println(" payload: " + Term.toString(payload));
}
}
}
}
assertTrue(sawZero);
assertEquals(8, count);
// System.out.println("\ngetSpans test");
Spans spans = snq.createWeight(is, false, 1f).getSpans(is.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
count = 0;
sawZero = false;
while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
count++;
sawZero |= spans.startPosition() == 0;
// System.out.println(spans.doc() + " - " + spans.start() + " - " +
// spans.end());
}
}
assertEquals(4, count);
assertTrue(sawZero);
writer.close();
is.getIndexReader().close();
dir.close();
}
use of org.apache.lucene.search.spans.SpanTermQuery in project lucene-solr by apache.
the class TestPayloadExplanations method testSimpleTerm.
public void testSimpleTerm() throws Exception {
SpanTermQuery q = new SpanTermQuery(new Term(FIELD, "w2"));
testAllFunctions(q, new int[] { 0, 1, 2, 3 });
}
use of org.apache.lucene.search.spans.SpanTermQuery in project lucene-solr by apache.
the class TestPayloadExplanations method testOrderedNearQuery.
public void testOrderedNearQuery() throws Exception {
SpanNearQuery q = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term(FIELD, "w3")), new SpanTermQuery(new Term(FIELD, "w2")) }, 1, true);
testAllFunctions(q, new int[] { 1, 3 });
}
use of org.apache.lucene.search.spans.SpanTermQuery in project lucene-solr by apache.
the class TestPayloadExplanations method testUnorderedNearQuery.
public void testUnorderedNearQuery() throws Exception {
SpanNearQuery q = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term(FIELD, "w2")), new SpanTermQuery(new Term(FIELD, "w3")) }, 1, false);
testAllFunctions(q, new int[] { 0, 1, 2, 3 });
}
Aggregations