use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.
the class TestCoreParser method testSpanTermXML.
public void testSpanTermXML() throws Exception {
Query q = parse("SpanQuery.xml");
dumpResults("Span Query", q, 5);
SpanQuery sq = parseAsSpan("SpanQuery.xml");
dumpResults("Span Query", sq, 5);
assertEquals(q, sq);
}
use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.
the class ApacheLuceneSolrNearQueryBuilder method getSpanQuery.
public SpanQuery getSpanQuery(Element e) throws ParserException {
final String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
final SpanQuery[] spanQueries = new SpanQuery[] { new SpanTermQuery(new Term(fieldName, "Apache")), new SpanTermQuery(new Term(fieldName, "Lucene")), new SpanTermQuery(new Term(fieldName, "Solr")) };
final int slop = 42;
final boolean inOrder = false;
return new SpanNearQuery(spanQueries, slop, inOrder);
}
use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.
the class QueryBuilder method analyzeGraphPhrase.
/**
* Creates a span near (phrase) query from a graph token stream. The articulation points of the graph are visited in
* order and the queries created at each point are merged in the returned near query.
*/
protected SpanQuery analyzeGraphPhrase(TokenStream source, String field, int phraseSlop) throws IOException {
source.reset();
GraphTokenStreamFiniteStrings graph = new GraphTokenStreamFiniteStrings(source);
List<SpanQuery> clauses = new ArrayList<>();
int[] articulationPoints = graph.articulationPoints();
int lastState = 0;
for (int i = 0; i <= articulationPoints.length; i++) {
int start = lastState;
int end = -1;
if (i < articulationPoints.length) {
end = articulationPoints[i];
}
lastState = end;
final SpanQuery queryPos;
if (graph.hasSidePath(start)) {
List<SpanQuery> queries = new ArrayList<>();
Iterator<TokenStream> it = graph.getFiniteStrings(start, end);
while (it.hasNext()) {
TokenStream ts = it.next();
SpanQuery q = createSpanQuery(ts, field);
if (q != null) {
queries.add(q);
}
}
if (queries.size() > 0) {
queryPos = new SpanOrQuery(queries.toArray(new SpanQuery[0]));
} else {
queryPos = null;
}
} else {
Term[] terms = graph.getTerms(field, start);
assert terms.length > 0;
if (terms.length == 1) {
queryPos = new SpanTermQuery(terms[0]);
} else {
SpanTermQuery[] orClauses = new SpanTermQuery[terms.length];
for (int idx = 0; idx < terms.length; idx++) {
orClauses[idx] = new SpanTermQuery(terms[idx]);
}
queryPos = new SpanOrQuery(orClauses);
}
}
if (queryPos != null) {
clauses.add(queryPos);
}
}
if (clauses.isEmpty()) {
return null;
} else if (clauses.size() == 1) {
return clauses.get(0);
} else {
return new SpanNearQuery(clauses.toArray(new SpanQuery[0]), phraseSlop, true);
}
}
use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.
the class TestSolrCoreParser method testHandySpanQuery.
// test custom query (HandyQueryBuilder) wrapping a SpanQuery
public void testHandySpanQuery() throws IOException, ParserException {
final String lhsXml = "<SpanOr fieldName='contents'>" + "<SpanTerm>rain</SpanTerm>" + "<SpanTerm>spain</SpanTerm>" + "<SpanTerm>plain</SpanTerm>" + "</SpanOr>";
final String rhsXml = "<SpanNear fieldName='contents' slop='2' inOrder='true'>" + "<SpanTerm>sunny</SpanTerm>" + "<SpanTerm>sky</SpanTerm>" + "</SpanNear>";
final Query query = parseHandyQuery(lhsXml, rhsXml);
final BooleanQuery bq = (BooleanQuery) query;
assertEquals(2, bq.clauses().size());
for (int ii = 0; ii < bq.clauses().size(); ++ii) {
final Query clauseQuery = bq.clauses().get(ii).getQuery();
switch(ii) {
case 0:
assertTrue(unwrapSpanBoostQuery(clauseQuery) instanceof SpanOrQuery);
break;
case 1:
assertTrue(unwrapSpanBoostQuery(clauseQuery) instanceof SpanNearQuery);
break;
default:
fail("unexpected clause index " + ii);
}
}
}
use of org.apache.lucene.search.spans.SpanQuery in project lucene-solr by apache.
the class TestBooleanQuery method testBooleanSpanQuery.
// LUCENE-4477 / LUCENE-4401:
public void testBooleanSpanQuery() throws Exception {
boolean failed = false;
int hits = 0;
Directory directory = newDirectory();
Analyzer indexerAnalyzer = new MockAnalyzer(random());
IndexWriterConfig config = new IndexWriterConfig(indexerAnalyzer);
IndexWriter writer = new IndexWriter(directory, config);
String FIELD = "content";
Document d = new Document();
d.add(new TextField(FIELD, "clockwork orange", Field.Store.YES));
writer.addDocument(d);
writer.close();
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher searcher = newSearcher(indexReader);
BooleanQuery.Builder query = new BooleanQuery.Builder();
SpanQuery sq1 = new SpanTermQuery(new Term(FIELD, "clockwork"));
SpanQuery sq2 = new SpanTermQuery(new Term(FIELD, "clckwork"));
query.add(sq1, BooleanClause.Occur.SHOULD);
query.add(sq2, BooleanClause.Occur.SHOULD);
TopScoreDocCollector collector = TopScoreDocCollector.create(1000);
searcher.search(query.build(), collector);
hits = collector.topDocs().scoreDocs.length;
for (ScoreDoc scoreDoc : collector.topDocs().scoreDocs) {
System.out.println(scoreDoc.doc);
}
indexReader.close();
assertEquals("Bug in boolean query composed of span queries", failed, false);
assertEquals("Bug in boolean query composed of span queries", hits, 1);
directory.close();
}
Aggregations