use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.
the class TestUnifiedHighlighterMTQ method testSpanNear.
public void testSpanNear() throws Exception {
RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);
Field body = new Field("body", "", fieldType);
Document doc = new Document();
doc.add(body);
body.setStringValue("This is a test.");
iw.addDocument(doc);
body.setStringValue("Test a one sentence document.");
iw.addDocument(doc);
IndexReader ir = iw.getReader();
iw.close();
IndexSearcher searcher = newSearcher(ir);
UnifiedHighlighter highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);
SpanQuery childQuery = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("body", "te*")));
Query query = new SpanNearQuery(new SpanQuery[] { childQuery, childQuery }, 0, false);
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
assertEquals(2, topDocs.totalHits);
String[] snippets = highlighter.highlight("body", query, topDocs);
assertEquals(2, snippets.length);
assertEquals("This is a <b>test</b>.", snippets[0]);
assertEquals("<b>Test</b> a one sentence document.", snippets[1]);
ir.close();
}
use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.
the class TestUnifiedHighlighterMTQ method testSpanOr.
public void testSpanOr() throws Exception {
RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);
Field body = new Field("body", "", fieldType);
Document doc = new Document();
doc.add(body);
body.setStringValue("This is a test.");
iw.addDocument(doc);
body.setStringValue("Test a one sentence document.");
iw.addDocument(doc);
IndexReader ir = iw.getReader();
iw.close();
IndexSearcher searcher = newSearcher(ir);
UnifiedHighlighter highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);
SpanQuery childQuery = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("body", "te*")));
Query query = new SpanOrQuery(new SpanQuery[] { childQuery });
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
assertEquals(2, topDocs.totalHits);
String[] snippets = highlighter.highlight("body", query, topDocs);
assertEquals(2, snippets.length);
assertEquals("This is a <b>test</b>.", snippets[0]);
assertEquals("<b>Test</b> a one sentence document.", snippets[1]);
ir.close();
}
use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.
the class SpanOrQueryNodeBuilder method build.
@Override
public SpanOrQuery build(QueryNode node) throws QueryNodeException {
// validates node
BooleanQueryNode booleanNode = (BooleanQueryNode) node;
List<QueryNode> children = booleanNode.getChildren();
SpanQuery[] spanQueries = new SpanQuery[children.size()];
int i = 0;
for (QueryNode child : children) {
spanQueries[i++] = (SpanQuery) child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
}
return new SpanOrQuery(spanQueries);
}
use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.
the class BoostingTermBuilder method getSpanQuery.
@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
String value = DOMUtils.getNonBlankTextOrFail(e);
SpanQuery btq = new PayloadScoreQuery(new SpanTermQuery(new Term(fieldName, value)), new AveragePayloadFunction());
btq = new SpanBoostQuery(btq, DOMUtils.getAttribute(e, "boost", 1.0f));
return btq;
}
use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.
the class SpanNotBuilder method getSpanQuery.
@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
Element includeElem = DOMUtils.getChildByTagOrFail(e, "Include");
includeElem = DOMUtils.getFirstChildOrFail(includeElem);
Element excludeElem = DOMUtils.getChildByTagOrFail(e, "Exclude");
excludeElem = DOMUtils.getFirstChildOrFail(excludeElem);
SpanQuery include = factory.getSpanQuery(includeElem);
SpanQuery exclude = factory.getSpanQuery(excludeElem);
SpanNotQuery snq = new SpanNotQuery(include, exclude);
float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
return new SpanBoostQuery(snq, boost);
}
Aggregations