use of org.apache.lucene.search.spans.SpanQuery 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.SpanQuery in project lucene-solr by apache.
the class PayloadScoreQParserPlugin method createParser.
@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
return new QParser(qstr, localParams, params, req) {
@Override
public Query parse() throws SyntaxError {
String field = localParams.get(QueryParsing.F);
String value = localParams.get(QueryParsing.V);
String func = localParams.get("func");
boolean includeSpanScore = localParams.getBool("includeSpanScore", false);
if (field == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'f' not specified");
}
if (value == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "query string missing");
}
FieldType ft = req.getCore().getLatestSchema().getFieldType(field);
Analyzer analyzer = ft.getQueryAnalyzer();
SpanQuery query = null;
try {
query = PayloadUtils.createSpanQuery(field, value, analyzer);
} catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
}
if (query == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "SpanQuery is null");
}
// note: this query(/parser) does not support func=first; 'first' is a payload() value source feature only
PayloadFunction payloadFunction = PayloadUtils.getPayloadFunction(func);
if (payloadFunction == null)
throw new SyntaxError("Unknown payload function: " + func);
return new PayloadScoreQuery(query, payloadFunction, includeSpanScore);
}
};
}
use of org.apache.lucene.search.spans.SpanQuery 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.SpanQuery in project lucene-solr by apache.
the class TestPayloadExplanations method testPT2.
public void testPT2() throws Exception {
for (PayloadFunction fn : functions) {
SpanQuery q = pt("w1", fn);
qtest(new SpanBoostQuery(q, 1000), new int[] { 0, 1, 2, 3 });
}
}
use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.
the class TestPayloadTermQuery method testNoPayload.
public void testNoPayload() throws Exception {
SpanQuery q1 = new PayloadScoreQuery(new SpanTermQuery(new Term(PayloadHelper.NO_PAYLOAD_FIELD, "zero")), new MaxPayloadFunction());
SpanQuery q2 = new PayloadScoreQuery(new SpanTermQuery(new Term(PayloadHelper.NO_PAYLOAD_FIELD, "foo")), new MaxPayloadFunction());
BooleanClause c1 = new BooleanClause(q1, BooleanClause.Occur.MUST);
BooleanClause c2 = new BooleanClause(q2, BooleanClause.Occur.MUST_NOT);
BooleanQuery.Builder query = new BooleanQuery.Builder();
query.add(c1);
query.add(c2);
TopDocs hits = searcher.search(query.build(), 100);
assertTrue("hits is null and it shouldn't be", hits != null);
assertTrue("hits Size: " + hits.totalHits + " is not: " + 1, hits.totalHits == 1);
int[] results = new int[1];
//hits.scoreDocs[0].doc;
results[0] = 0;
CheckHits.checkHitCollector(random(), query.build(), PayloadHelper.NO_PAYLOAD_FIELD, searcher, results);
}
Aggregations