mirror of https://github.com/apache/lucene.git
LUCENE 9204: Move SpanQuery and subclasses to the queries module (#152)
This commit is contained in:
parent
5912e65434
commit
4464cd87cc
|
@ -116,6 +116,9 @@ API Changes
|
|||
|
||||
* LUCENE-9843: Remove compression option on default codec's docvalues. (Jack Conradson)
|
||||
|
||||
* LUCENE-9204: SpanQuery and its subclasses have been moved from core/ into the
|
||||
queries/ module. (Alan Woodward)
|
||||
|
||||
Improvements
|
||||
|
||||
* LUCENE-9960: Avoid unnecessary top element replacement for equal elements in PriorityQueue. (Dawid Weiss)
|
||||
|
|
|
@ -405,4 +405,8 @@ it was decided to remove the ability to estimate their RAM usage.
|
|||
|
||||
LongValueFacetCounts will now automatically detect whether-or-not an indexed field is single- or
|
||||
multi-valued. The user no longer needs to provide this information to the ctors. Migrating should
|
||||
be as simple as no longer providing this boolean.
|
||||
be as simple as no longer providing this boolean.
|
||||
|
||||
## SpanQuery and subclasses have moved from core/ to the queries module
|
||||
|
||||
They can now be found in the o.a.l.queries.spans package.
|
||||
|
|
|
@ -23,14 +23,14 @@ import org.apache.lucene.analysis.Analyzer;
|
|||
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
||||
import org.apache.lucene.benchmark.byTask.tasks.NewAnalyzerTask;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanFirstQuery;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.queryparser.classic.QueryParser;
|
||||
import org.apache.lucene.search.MultiTermQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.WildcardQuery;
|
||||
import org.apache.lucene.search.spans.SpanFirstQuery;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
|
||||
/**
|
||||
* A QueryMaker that uses common and uncommon actual Wikipedia queries for searching the English
|
||||
|
|
|
@ -22,13 +22,13 @@ import java.util.List;
|
|||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.benchmark.byTask.tasks.NewAnalyzerTask;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanFirstQuery;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.queryparser.classic.QueryParser;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.WildcardQuery;
|
||||
import org.apache.lucene.search.spans.SpanFirstQuery;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
|
||||
/**
|
||||
* A QueryMaker that makes queries devised manually (by Grant Ingersoll) for searching in the
|
||||
|
|
|
@ -40,8 +40,8 @@ import org.apache.lucene.index.IndexReader;
|
|||
* <li>{@link MatchAllDocsQuery}
|
||||
* </ul>
|
||||
*
|
||||
* <p>See also the family of {@link org.apache.lucene.search.spans Span Queries} and additional
|
||||
* queries available in the <a href="{@docRoot}/../queries/overview-summary.html">Queries module</a>
|
||||
* <p>See also additional queries available in the <a
|
||||
* href="{@docRoot}/../queries/overview-summary.html">Queries module</a>
|
||||
*/
|
||||
public abstract class Query {
|
||||
|
||||
|
|
|
@ -34,12 +34,11 @@
|
|||
* <h2>Search Basics</h2>
|
||||
*
|
||||
* <p>Lucene offers a wide variety of {@link org.apache.lucene.search.Query} implementations, most
|
||||
* of which are in this package, its subpackage ({@link org.apache.lucene.search.spans spans}), or
|
||||
* the <a href="{@docRoot}/../queries/overview-summary.html">queries module</a>. These
|
||||
* implementations can be combined in a wide variety of ways to provide complex querying
|
||||
* capabilities along with information about where matches took place in the document collection.
|
||||
* The <a href="#query">Query Classes</a> section below highlights some of the more important Query
|
||||
* classes. For details on implementing your own Query class, see <a
|
||||
* of which are in this package or the <a href="{@docRoot}/../queries/overview-summary.html">queries
|
||||
* module</a>. These implementations can be combined in a wide variety of ways to provide complex
|
||||
* querying capabilities along with information about where matches took place in the document
|
||||
* collection. The <a href="#query">Query Classes</a> section below highlights some of the more
|
||||
* important Query classes. For details on implementing your own Query class, see <a
|
||||
* href="#customQueriesExpert">Custom Queries -- Expert Level</a> below.
|
||||
*
|
||||
* <p>To perform a search, applications usually call {@link
|
||||
|
@ -121,7 +120,7 @@
|
|||
*
|
||||
* <h3>Phrases</h3>
|
||||
*
|
||||
* <p>Another common search is to find documents containing certain phrases. This is handled three
|
||||
* <p>Another common search is to find documents containing certain phrases. This is handled in
|
||||
* different ways:
|
||||
*
|
||||
* <ol>
|
||||
|
@ -136,12 +135,8 @@
|
|||
* general form of PhraseQuery that accepts multiple Terms for a position in the phrase. For
|
||||
* example, this can be used to perform phrase queries that also incorporate synonyms.
|
||||
* <li>
|
||||
* <p>{@link org.apache.lucene.search.spans.SpanNearQuery SpanNearQuery} — Matches a
|
||||
* sequence of other {@link org.apache.lucene.search.spans.SpanQuery SpanQuery} instances.
|
||||
* {@link org.apache.lucene.search.spans.SpanNearQuery SpanNearQuery} allows for much more
|
||||
* complicated phrase queries since it is constructed from other {@link
|
||||
* org.apache.lucene.search.spans.SpanQuery SpanQuery} instances, instead of only {@link
|
||||
* org.apache.lucene.search.TermQuery TermQuery} instances.
|
||||
* <p>Interval queries in the <a href="{@docRoot}/../queries/overview-summary.html">Queries
|
||||
* module</a>
|
||||
* </ol>
|
||||
*
|
||||
* <h3>{@link org.apache.lucene.search.PointRangeQuery PointRangeQuery} </h3>
|
||||
|
|
|
@ -27,7 +27,6 @@ import java.util.concurrent.ExecutorService;
|
|||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.analysis.MockAnalyzer;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Field;
|
||||
|
@ -35,15 +34,12 @@ import org.apache.lucene.document.TextField;
|
|||
import org.apache.lucene.index.DirectoryReader;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.IndexWriter;
|
||||
import org.apache.lucene.index.IndexWriterConfig;
|
||||
import org.apache.lucene.index.LeafReaderContext;
|
||||
import org.apache.lucene.index.MultiReader;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.BooleanClause.Occur;
|
||||
import org.apache.lucene.search.similarities.ClassicSimilarity;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
import org.apache.lucene.util.NamedThreadFactory;
|
||||
|
@ -386,41 +382,6 @@ public class TestBooleanQuery extends LuceneTestCase {
|
|||
d.close();
|
||||
}
|
||||
|
||||
// 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, Integer.MAX_VALUE);
|
||||
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();
|
||||
}
|
||||
|
||||
public void testMinShouldMatchLeniency() throws Exception {
|
||||
Directory dir = newDirectory();
|
||||
IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
|
||||
|
|
|
@ -16,13 +16,10 @@
|
|||
*/
|
||||
package org.apache.lucene.search;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.BooleanClause.Occur;
|
||||
import org.apache.lucene.search.similarities.ClassicSimilarity;
|
||||
import org.apache.lucene.search.spans.*;
|
||||
|
||||
/**
|
||||
* TestExplanations subclass that builds up super crazy complex queries on the assumption that if
|
||||
|
@ -43,126 +40,6 @@ public class TestComplexExplanations extends BaseExplanationTestCase {
|
|||
super.tearDown();
|
||||
}
|
||||
|
||||
public void test1() throws Exception {
|
||||
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
|
||||
PhraseQuery phraseQuery = new PhraseQuery(1, FIELD, "w1", "w2");
|
||||
q.add(phraseQuery, Occur.MUST);
|
||||
q.add(snear(st("w2"), sor("w5", "zz"), 4, true), Occur.SHOULD);
|
||||
q.add(snear(sf("w3", 2), st("w2"), st("w3"), 5, true), Occur.SHOULD);
|
||||
|
||||
Query t =
|
||||
new BooleanQuery.Builder()
|
||||
.add(new TermQuery(new Term(FIELD, "xx")), Occur.MUST)
|
||||
.add(matchTheseItems(new int[] {1, 3}), Occur.FILTER)
|
||||
.build();
|
||||
q.add(new BoostQuery(t, 1000), Occur.SHOULD);
|
||||
|
||||
t = new ConstantScoreQuery(matchTheseItems(new int[] {0, 2}));
|
||||
q.add(new BoostQuery(t, 30), Occur.SHOULD);
|
||||
|
||||
List<Query> disjuncts = new ArrayList<>();
|
||||
disjuncts.add(snear(st("w2"), sor("w5", "zz"), 4, true));
|
||||
disjuncts.add(new TermQuery(new Term(FIELD, "QQ")));
|
||||
|
||||
BooleanQuery.Builder xxYYZZ = new BooleanQuery.Builder();
|
||||
;
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "xx")), Occur.SHOULD);
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "yy")), Occur.SHOULD);
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "zz")), Occur.MUST_NOT);
|
||||
|
||||
disjuncts.add(xxYYZZ.build());
|
||||
|
||||
BooleanQuery.Builder xxW1 = new BooleanQuery.Builder();
|
||||
;
|
||||
xxW1.add(new TermQuery(new Term(FIELD, "xx")), Occur.MUST_NOT);
|
||||
xxW1.add(new TermQuery(new Term(FIELD, "w1")), Occur.MUST_NOT);
|
||||
|
||||
disjuncts.add(xxW1.build());
|
||||
|
||||
List<Query> disjuncts2 = new ArrayList<>();
|
||||
disjuncts2.add(new TermQuery(new Term(FIELD, "w1")));
|
||||
disjuncts2.add(new TermQuery(new Term(FIELD, "w2")));
|
||||
disjuncts2.add(new TermQuery(new Term(FIELD, "w3")));
|
||||
disjuncts.add(new DisjunctionMaxQuery(disjuncts2, 0.5f));
|
||||
|
||||
q.add(new DisjunctionMaxQuery(disjuncts, 0.2f), Occur.SHOULD);
|
||||
|
||||
BooleanQuery.Builder b = new BooleanQuery.Builder();
|
||||
;
|
||||
b.setMinimumNumberShouldMatch(2);
|
||||
b.add(snear("w1", "w2", 1, true), Occur.SHOULD);
|
||||
b.add(snear("w2", "w3", 1, true), Occur.SHOULD);
|
||||
b.add(snear("w1", "w3", 3, true), Occur.SHOULD);
|
||||
|
||||
q.add(b.build(), Occur.SHOULD);
|
||||
|
||||
qtest(q.build(), new int[] {0, 1, 2});
|
||||
}
|
||||
|
||||
public void test2() throws Exception {
|
||||
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
|
||||
PhraseQuery phraseQuery = new PhraseQuery(1, FIELD, "w1", "w2");
|
||||
q.add(phraseQuery, Occur.MUST);
|
||||
q.add(snear(st("w2"), sor("w5", "zz"), 4, true), Occur.SHOULD);
|
||||
q.add(snear(sf("w3", 2), st("w2"), st("w3"), 5, true), Occur.SHOULD);
|
||||
|
||||
Query t =
|
||||
new BooleanQuery.Builder()
|
||||
.add(new TermQuery(new Term(FIELD, "xx")), Occur.MUST)
|
||||
.add(matchTheseItems(new int[] {1, 3}), Occur.FILTER)
|
||||
.build();
|
||||
q.add(new BoostQuery(t, 1000), Occur.SHOULD);
|
||||
|
||||
t = new ConstantScoreQuery(matchTheseItems(new int[] {0, 2}));
|
||||
q.add(new BoostQuery(t, 20), Occur.SHOULD);
|
||||
|
||||
List<Query> disjuncts = new ArrayList<>();
|
||||
disjuncts.add(snear(st("w2"), sor("w5", "zz"), 4, true));
|
||||
disjuncts.add(new TermQuery(new Term(FIELD, "QQ")));
|
||||
|
||||
BooleanQuery.Builder xxYYZZ = new BooleanQuery.Builder();
|
||||
;
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "xx")), Occur.SHOULD);
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "yy")), Occur.SHOULD);
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "zz")), Occur.MUST_NOT);
|
||||
|
||||
disjuncts.add(xxYYZZ.build());
|
||||
|
||||
BooleanQuery.Builder xxW1 = new BooleanQuery.Builder();
|
||||
;
|
||||
xxW1.add(new TermQuery(new Term(FIELD, "xx")), Occur.MUST_NOT);
|
||||
xxW1.add(new TermQuery(new Term(FIELD, "w1")), Occur.MUST_NOT);
|
||||
|
||||
disjuncts.add(xxW1.build());
|
||||
|
||||
DisjunctionMaxQuery dm2 =
|
||||
new DisjunctionMaxQuery(
|
||||
Arrays.asList(
|
||||
new TermQuery(new Term(FIELD, "w1")),
|
||||
new TermQuery(new Term(FIELD, "w2")),
|
||||
new TermQuery(new Term(FIELD, "w3"))),
|
||||
0.5f);
|
||||
disjuncts.add(dm2);
|
||||
|
||||
q.add(new DisjunctionMaxQuery(disjuncts, 0.2f), Occur.SHOULD);
|
||||
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
||||
;
|
||||
builder.setMinimumNumberShouldMatch(2);
|
||||
builder.add(snear("w1", "w2", 1, true), Occur.SHOULD);
|
||||
builder.add(snear("w2", "w3", 1, true), Occur.SHOULD);
|
||||
builder.add(snear("w1", "w3", 3, true), Occur.SHOULD);
|
||||
BooleanQuery b = builder.build();
|
||||
|
||||
q.add(new BoostQuery(b, 0), Occur.SHOULD);
|
||||
|
||||
qtest(q.build(), new int[] {0, 1, 2});
|
||||
}
|
||||
|
||||
// :TODO: we really need more crazy complex cases.
|
||||
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
|
@ -273,50 +150,4 @@ public class TestComplexExplanations extends BaseExplanationTestCase {
|
|||
|
||||
bqtest(new BoostQuery(query, 0), new int[] {0, 1, 2, 3});
|
||||
}
|
||||
|
||||
public void testST3() throws Exception {
|
||||
SpanQuery q = st("w1");
|
||||
bqtest(new SpanBoostQuery(q, 0), new int[] {0, 1, 2, 3});
|
||||
}
|
||||
|
||||
public void testST6() throws Exception {
|
||||
SpanQuery q = st("xx");
|
||||
qtest(new SpanBoostQuery(q, 0), new int[] {2, 3});
|
||||
}
|
||||
|
||||
public void testSF3() throws Exception {
|
||||
SpanQuery q = sf(("w1"), 1);
|
||||
bqtest(new SpanBoostQuery(q, 0), new int[] {0, 1, 2, 3});
|
||||
}
|
||||
|
||||
public void testSF7() throws Exception {
|
||||
SpanQuery q = sf(("xx"), 3);
|
||||
bqtest(new SpanBoostQuery(q, 0), new int[] {2, 3});
|
||||
}
|
||||
|
||||
public void testSNot3() throws Exception {
|
||||
SpanQuery q = snot(sf("w1", 10), st("QQ"));
|
||||
bqtest(new SpanBoostQuery(q, 0), new int[] {0, 1, 2, 3});
|
||||
}
|
||||
|
||||
public void testSNot6() throws Exception {
|
||||
SpanQuery q = snot(sf("w1", 10), st("xx"));
|
||||
bqtest(new SpanBoostQuery(q, 0), new int[] {0, 1, 2, 3});
|
||||
}
|
||||
|
||||
public void testSNot8() throws Exception {
|
||||
// NOTE: using qtest not bqtest
|
||||
SpanQuery f = snear("w1", "w3", 10, true);
|
||||
f = new SpanBoostQuery(f, 0);
|
||||
SpanQuery q = snot(f, st("xx"));
|
||||
qtest(q, new int[] {0, 1, 3});
|
||||
}
|
||||
|
||||
public void testSNot9() throws Exception {
|
||||
// NOTE: using qtest not bqtest
|
||||
SpanQuery t = st("xx");
|
||||
t = new SpanBoostQuery(t, 0);
|
||||
SpanQuery q = snot(snear("w1", "w3", 10, true), t);
|
||||
qtest(q, new int[] {0, 1, 3});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,6 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.analysis.MockAnalyzer;
|
||||
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
||||
import org.apache.lucene.document.Document;
|
||||
|
@ -41,7 +40,6 @@ import org.apache.lucene.index.RandomIndexWriter;
|
|||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.similarities.ClassicSimilarity;
|
||||
import org.apache.lucene.search.similarities.Similarity;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
|
@ -457,40 +455,6 @@ public class TestDisjunctionMaxQuery extends LuceneTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
// LUCENE-4477 / LUCENE-4401:
|
||||
public void testBooleanSpanQuery() throws Exception {
|
||||
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);
|
||||
|
||||
DisjunctionMaxQuery query =
|
||||
new DisjunctionMaxQuery(
|
||||
Arrays.asList(
|
||||
new SpanTermQuery(new Term(FIELD, "clockwork")),
|
||||
new SpanTermQuery(new Term(FIELD, "clckwork"))),
|
||||
1.0f);
|
||||
TopScoreDocCollector collector = TopScoreDocCollector.create(1000, Integer.MAX_VALUE);
|
||||
searcher.search(query, collector);
|
||||
hits = collector.topDocs().scoreDocs.length;
|
||||
for (ScoreDoc scoreDoc : collector.topDocs().scoreDocs) {
|
||||
System.out.println(scoreDoc.doc);
|
||||
}
|
||||
indexReader.close();
|
||||
assertEquals(hits, 1);
|
||||
directory.close();
|
||||
}
|
||||
|
||||
public void testRewriteBoolean() throws Exception {
|
||||
Query sub1 = tq("hed", "albino");
|
||||
Query sub2 = tq("hed", "elephant");
|
||||
|
|
|
@ -33,10 +33,6 @@ import org.apache.lucene.index.ReaderUtil;
|
|||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.index.Terms;
|
||||
import org.apache.lucene.index.TermsEnum;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanOrQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
|
||||
public class TestMatchesIterator extends MatchesTestBase {
|
||||
|
@ -443,37 +439,6 @@ public class TestMatchesIterator extends MatchesTestBase {
|
|||
// 0 1 2 3 4 5 6 7
|
||||
// "a phrase sentence with many phrase sentence iterations of a phrase sentence",
|
||||
|
||||
public void testSpanQuery() throws IOException {
|
||||
SpanQuery subq =
|
||||
SpanNearQuery.newOrderedNearQuery(FIELD_WITH_OFFSETS)
|
||||
.addClause(new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "with")))
|
||||
.addClause(new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "many")))
|
||||
.build();
|
||||
Query q =
|
||||
SpanNearQuery.newOrderedNearQuery(FIELD_WITH_OFFSETS)
|
||||
.addClause(new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "sentence")))
|
||||
.addClause(
|
||||
new SpanOrQuery(
|
||||
subq, new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "iterations"))))
|
||||
.build();
|
||||
checkMatches(
|
||||
q, FIELD_WITH_OFFSETS, new int[][] {{0}, {1}, {2}, {3}, {4, 2, 4, 9, 27, 6, 7, 35, 54}});
|
||||
checkLabelCount(q, FIELD_WITH_OFFSETS, new int[] {0, 0, 0, 0, 1});
|
||||
checkTermMatches(
|
||||
q,
|
||||
FIELD_WITH_OFFSETS,
|
||||
new TermMatch[][][] {
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
{
|
||||
{new TermMatch(2, 9, 17), new TermMatch(3, 18, 22), new TermMatch(4, 23, 27)},
|
||||
{new TermMatch(6, 35, 43), new TermMatch(7, 44, 54)}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void testPointQuery() throws IOException {
|
||||
IndexOrDocValuesQuery pointQuery =
|
||||
new IndexOrDocValuesQuery(
|
||||
|
|
|
@ -17,30 +17,18 @@
|
|||
package org.apache.lucene.search;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.analysis.MockPayloadAnalyzer;
|
||||
import org.apache.lucene.analysis.Tokenizer;
|
||||
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
|
||||
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
|
||||
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.document.TextField;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.LeafReader;
|
||||
import org.apache.lucene.index.MultiTerms;
|
||||
import org.apache.lucene.index.PostingsEnum;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.spans.SpanCollector;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.spans.SpanWeight;
|
||||
import org.apache.lucene.search.spans.Spans;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
@ -189,106 +177,4 @@ public class TestPositionIncrement extends LuceneTestCase {
|
|||
reader.close();
|
||||
store.close();
|
||||
}
|
||||
|
||||
static class PayloadSpanCollector implements SpanCollector {
|
||||
|
||||
List<BytesRef> payloads = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void collectLeaf(PostingsEnum postings, int position, Term term) throws IOException {
|
||||
if (postings.getPayload() != null) payloads.add(BytesRef.deepCopyOf(postings.getPayload()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
payloads.clear();
|
||||
}
|
||||
}
|
||||
|
||||
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, ScoreMode.COMPLETE_NO_SCORES, 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, ScoreMode.COMPLETE_NO_SCORES, 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,9 +32,6 @@ import org.apache.lucene.index.Term;
|
|||
import org.apache.lucene.search.BooleanClause.Occur;
|
||||
import org.apache.lucene.search.similarities.BM25Similarity;
|
||||
import org.apache.lucene.search.similarities.ClassicSimilarity;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.ArrayUtil;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
@ -185,18 +182,6 @@ public class TestQueryRescorer extends LuceneTestCase {
|
|||
assertEquals("1", searcher.doc(hits2.scoreDocs[0].doc).get("id"));
|
||||
assertEquals("0", searcher.doc(hits2.scoreDocs[1].doc).get("id"));
|
||||
|
||||
// Resort using SpanNearQuery:
|
||||
SpanTermQuery t1 = new SpanTermQuery(new Term("field", "wizard"));
|
||||
SpanTermQuery t2 = new SpanTermQuery(new Term("field", "oz"));
|
||||
SpanNearQuery snq = new SpanNearQuery(new SpanQuery[] {t1, t2}, 0, true);
|
||||
|
||||
TopDocs hits3 = QueryRescorer.rescore(searcher, hits, snq, 2.0, 10);
|
||||
|
||||
// Resorting changed the order:
|
||||
assertEquals(2, hits3.totalHits.value);
|
||||
assertEquals("1", searcher.doc(hits3.scoreDocs[0].doc).get("id"));
|
||||
assertEquals("0", searcher.doc(hits3.scoreDocs[1].doc).get("id"));
|
||||
|
||||
r.close();
|
||||
dir.close();
|
||||
}
|
||||
|
@ -404,18 +389,6 @@ public class TestQueryRescorer extends LuceneTestCase {
|
|||
assertEquals("1", searcher.doc(hits2.scoreDocs[0].doc).get("id"));
|
||||
assertEquals("0", searcher.doc(hits2.scoreDocs[1].doc).get("id"));
|
||||
|
||||
// Resort using SpanNearQuery:
|
||||
SpanTermQuery t1 = new SpanTermQuery(new Term("field", "wizard"));
|
||||
SpanTermQuery t2 = new SpanTermQuery(new Term("field", "oz"));
|
||||
SpanNearQuery snq = new SpanNearQuery(new SpanQuery[] {t1, t2}, 0, true);
|
||||
|
||||
TopDocs hits3 = QueryRescorer.rescore(searcher, hits, snq, 2.0, 10);
|
||||
|
||||
// Resorting changed the order:
|
||||
assertEquals(2, hits3.totalHits.value);
|
||||
assertEquals("1", searcher.doc(hits3.scoreDocs[0].doc).get("id"));
|
||||
assertEquals("0", searcher.doc(hits3.scoreDocs[1].doc).get("id"));
|
||||
|
||||
r.close();
|
||||
dir.close();
|
||||
}
|
||||
|
|
|
@ -30,9 +30,6 @@ import java.util.Map;
|
|||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
public class TestQueryVisitor extends LuceneTestCase {
|
||||
|
@ -56,15 +53,6 @@ public class TestQueryVisitor extends LuceneTestCase {
|
|||
.build(),
|
||||
3),
|
||||
BooleanClause.Occur.MUST)
|
||||
.add(
|
||||
new SpanNearQuery(
|
||||
new SpanQuery[] {
|
||||
new SpanTermQuery(new Term("field1", "term6")),
|
||||
new SpanTermQuery(new Term("field1", "term7"))
|
||||
},
|
||||
2,
|
||||
true),
|
||||
BooleanClause.Occur.MUST)
|
||||
.add(new TermQuery(new Term("field1", "term8")), BooleanClause.Occur.MUST_NOT)
|
||||
.add(new PrefixQuery(new Term("field1", "term9")), BooleanClause.Occur.SHOULD)
|
||||
.add(
|
||||
|
@ -85,8 +73,7 @@ public class TestQueryVisitor extends LuceneTestCase {
|
|||
Arrays.asList(
|
||||
new Term("field1", "t1"), new Term("field1", "tm2"),
|
||||
new Term("field1", "tm3"), new Term("field1", "term4"),
|
||||
new Term("field1", "term5"), new Term("field1", "term6"),
|
||||
new Term("field1", "term7"), new Term("field2", "term10")));
|
||||
new Term("field1", "term5"), new Term("field2", "term10")));
|
||||
query.visit(QueryVisitor.termCollector(terms));
|
||||
assertThat(terms, equalTo(expected));
|
||||
}
|
||||
|
@ -113,8 +100,6 @@ public class TestQueryVisitor extends LuceneTestCase {
|
|||
new Term("field1", "tm3"),
|
||||
new Term("field1", "term4"),
|
||||
new Term("field1", "term5"),
|
||||
new Term("field1", "term6"),
|
||||
new Term("field1", "term7"),
|
||||
new Term("field1", "term8"),
|
||||
new Term("field2", "term10")));
|
||||
query.visit(visitor);
|
||||
|
@ -174,8 +159,6 @@ public class TestQueryVisitor extends LuceneTestCase {
|
|||
expected.put(new Term("field1", "tm3"), 2f);
|
||||
expected.put(new Term("field1", "term4"), 3f);
|
||||
expected.put(new Term("field1", "term5"), 3f);
|
||||
expected.put(new Term("field1", "term6"), 1f);
|
||||
expected.put(new Term("field1", "term7"), 1f);
|
||||
expected.put(new Term("field2", "term10"), 6f);
|
||||
assertThat(termsToBoosts, equalTo(expected));
|
||||
}
|
||||
|
|
|
@ -33,9 +33,6 @@ import org.apache.lucene.search.Explanation;
|
|||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.search.TopDocs;
|
||||
import org.apache.lucene.search.spans.SpanOrQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
import org.apache.lucene.util.TestUtil;
|
||||
|
@ -268,33 +265,4 @@ public class TestSimilarity2 extends LuceneTestCase {
|
|||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
/** make sure all sims work with spanOR(termX, termY) where termY does not exist */
|
||||
public void testCrazySpans() throws Exception {
|
||||
// historically this was a problem, but sim's no longer have to score terms that dont exist
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
FieldType ft = new FieldType(TextField.TYPE_NOT_STORED);
|
||||
doc.add(newField("foo", "bar", ft));
|
||||
iw.addDocument(doc);
|
||||
IndexReader ir = iw.getReader();
|
||||
iw.close();
|
||||
IndexSearcher is = newSearcher(ir);
|
||||
|
||||
for (Similarity sim : sims) {
|
||||
is.setSimilarity(sim);
|
||||
SpanTermQuery s1 = new SpanTermQuery(new Term("foo", "bar"));
|
||||
SpanTermQuery s2 = new SpanTermQuery(new Term("foo", "baz"));
|
||||
Query query = new SpanOrQuery(s1, s2);
|
||||
TopDocs td = is.search(query, 10);
|
||||
assertEquals(1, td.totalHits.value);
|
||||
float score = td.scoreDocs[0].score;
|
||||
assertFalse("negative score for " + sim, score < 0.0f);
|
||||
assertFalse("inf score for " + sim, Float.isInfinite(score));
|
||||
assertFalse("nan score for " + sim, Float.isNaN(score));
|
||||
}
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,8 +27,8 @@ import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
|
|||
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.memory.MemoryIndex;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
|
||||
/**
|
||||
* {@link Scorer} implementation which scores text fragments by the number of unique query terms
|
||||
|
|
|
@ -21,7 +21,7 @@ import org.apache.lucene.analysis.TokenStream;
|
|||
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
|
||||
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
|
||||
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
|
||||
import org.apache.lucene.search.spans.Spans;
|
||||
import org.apache.lucene.queries.spans.Spans;
|
||||
|
||||
/**
|
||||
* {@link Fragmenter} implementation which breaks text up into same-size fragments but does not
|
||||
|
|
|
@ -39,6 +39,15 @@ import org.apache.lucene.index.Terms;
|
|||
import org.apache.lucene.index.memory.MemoryIndex;
|
||||
import org.apache.lucene.queries.CommonTermsQuery;
|
||||
import org.apache.lucene.queries.function.FunctionScoreQuery;
|
||||
import org.apache.lucene.queries.spans.FieldMaskingSpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanFirstQuery;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanNotQuery;
|
||||
import org.apache.lucene.queries.spans.SpanOrQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.queries.spans.SpanWeight;
|
||||
import org.apache.lucene.queries.spans.Spans;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.BoostQuery;
|
||||
|
@ -54,15 +63,6 @@ import org.apache.lucene.search.QueryVisitor;
|
|||
import org.apache.lucene.search.ScoreMode;
|
||||
import org.apache.lucene.search.SynonymQuery;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.search.spans.FieldMaskingSpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanFirstQuery;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanNotQuery;
|
||||
import org.apache.lucene.search.spans.SpanOrQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.spans.SpanWeight;
|
||||
import org.apache.lucene.search.spans.Spans;
|
||||
import org.apache.lucene.util.Bits;
|
||||
import org.apache.lucene.util.IOUtils;
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ import org.apache.lucene.analysis.TokenStream;
|
|||
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
|
||||
import org.apache.lucene.index.LeafReader;
|
||||
import org.apache.lucene.index.memory.MemoryIndex;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
|
||||
/**
|
||||
* Uses an {@link Analyzer} on content to get offsets and then populates a {@link MemoryIndex}.
|
||||
|
|
|
@ -20,12 +20,12 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.search.AutomatonQuery;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.FuzzyQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.QueryVisitor;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.util.automaton.ByteRunAutomaton;
|
||||
|
||||
/**
|
||||
|
|
|
@ -34,6 +34,11 @@ import org.apache.lucene.index.PostingsEnum;
|
|||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.index.Terms;
|
||||
import org.apache.lucene.index.TermsEnum;
|
||||
import org.apache.lucene.queries.spans.SpanCollector;
|
||||
import org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanScorer;
|
||||
import org.apache.lucene.queries.spans.Spans;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.MatchAllDocsQuery;
|
||||
import org.apache.lucene.search.MultiTermQuery;
|
||||
|
@ -45,11 +50,6 @@ import org.apache.lucene.search.TwoPhaseIterator;
|
|||
import org.apache.lucene.search.Weight;
|
||||
import org.apache.lucene.search.highlight.WeightedSpanTerm;
|
||||
import org.apache.lucene.search.highlight.WeightedSpanTermExtractor;
|
||||
import org.apache.lucene.search.spans.SpanCollector;
|
||||
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanScorer;
|
||||
import org.apache.lucene.search.spans.Spans;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.PriorityQueue;
|
||||
|
||||
|
|
|
@ -48,6 +48,7 @@ import org.apache.lucene.index.MultiReader;
|
|||
import org.apache.lucene.index.ReaderUtil;
|
||||
import org.apache.lucene.index.StoredFieldVisitor;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.search.DocIdSetIterator;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.MatchAllDocsQuery;
|
||||
|
@ -58,7 +59,6 @@ import org.apache.lucene.search.QueryVisitor;
|
|||
import org.apache.lucene.search.ScoreDoc;
|
||||
import org.apache.lucene.search.TopDocs;
|
||||
import org.apache.lucene.search.Weight;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.IOUtils;
|
||||
import org.apache.lucene.util.InPlaceMergeSorter;
|
||||
|
|
|
@ -59,6 +59,12 @@ import org.apache.lucene.index.Term;
|
|||
import org.apache.lucene.queries.CommonTermsQuery;
|
||||
import org.apache.lucene.queries.function.FunctionScoreQuery;
|
||||
import org.apache.lucene.queries.payloads.SpanPayloadCheckQuery;
|
||||
import org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanNotQuery;
|
||||
import org.apache.lucene.queries.spans.SpanOrQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.BooleanClause.Occur;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.ConstantScoreQuery;
|
||||
|
@ -81,12 +87,6 @@ import org.apache.lucene.search.TermRangeQuery;
|
|||
import org.apache.lucene.search.TopDocs;
|
||||
import org.apache.lucene.search.WildcardQuery;
|
||||
import org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner;
|
||||
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanNotQuery;
|
||||
import org.apache.lucene.search.spans.SpanOrQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
|
|
@ -35,6 +35,9 @@ import org.apache.lucene.index.IndexReader;
|
|||
import org.apache.lucene.index.IndexWriter;
|
||||
import org.apache.lucene.index.LeafReaderContext;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.PhraseQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
|
@ -42,9 +45,6 @@ import org.apache.lucene.search.Scorable;
|
|||
import org.apache.lucene.search.ScoreMode;
|
||||
import org.apache.lucene.search.SimpleCollector;
|
||||
import org.apache.lucene.search.TopDocs;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.FixedBitSet;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
|
|
@ -21,14 +21,14 @@ import org.apache.lucene.analysis.Analyzer;
|
|||
import org.apache.lucene.analysis.MockAnalyzer;
|
||||
import org.apache.lucene.analysis.MockTokenizer;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.BooleanClause.Occur;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.PhraseQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
public class TestMisses extends LuceneTestCase {
|
||||
|
|
|
@ -39,13 +39,13 @@ import org.apache.lucene.index.IndexReader;
|
|||
import org.apache.lucene.index.IndexWriter;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.DisjunctionMaxQuery;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.TopDocs;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.TestUtil;
|
||||
|
|
|
@ -50,6 +50,8 @@ import org.apache.lucene.index.IndexableField;
|
|||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.intervals.IntervalQuery;
|
||||
import org.apache.lucene.queries.intervals.Intervals;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.queryparser.flexible.core.QueryNodeException;
|
||||
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser;
|
||||
import org.apache.lucene.queryparser.flexible.standard.config.StandardQueryConfigHandler;
|
||||
|
@ -61,8 +63,6 @@ import org.apache.lucene.search.PhraseQuery;
|
|||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.search.TopDocs;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
|
|
|
@ -36,6 +36,15 @@ import org.apache.lucene.document.FieldType;
|
|||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanBoostQuery;
|
||||
import org.apache.lucene.queries.spans.SpanFirstQuery;
|
||||
import org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanNotQuery;
|
||||
import org.apache.lucene.queries.spans.SpanOrQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.queries.spans.SpanWeight;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.BoostQuery;
|
||||
|
@ -55,15 +64,6 @@ import org.apache.lucene.search.TermQuery;
|
|||
import org.apache.lucene.search.TermRangeQuery;
|
||||
import org.apache.lucene.search.TopDocs;
|
||||
import org.apache.lucene.search.WildcardQuery;
|
||||
import org.apache.lucene.search.spans.SpanBoostQuery;
|
||||
import org.apache.lucene.search.spans.SpanFirstQuery;
|
||||
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanNotQuery;
|
||||
import org.apache.lucene.search.spans.SpanOrQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.spans.SpanWeight;
|
||||
import org.apache.lucene.search.uhighlight.UnifiedHighlighter.HighlightFlag;
|
||||
import org.apache.lucene.store.BaseDirectoryWrapper;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
|
|
|
@ -32,6 +32,11 @@ import org.apache.lucene.document.FieldType;
|
|||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanOrQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.BoostQuery;
|
||||
|
@ -50,11 +55,6 @@ import org.apache.lucene.search.TopDocs;
|
|||
import org.apache.lucene.search.TotalHits;
|
||||
import org.apache.lucene.search.Weight;
|
||||
import org.apache.lucene.search.WildcardQuery;
|
||||
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanOrQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.uhighlight.UnifiedHighlighter.HighlightFlag;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.IOUtils;
|
||||
|
|
|
@ -28,11 +28,11 @@ import org.apache.lucene.analysis.Analyzer;
|
|||
import org.apache.lucene.analysis.MockAnalyzer;
|
||||
import org.apache.lucene.index.LeafReader;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.search.DocIdSetIterator;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.MatchAllDocsQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.uhighlight.FieldHighlighter;
|
||||
import org.apache.lucene.search.uhighlight.FieldOffsetStrategy;
|
||||
import org.apache.lucene.search.uhighlight.LabelledCharArrayMatcher;
|
||||
|
|
|
@ -54,6 +54,9 @@ import org.apache.lucene.document.SortedSetDocValuesField;
|
|||
import org.apache.lucene.document.StringField;
|
||||
import org.apache.lucene.document.TextField;
|
||||
import org.apache.lucene.index.*;
|
||||
import org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.queries.spans.SpanOrQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queryparser.classic.QueryParser;
|
||||
import org.apache.lucene.search.DocIdSetIterator;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
|
@ -62,9 +65,6 @@ import org.apache.lucene.search.Query;
|
|||
import org.apache.lucene.search.RegexpQuery;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.search.TopDocs;
|
||||
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.search.spans.SpanOrQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.store.ByteBuffersDirectory;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
|
|
|
@ -26,6 +26,10 @@ import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
|||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.queryparser.complexPhrase.ComplexPhraseQueryParser;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
|
@ -35,10 +39,6 @@ import org.apache.lucene.search.Query;
|
|||
import org.apache.lucene.search.RegexpQuery;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.search.WildcardQuery;
|
||||
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
|
||||
public class TestHighlightingMatcher extends MonitorTestBase {
|
||||
|
||||
|
|
|
@ -22,18 +22,18 @@ import java.util.Collections;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.FieldMaskingSpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanBoostQuery;
|
||||
import org.apache.lucene.queries.spans.SpanContainingQuery;
|
||||
import org.apache.lucene.queries.spans.SpanFirstQuery;
|
||||
import org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanOrQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.queries.spans.SpanWithinQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.RegexpQuery;
|
||||
import org.apache.lucene.search.spans.FieldMaskingSpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanBoostQuery;
|
||||
import org.apache.lucene.search.spans.SpanContainingQuery;
|
||||
import org.apache.lucene.search.spans.SpanFirstQuery;
|
||||
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanOrQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.spans.SpanWithinQuery;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
public class TestSpanExtractors extends LuceneTestCase {
|
||||
|
|
|
@ -16,8 +16,8 @@
|
|||
*/
|
||||
package org.apache.lucene.queries.payloads;
|
||||
|
||||
import org.apache.lucene.queries.spans.Spans;
|
||||
import org.apache.lucene.search.Explanation;
|
||||
import org.apache.lucene.search.spans.Spans;
|
||||
|
||||
/**
|
||||
* An abstract class that defines a way for PayloadScoreQuery instances to transform the cumulative
|
||||
|
|
|
@ -24,6 +24,12 @@ import org.apache.lucene.index.LeafReaderContext;
|
|||
import org.apache.lucene.index.PostingsEnum;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.index.TermStates;
|
||||
import org.apache.lucene.queries.spans.FilterSpans;
|
||||
import org.apache.lucene.queries.spans.SpanCollector;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanScorer;
|
||||
import org.apache.lucene.queries.spans.SpanWeight;
|
||||
import org.apache.lucene.queries.spans.Spans;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.Explanation;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
|
@ -31,12 +37,6 @@ import org.apache.lucene.search.LeafSimScorer;
|
|||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.QueryVisitor;
|
||||
import org.apache.lucene.search.ScoreMode;
|
||||
import org.apache.lucene.search.spans.FilterSpans;
|
||||
import org.apache.lucene.search.spans.SpanCollector;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanScorer;
|
||||
import org.apache.lucene.search.spans.SpanWeight;
|
||||
import org.apache.lucene.search.spans.Spans;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
|
||||
/** A Query class that uses a {@link PayloadFunction} to modify the score of a wrapped SpanQuery */
|
||||
|
|
|
@ -26,19 +26,19 @@ import org.apache.lucene.index.PostingsEnum;
|
|||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.index.TermStates;
|
||||
import org.apache.lucene.index.Terms;
|
||||
import org.apache.lucene.queries.spans.FilterSpans;
|
||||
import org.apache.lucene.queries.spans.FilterSpans.AcceptStatus;
|
||||
import org.apache.lucene.queries.spans.SpanCollector;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanScorer;
|
||||
import org.apache.lucene.queries.spans.SpanWeight;
|
||||
import org.apache.lucene.queries.spans.Spans;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.LeafSimScorer;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.QueryVisitor;
|
||||
import org.apache.lucene.search.ScoreMode;
|
||||
import org.apache.lucene.search.spans.FilterSpans;
|
||||
import org.apache.lucene.search.spans.FilterSpans.AcceptStatus;
|
||||
import org.apache.lucene.search.spans.SpanCollector;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanScorer;
|
||||
import org.apache.lucene.search.spans.SpanWeight;
|
||||
import org.apache.lucene.search.spans.Spans;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
|
||||
/** Only return those matches that have a specific payload at the given position. */
|
||||
|
@ -73,7 +73,7 @@ public class SpanPayloadCheckQuery extends SpanQuery {
|
|||
};
|
||||
|
||||
/**
|
||||
* @param match The underlying {@link org.apache.lucene.search.spans.SpanQuery} to check
|
||||
* @param match The underlying {@link SpanQuery} to check
|
||||
* @param payloadToMatch The {@link java.util.List} of payloads to match
|
||||
*/
|
||||
public SpanPayloadCheckQuery(SpanQuery match, List<BytesRef> payloadToMatch) {
|
||||
|
@ -81,7 +81,7 @@ public class SpanPayloadCheckQuery extends SpanQuery {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param match The underlying {@link org.apache.lucene.search.spans.SpanQuery} to check
|
||||
* @param match The underlying {@link SpanQuery} to check
|
||||
* @param payloadToMatch The {@link java.util.List} of payloads to match
|
||||
* @param operation The equality check, lt, lte, gt, gte, or eq. Defaults to eq for equals)
|
||||
* @param payloadType specify if the format of the bytes in the payload (String, Integer, or
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
|
@ -14,15 +14,14 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.index.PostingsEnum;
|
||||
import org.apache.lucene.index.Term;
|
||||
|
||||
/**
|
||||
* An interface defining the collection of postings information from the leaves of a {@link
|
||||
* org.apache.lucene.search.spans.Spans}
|
||||
* An interface defining the collection of postings information from the leaves of a {@link Spans}
|
||||
*
|
||||
* @lucene.experimental
|
||||
*/
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import org.apache.lucene.search.DocIdSetIterator;
|
||||
import org.apache.lucene.search.TwoPhaseIterator;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.search.DocIdSetIterator;
|
|
@ -14,9 +14,9 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import org.apache.lucene.search.spans.FilterSpans.AcceptStatus;
|
||||
import org.apache.lucene.queries.spans.FilterSpans.AcceptStatus;
|
||||
|
||||
/**
|
||||
* Matches spans near the beginning of a field.
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
@ -23,12 +23,12 @@ import org.apache.lucene.index.IndexReader;
|
|||
import org.apache.lucene.index.LeafReaderContext;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.index.TermStates;
|
||||
import org.apache.lucene.queries.spans.FilterSpans.AcceptStatus;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.QueryVisitor;
|
||||
import org.apache.lucene.search.ScoreMode;
|
||||
import org.apache.lucene.search.spans.FilterSpans.AcceptStatus;
|
||||
|
||||
/** Base class for filtering a SpanQuery based on the position of a match. */
|
||||
public abstract class SpanPositionCheckQuery extends SpanQuery implements Cloneable {
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import org.apache.lucene.util.PriorityQueue;
|
||||
|
|
@ -14,10 +14,10 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.search.spans.FilterSpans.AcceptStatus;
|
||||
import org.apache.lucene.queries.spans.FilterSpans.AcceptStatus;
|
||||
|
||||
/**
|
||||
* Checks to see if the {@link #getMatch()} lies between a start and end position
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.search.DocIdSetIterator;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
|
@ -19,34 +19,34 @@
|
|||
* The calculus of spans.
|
||||
*
|
||||
* <p>A span is a <code><doc,startPosition,endPosition></code> tuple that is enumerated by
|
||||
* class {@link org.apache.lucene.search.spans.Spans Spans}.
|
||||
* class {@link org.apache.lucene.queries.spans.Spans Spans}.
|
||||
*
|
||||
* <p>The following span query operators are implemented:
|
||||
*
|
||||
* <ul>
|
||||
* <li>A {@link org.apache.lucene.search.spans.SpanTermQuery SpanTermQuery} matches all spans
|
||||
* <li>A {@link org.apache.lucene.queries.spans.SpanTermQuery SpanTermQuery} matches all spans
|
||||
* containing a particular {@link org.apache.lucene.index.Term Term}. This should not be used
|
||||
* for terms that are indexed at position Integer.MAX_VALUE.
|
||||
* <li>A {@link org.apache.lucene.search.spans.SpanNearQuery SpanNearQuery} matches spans which
|
||||
* <li>A {@link org.apache.lucene.queries.spans.SpanNearQuery SpanNearQuery} matches spans which
|
||||
* occur near one another, and can be used to implement things like phrase search (when
|
||||
* constructed from {@link org.apache.lucene.search.spans.SpanTermQuery}s) and inter-phrase
|
||||
* constructed from {@link org.apache.lucene.queries.spans.SpanTermQuery}s) and inter-phrase
|
||||
* proximity (when constructed from other {@link
|
||||
* org.apache.lucene.search.spans.SpanNearQuery}s).
|
||||
* <li>A {@link org.apache.lucene.search.spans.SpanWithinQuery SpanWithinQuery} matches spans
|
||||
* org.apache.lucene.queries.spans.SpanNearQuery}s).
|
||||
* <li>A {@link org.apache.lucene.queries.spans.SpanWithinQuery SpanWithinQuery} matches spans
|
||||
* which occur inside of another spans.
|
||||
* <li>A {@link org.apache.lucene.search.spans.SpanContainingQuery SpanContainingQuery} matches
|
||||
* <li>A {@link org.apache.lucene.queries.spans.SpanContainingQuery SpanContainingQuery} matches
|
||||
* spans which contain another spans.
|
||||
* <li>A {@link org.apache.lucene.search.spans.SpanOrQuery SpanOrQuery} merges spans from a number
|
||||
* of other {@link org.apache.lucene.search.spans.SpanQuery}s.
|
||||
* <li>A {@link org.apache.lucene.search.spans.SpanNotQuery SpanNotQuery} removes spans matching
|
||||
* one {@link org.apache.lucene.search.spans.SpanQuery SpanQuery} which overlap (or comes
|
||||
* <li>A {@link org.apache.lucene.queries.spans.SpanOrQuery SpanOrQuery} merges spans from a
|
||||
* number of other {@link org.apache.lucene.queries.spans.SpanQuery}s.
|
||||
* <li>A {@link org.apache.lucene.queries.spans.SpanNotQuery SpanNotQuery} removes spans matching
|
||||
* one {@link org.apache.lucene.queries.spans.SpanQuery SpanQuery} which overlap (or comes
|
||||
* near) another. This can be used, e.g., to implement within-paragraph search.
|
||||
* <li>A {@link org.apache.lucene.search.spans.SpanFirstQuery SpanFirstQuery} matches spans
|
||||
* <li>A {@link org.apache.lucene.queries.spans.SpanFirstQuery SpanFirstQuery} matches spans
|
||||
* matching <code>q</code> whose end position is less than <code>n</code>. This can be used to
|
||||
* constrain matches to the first part of the document.
|
||||
* <li>A {@link org.apache.lucene.search.spans.SpanPositionRangeQuery SpanPositionRangeQuery} is a
|
||||
* more general form of SpanFirstQuery that can constrain matches to arbitrary portions of the
|
||||
* document.
|
||||
* <li>A {@link org.apache.lucene.queries.spans.SpanPositionRangeQuery SpanPositionRangeQuery} is
|
||||
* a more general form of SpanFirstQuery that can constrain matches to arbitrary portions of
|
||||
* the document.
|
||||
* </ul>
|
||||
*
|
||||
* In all cases, output spans are minimally inclusive. In other words, a span formed by matching a
|
||||
|
@ -83,4 +83,4 @@
|
|||
* query.add(new TermQuery("content", "iraq"), true, false);
|
||||
* </pre>
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
|
@ -34,15 +34,15 @@ import org.apache.lucene.index.RandomIndexWriter;
|
|||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.payloads.SpanPayloadCheckQuery.MatchOperation;
|
||||
import org.apache.lucene.queries.payloads.SpanPayloadCheckQuery.PayloadType;
|
||||
import org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanPositionRangeQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.CheckHits;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.WildcardQuery;
|
||||
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanPositionRangeQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.English;
|
||||
|
|
|
@ -17,12 +17,12 @@
|
|||
package org.apache.lucene.queries.payloads;
|
||||
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanBoostQuery;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanOrQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.BaseExplanationTestCase;
|
||||
import org.apache.lucene.search.spans.SpanBoostQuery;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanOrQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
|
||||
/** TestExplanations subclass focusing on payload queries */
|
||||
public class TestPayloadExplanations extends BaseExplanationTestCase {
|
||||
|
|
|
@ -29,6 +29,12 @@ import org.apache.lucene.index.IndexReader;
|
|||
import org.apache.lucene.index.NoMergePolicy;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanContainingQuery;
|
||||
import org.apache.lucene.queries.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanOrQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.CollectionStatistics;
|
||||
import org.apache.lucene.search.Explanation;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
|
@ -37,12 +43,6 @@ import org.apache.lucene.search.TermStatistics;
|
|||
import org.apache.lucene.search.TopDocs;
|
||||
import org.apache.lucene.search.WildcardQuery;
|
||||
import org.apache.lucene.search.similarities.ClassicSimilarity;
|
||||
import org.apache.lucene.search.spans.SpanContainingQuery;
|
||||
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanOrQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.English;
|
||||
|
|
|
@ -0,0 +1,148 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.lucene.queries.payloads;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.apache.lucene.analysis.MockPayloadAnalyzer;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.TextField;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.LeafReader;
|
||||
import org.apache.lucene.index.PostingsEnum;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanCollector;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.queries.spans.SpanWeight;
|
||||
import org.apache.lucene.queries.spans.Spans;
|
||||
import org.apache.lucene.search.DocIdSetIterator;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.ScoreMode;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
public class TestPayloadSpanPositions extends LuceneTestCase {
|
||||
|
||||
static class PayloadSpanCollector implements SpanCollector {
|
||||
|
||||
List<BytesRef> payloads = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void collectLeaf(PostingsEnum postings, int position, Term term) throws IOException {
|
||||
if (postings.getPayload() != null) payloads.add(BytesRef.deepCopyOf(postings.getPayload()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
payloads.clear();
|
||||
}
|
||||
}
|
||||
|
||||
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, ScoreMode.COMPLETE_NO_SCORES, 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, ScoreMode.COMPLETE_NO_SCORES, 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();
|
||||
}
|
||||
}
|
|
@ -37,19 +37,19 @@ import org.apache.lucene.index.IndexReader;
|
|||
import org.apache.lucene.index.PostingsEnum;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanCollector;
|
||||
import org.apache.lucene.queries.spans.SpanFirstQuery;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanNotQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.queries.spans.SpanWeight;
|
||||
import org.apache.lucene.queries.spans.Spans;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.ScoreMode;
|
||||
import org.apache.lucene.search.TopDocs;
|
||||
import org.apache.lucene.search.similarities.ClassicSimilarity;
|
||||
import org.apache.lucene.search.similarities.Similarity;
|
||||
import org.apache.lucene.search.spans.SpanCollector;
|
||||
import org.apache.lucene.search.spans.SpanFirstQuery;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanNotQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.spans.SpanWeight;
|
||||
import org.apache.lucene.search.spans.Spans;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
|
|
@ -28,6 +28,10 @@ import org.apache.lucene.document.Field;
|
|||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.queries.spans.SpanWeight;
|
||||
import org.apache.lucene.queries.spans.Spans;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.CheckHits;
|
||||
|
@ -38,10 +42,6 @@ import org.apache.lucene.search.ScoreMode;
|
|||
import org.apache.lucene.search.TopDocs;
|
||||
import org.apache.lucene.search.similarities.ClassicSimilarity;
|
||||
import org.apache.lucene.search.similarities.Similarity;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.spans.SpanWeight;
|
||||
import org.apache.lucene.search.spans.Spans;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.English;
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.search.DocIdSetIterator;
|
||||
|
@ -148,7 +148,7 @@ class AssertingSpans extends Spans {
|
|||
assert state != State.DOC_FINISHED : "nextDoc() called after NO_MORE_DOCS: " + in;
|
||||
int nextDoc = in.nextDoc();
|
||||
assert nextDoc > doc : "backwards nextDoc from " + doc + " to " + nextDoc + ": " + in;
|
||||
if (nextDoc == DocIdSetIterator.NO_MORE_DOCS) {
|
||||
if (nextDoc == NO_MORE_DOCS) {
|
||||
state = State.DOC_FINISHED;
|
||||
} else {
|
||||
assert in.startPosition() == -1 : "invalid initial startPosition() [should be -1]: " + in;
|
||||
|
@ -166,7 +166,7 @@ class AssertingSpans extends Spans {
|
|||
int advanced = in.advance(target);
|
||||
assert advanced >= target
|
||||
: "backwards advance from: " + target + " to: " + advanced + ": " + in;
|
||||
if (advanced == DocIdSetIterator.NO_MORE_DOCS) {
|
||||
if (advanced == NO_MORE_DOCS) {
|
||||
state = State.DOC_FINISHED;
|
||||
} else {
|
||||
assert in.startPosition() == -1 : "invalid initial startPosition() [should be -1]: " + in;
|
||||
|
@ -215,7 +215,7 @@ class AssertingSpans extends Spans {
|
|||
|
||||
@Override
|
||||
public boolean matches() throws IOException {
|
||||
if (approximation.docID() == -1 || approximation.docID() == DocIdSetIterator.NO_MORE_DOCS) {
|
||||
if (approximation.docID() == -1 || approximation.docID() == NO_MORE_DOCS) {
|
||||
throw new AssertionError(
|
||||
"matches() should not be called on doc ID " + approximation.docID());
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.spanFirstQuery;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.spanNearOrderedQuery;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.spanNearUnorderedQuery;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.spanNotQuery;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.spanOrQuery;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.spanTermQuery;
|
||||
|
||||
import org.apache.lucene.search.BaseExplanationTestCase;
|
||||
|
||||
public abstract class BaseSpanExplanationTestCase extends BaseExplanationTestCase {
|
||||
|
||||
/** MACRO for SpanTermQuery */
|
||||
public SpanQuery st(String s) {
|
||||
return spanTermQuery(FIELD, s);
|
||||
}
|
||||
|
||||
/** MACRO for SpanNotQuery */
|
||||
public SpanQuery snot(SpanQuery i, SpanQuery e) {
|
||||
return spanNotQuery(i, e);
|
||||
}
|
||||
|
||||
/** MACRO for SpanOrQuery containing two SpanTerm queries */
|
||||
public SpanQuery sor(String s, String e) {
|
||||
return spanOrQuery(FIELD, s, e);
|
||||
}
|
||||
|
||||
/** MACRO for SpanOrQuery containing two SpanQueries */
|
||||
public SpanQuery sor(SpanQuery s, SpanQuery e) {
|
||||
return spanOrQuery(s, e);
|
||||
}
|
||||
|
||||
/** MACRO for SpanOrQuery containing three SpanTerm queries */
|
||||
public SpanQuery sor(String s, String m, String e) {
|
||||
return spanOrQuery(FIELD, s, m, e);
|
||||
}
|
||||
/** MACRO for SpanOrQuery containing two SpanQueries */
|
||||
public SpanQuery sor(SpanQuery s, SpanQuery m, SpanQuery e) {
|
||||
return spanOrQuery(s, m, e);
|
||||
}
|
||||
|
||||
/** MACRO for SpanNearQuery containing two SpanTerm queries */
|
||||
public SpanQuery snear(String s, String e, int slop, boolean inOrder) {
|
||||
return snear(st(s), st(e), slop, inOrder);
|
||||
}
|
||||
|
||||
/** MACRO for SpanNearQuery containing two SpanQueries */
|
||||
public SpanQuery snear(SpanQuery s, SpanQuery e, int slop, boolean inOrder) {
|
||||
if (inOrder) {
|
||||
return spanNearOrderedQuery(slop, s, e);
|
||||
} else {
|
||||
return spanNearUnorderedQuery(slop, s, e);
|
||||
}
|
||||
}
|
||||
|
||||
/** MACRO for SpanNearQuery containing three SpanTerm queries */
|
||||
public SpanQuery snear(String s, String m, String e, int slop, boolean inOrder) {
|
||||
return snear(st(s), st(m), st(e), slop, inOrder);
|
||||
}
|
||||
/** MACRO for SpanNearQuery containing three SpanQueries */
|
||||
public SpanQuery snear(SpanQuery s, SpanQuery m, SpanQuery e, int slop, boolean inOrder) {
|
||||
if (inOrder) {
|
||||
return spanNearOrderedQuery(slop, s, m, e);
|
||||
} else {
|
||||
return spanNearUnorderedQuery(slop, s, m, e);
|
||||
}
|
||||
}
|
||||
|
||||
/** MACRO for SpanFirst(SpanTermQuery) */
|
||||
public SpanQuery sf(String s, int b) {
|
||||
return spanFirstQuery(st(s), b);
|
||||
}
|
||||
}
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
|
@ -14,9 +14,9 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.index.Term;
|
|
@ -14,24 +14,32 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.*;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import org.apache.lucene.analysis.*;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.document.TextField;
|
||||
import org.apache.lucene.index.DirectoryReader;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.IndexWriter;
|
||||
import org.apache.lucene.index.IndexWriterConfig;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.CheckHits;
|
||||
import org.apache.lucene.search.DisjunctionMaxQuery;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.PhraseQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.ScoreDoc;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.search.TopScoreDocCollector;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.English;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
@ -414,7 +422,7 @@ public class TestBasics extends LuceneTestCase {
|
|||
public void testSpanOr() throws Exception {
|
||||
SpanQuery near1 = spanNearOrderedQuery("field", 0, "thirty", "three");
|
||||
SpanQuery near2 = spanNearOrderedQuery("field", 0, "forty", "seven");
|
||||
SpanQuery query = spanOrQuery(near1, near2);
|
||||
SpanQuery query = SpanTestUtil.spanOrQuery(near1, near2);
|
||||
|
||||
checkHits(
|
||||
query,
|
||||
|
@ -431,7 +439,7 @@ public class TestBasics extends LuceneTestCase {
|
|||
public void testSpanExactNested() throws Exception {
|
||||
SpanQuery near1 = spanNearOrderedQuery("field", 0, "three", "hundred");
|
||||
SpanQuery near2 = spanNearOrderedQuery("field", 0, "thirty", "three");
|
||||
SpanQuery query = spanNearOrderedQuery(0, near1, near2);
|
||||
SpanQuery query = SpanTestUtil.spanNearOrderedQuery(0, near1, near2);
|
||||
|
||||
checkHits(query, new int[] {333, 1333});
|
||||
|
||||
|
@ -441,7 +449,7 @@ public class TestBasics extends LuceneTestCase {
|
|||
public void testSpanNearOr() throws Exception {
|
||||
SpanQuery to1 = spanOrQuery("field", "six", "seven");
|
||||
SpanQuery to2 = spanOrQuery("field", "seven", "six");
|
||||
SpanQuery query = spanNearOrderedQuery(10, to1, to2);
|
||||
SpanQuery query = SpanTestUtil.spanNearOrderedQuery(10, to1, to2);
|
||||
|
||||
checkHits(
|
||||
query,
|
||||
|
@ -458,9 +466,9 @@ public class TestBasics extends LuceneTestCase {
|
|||
SpanQuery tt1 = spanNearOrderedQuery("field", 0, "six", "hundred");
|
||||
SpanQuery tt2 = spanNearOrderedQuery("field", 0, "seven", "hundred");
|
||||
|
||||
SpanQuery to1 = spanOrQuery(tt1, tt2);
|
||||
SpanQuery to1 = SpanTestUtil.spanOrQuery(tt1, tt2);
|
||||
SpanQuery to2 = spanOrQuery("field", "seven", "six");
|
||||
SpanQuery query = spanNearOrderedQuery(100, to1, to2);
|
||||
SpanQuery query = SpanTestUtil.spanNearOrderedQuery(100, to1, to2);
|
||||
|
||||
checkHits(
|
||||
query,
|
||||
|
@ -476,4 +484,73 @@ public class TestBasics extends LuceneTestCase {
|
|||
private void checkHits(Query query, int[] results) throws IOException {
|
||||
CheckHits.checkHits(random(), query, "field", searcher, results);
|
||||
}
|
||||
|
||||
// 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, Integer.MAX_VALUE);
|
||||
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();
|
||||
}
|
||||
|
||||
// LUCENE-4477 / LUCENE-4401:
|
||||
public void testDismaxSpanQuery() throws Exception {
|
||||
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);
|
||||
|
||||
DisjunctionMaxQuery query =
|
||||
new DisjunctionMaxQuery(
|
||||
Arrays.asList(
|
||||
new SpanTermQuery(new Term(FIELD, "clockwork")),
|
||||
new SpanTermQuery(new Term(FIELD, "clckwork"))),
|
||||
1.0f);
|
||||
TopScoreDocCollector collector = TopScoreDocCollector.create(1000, Integer.MAX_VALUE);
|
||||
searcher.search(query, collector);
|
||||
hits = collector.topDocs().scoreDocs.length;
|
||||
for (ScoreDoc scoreDoc : collector.topDocs().scoreDocs) {
|
||||
System.out.println(scoreDoc.doc);
|
||||
}
|
||||
indexReader.close();
|
||||
assertEquals(hits, 1);
|
||||
directory.close();
|
||||
}
|
||||
}
|
|
@ -14,10 +14,10 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.assertFinished;
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.assertNext;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.assertFinished;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.assertNext;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
|
@ -14,10 +14,10 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.assertFinished;
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.assertNext;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.assertFinished;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.assertNext;
|
||||
|
||||
import org.apache.lucene.analysis.MockAnalyzer;
|
||||
import org.apache.lucene.document.Document;
|
|
@ -0,0 +1,131 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.QueryRescorer;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.search.TopDocs;
|
||||
import org.apache.lucene.search.similarities.ClassicSimilarity;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
public class TestQueryRescorerWithSpans extends LuceneTestCase {
|
||||
|
||||
private IndexSearcher getSearcher(IndexReader r) {
|
||||
IndexSearcher searcher = newSearcher(r);
|
||||
|
||||
// We rely on more tokens = lower score:
|
||||
searcher.setSimilarity(new ClassicSimilarity());
|
||||
|
||||
return searcher;
|
||||
}
|
||||
|
||||
public void testBasic() throws Exception {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig());
|
||||
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("id", "0", Field.Store.YES));
|
||||
doc.add(newTextField("field", "wizard the the the the the oz", Field.Store.NO));
|
||||
w.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("id", "1", Field.Store.YES));
|
||||
// 1 extra token, but wizard and oz are close;
|
||||
doc.add(newTextField("field", "wizard oz the the the the the the", Field.Store.NO));
|
||||
w.addDocument(doc);
|
||||
IndexReader r = w.getReader();
|
||||
w.close();
|
||||
|
||||
// Do ordinary BooleanQuery:
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("field", "wizard")), BooleanClause.Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term("field", "oz")), BooleanClause.Occur.SHOULD);
|
||||
IndexSearcher searcher = getSearcher(r);
|
||||
|
||||
TopDocs hits = searcher.search(bq.build(), 10);
|
||||
assertEquals(2, hits.totalHits.value);
|
||||
assertEquals("0", searcher.doc(hits.scoreDocs[0].doc).get("id"));
|
||||
assertEquals("1", searcher.doc(hits.scoreDocs[1].doc).get("id"));
|
||||
|
||||
// Resort using SpanNearQuery:
|
||||
SpanTermQuery t1 = new SpanTermQuery(new Term("field", "wizard"));
|
||||
SpanTermQuery t2 = new SpanTermQuery(new Term("field", "oz"));
|
||||
SpanNearQuery snq = new SpanNearQuery(new SpanQuery[] {t1, t2}, 0, true);
|
||||
|
||||
TopDocs hits3 = QueryRescorer.rescore(searcher, hits, snq, 2.0, 10);
|
||||
|
||||
// Resorting changed the order:
|
||||
assertEquals(2, hits3.totalHits.value);
|
||||
assertEquals("1", searcher.doc(hits3.scoreDocs[0].doc).get("id"));
|
||||
assertEquals("0", searcher.doc(hits3.scoreDocs[1].doc).get("id"));
|
||||
|
||||
r.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
public void testMissingSecondPassScore() throws Exception {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig());
|
||||
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("id", "0", Field.Store.YES));
|
||||
doc.add(newTextField("field", "wizard the the the the the oz", Field.Store.NO));
|
||||
w.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("id", "1", Field.Store.YES));
|
||||
// 1 extra token, but wizard and oz are close;
|
||||
doc.add(newTextField("field", "wizard oz the the the the the the", Field.Store.NO));
|
||||
w.addDocument(doc);
|
||||
IndexReader r = w.getReader();
|
||||
w.close();
|
||||
|
||||
// Do ordinary BooleanQuery:
|
||||
BooleanQuery.Builder bq = new BooleanQuery.Builder();
|
||||
bq.add(new TermQuery(new Term("field", "wizard")), BooleanClause.Occur.SHOULD);
|
||||
bq.add(new TermQuery(new Term("field", "oz")), BooleanClause.Occur.SHOULD);
|
||||
IndexSearcher searcher = getSearcher(r);
|
||||
|
||||
TopDocs hits = searcher.search(bq.build(), 10);
|
||||
assertEquals(2, hits.totalHits.value);
|
||||
assertEquals("0", searcher.doc(hits.scoreDocs[0].doc).get("id"));
|
||||
assertEquals("1", searcher.doc(hits.scoreDocs[1].doc).get("id"));
|
||||
|
||||
// Resort using SpanNearQuery:
|
||||
SpanTermQuery t1 = new SpanTermQuery(new Term("field", "wizard"));
|
||||
SpanTermQuery t2 = new SpanTermQuery(new Term("field", "oz"));
|
||||
SpanNearQuery snq = new SpanNearQuery(new SpanQuery[] {t1, t2}, 0, true);
|
||||
|
||||
TopDocs hits3 = QueryRescorer.rescore(searcher, hits, snq, 2.0, 10);
|
||||
|
||||
// Resorting changed the order:
|
||||
assertEquals(2, hits3.totalHits.value);
|
||||
assertEquals("1", searcher.doc(hits3.scoreDocs[0].doc).get("id"));
|
||||
assertEquals("0", searcher.doc(hits3.scoreDocs[1].doc).get("id"));
|
||||
|
||||
r.close();
|
||||
dir.close();
|
||||
}
|
||||
}
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
|
@ -14,9 +14,9 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.*;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.*;
|
||||
|
||||
import org.apache.lucene.analysis.MockAnalyzer;
|
||||
import org.apache.lucene.document.Document;
|
|
@ -14,9 +14,12 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.index.DirectoryReader;
|
||||
|
@ -24,10 +27,11 @@ import org.apache.lucene.index.IndexWriter;
|
|||
import org.apache.lucene.index.LeafReaderContext;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.*;
|
||||
import org.apache.lucene.search.BooleanClause.Occur;
|
||||
import org.apache.lucene.store.Directory;
|
||||
|
||||
/** TestExplanations subclass focusing on span queries */
|
||||
public class TestSpanExplanations extends BaseExplanationTestCase {
|
||||
public class TestSpanExplanations extends BaseSpanExplanationTestCase {
|
||||
private static final String FIELD_CONTENT = "content";
|
||||
|
||||
/* simple SpanTermQueries */
|
||||
|
@ -42,6 +46,11 @@ public class TestSpanExplanations extends BaseExplanationTestCase {
|
|||
qtest(new BoostQuery(q, 1000), new int[] {0, 1, 2, 3});
|
||||
}
|
||||
|
||||
public void testST3() throws Exception {
|
||||
SpanQuery q = st("w1");
|
||||
bqtest(new SpanBoostQuery(q, 0), new int[] {0, 1, 2, 3});
|
||||
}
|
||||
|
||||
public void testST4() throws Exception {
|
||||
SpanQuery q = st("xx");
|
||||
qtest(q, new int[] {2, 3});
|
||||
|
@ -52,6 +61,11 @@ public class TestSpanExplanations extends BaseExplanationTestCase {
|
|||
qtest(new BoostQuery(q, 1000), new int[] {2, 3});
|
||||
}
|
||||
|
||||
public void testST6() throws Exception {
|
||||
SpanQuery q = st("xx");
|
||||
qtest(new SpanBoostQuery(q, 0), new int[] {2, 3});
|
||||
}
|
||||
|
||||
/* some SpanFirstQueries */
|
||||
|
||||
public void testSF1() throws Exception {
|
||||
|
@ -64,6 +78,11 @@ public class TestSpanExplanations extends BaseExplanationTestCase {
|
|||
qtest(new BoostQuery(q, 1000), new int[] {0, 1, 2, 3});
|
||||
}
|
||||
|
||||
public void testSF3() throws Exception {
|
||||
SpanQuery q = sf(("w1"), 1);
|
||||
bqtest(new SpanBoostQuery(q, 0), new int[] {0, 1, 2, 3});
|
||||
}
|
||||
|
||||
public void testSF4() throws Exception {
|
||||
SpanQuery q = sf(("xx"), 2);
|
||||
qtest(q, new int[] {2});
|
||||
|
@ -79,6 +98,11 @@ public class TestSpanExplanations extends BaseExplanationTestCase {
|
|||
qtest(new BoostQuery(q, 1000), new int[] {2});
|
||||
}
|
||||
|
||||
public void testSF7() throws Exception {
|
||||
SpanQuery q = sf(("xx"), 3);
|
||||
bqtest(new SpanBoostQuery(q, 0), new int[] {2, 3});
|
||||
}
|
||||
|
||||
/* some SpanOrQueries */
|
||||
|
||||
public void testSO1() throws Exception {
|
||||
|
@ -170,6 +194,11 @@ public class TestSpanExplanations extends BaseExplanationTestCase {
|
|||
qtest(new BoostQuery(q, 1000), new int[] {0, 1, 2, 3});
|
||||
}
|
||||
|
||||
public void testSNot3() throws Exception {
|
||||
SpanQuery q = snot(sf("w1", 10), st("QQ"));
|
||||
bqtest(new SpanBoostQuery(q, 0), new int[] {0, 1, 2, 3});
|
||||
}
|
||||
|
||||
public void testSNot4() throws Exception {
|
||||
SpanQuery q = snot(sf("w1", 10), st("xx"));
|
||||
qtest(q, new int[] {0, 1, 2, 3});
|
||||
|
@ -180,12 +209,33 @@ public class TestSpanExplanations extends BaseExplanationTestCase {
|
|||
qtest(new BoostQuery(q, 1000), new int[] {0, 1, 2, 3});
|
||||
}
|
||||
|
||||
public void testSNot6() throws Exception {
|
||||
SpanQuery q = snot(sf("w1", 10), st("xx"));
|
||||
bqtest(new SpanBoostQuery(q, 0), new int[] {0, 1, 2, 3});
|
||||
}
|
||||
|
||||
public void testSNot7() throws Exception {
|
||||
SpanQuery f = snear("w1", "w3", 10, true);
|
||||
SpanQuery q = snot(f, st("xx"));
|
||||
qtest(q, new int[] {0, 1, 3});
|
||||
}
|
||||
|
||||
public void testSNot8() throws Exception {
|
||||
// NOTE: using qtest not bqtest
|
||||
SpanQuery f = snear("w1", "w3", 10, true);
|
||||
f = new SpanBoostQuery(f, 0);
|
||||
SpanQuery q = snot(f, st("xx"));
|
||||
qtest(q, new int[] {0, 1, 3});
|
||||
}
|
||||
|
||||
public void testSNot9() throws Exception {
|
||||
// NOTE: using qtest not bqtest
|
||||
SpanQuery t = st("xx");
|
||||
t = new SpanBoostQuery(t, 0);
|
||||
SpanQuery q = snot(snear("w1", "w3", 10, true), t);
|
||||
qtest(q, new int[] {0, 1, 3});
|
||||
}
|
||||
|
||||
public void testSNot10() throws Exception {
|
||||
SpanQuery t = st("xx");
|
||||
SpanQuery q = snot(snear("w1", "w3", 10, true), t);
|
||||
|
@ -223,4 +273,124 @@ public class TestSpanExplanations extends BaseExplanationTestCase {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void test1() throws Exception {
|
||||
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
|
||||
PhraseQuery phraseQuery = new PhraseQuery(1, FIELD, "w1", "w2");
|
||||
q.add(phraseQuery, Occur.MUST);
|
||||
q.add(snear(st("w2"), sor("w5", "zz"), 4, true), Occur.SHOULD);
|
||||
q.add(snear(sf("w3", 2), st("w2"), st("w3"), 5, true), Occur.SHOULD);
|
||||
|
||||
Query t =
|
||||
new BooleanQuery.Builder()
|
||||
.add(new TermQuery(new Term(FIELD, "xx")), Occur.MUST)
|
||||
.add(matchTheseItems(new int[] {1, 3}), Occur.FILTER)
|
||||
.build();
|
||||
q.add(new BoostQuery(t, 1000), Occur.SHOULD);
|
||||
|
||||
t = new ConstantScoreQuery(matchTheseItems(new int[] {0, 2}));
|
||||
q.add(new BoostQuery(t, 30), Occur.SHOULD);
|
||||
|
||||
List<Query> disjuncts = new ArrayList<>();
|
||||
disjuncts.add(snear(st("w2"), sor("w5", "zz"), 4, true));
|
||||
disjuncts.add(new TermQuery(new Term(FIELD, "QQ")));
|
||||
|
||||
BooleanQuery.Builder xxYYZZ = new BooleanQuery.Builder();
|
||||
;
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "xx")), Occur.SHOULD);
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "yy")), Occur.SHOULD);
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "zz")), Occur.MUST_NOT);
|
||||
|
||||
disjuncts.add(xxYYZZ.build());
|
||||
|
||||
BooleanQuery.Builder xxW1 = new BooleanQuery.Builder();
|
||||
;
|
||||
xxW1.add(new TermQuery(new Term(FIELD, "xx")), Occur.MUST_NOT);
|
||||
xxW1.add(new TermQuery(new Term(FIELD, "w1")), Occur.MUST_NOT);
|
||||
|
||||
disjuncts.add(xxW1.build());
|
||||
|
||||
List<Query> disjuncts2 = new ArrayList<>();
|
||||
disjuncts2.add(new TermQuery(new Term(FIELD, "w1")));
|
||||
disjuncts2.add(new TermQuery(new Term(FIELD, "w2")));
|
||||
disjuncts2.add(new TermQuery(new Term(FIELD, "w3")));
|
||||
disjuncts.add(new DisjunctionMaxQuery(disjuncts2, 0.5f));
|
||||
|
||||
q.add(new DisjunctionMaxQuery(disjuncts, 0.2f), Occur.SHOULD);
|
||||
|
||||
BooleanQuery.Builder b = new BooleanQuery.Builder();
|
||||
;
|
||||
b.setMinimumNumberShouldMatch(2);
|
||||
b.add(snear("w1", "w2", 1, true), Occur.SHOULD);
|
||||
b.add(snear("w2", "w3", 1, true), Occur.SHOULD);
|
||||
b.add(snear("w1", "w3", 3, true), Occur.SHOULD);
|
||||
|
||||
q.add(b.build(), Occur.SHOULD);
|
||||
|
||||
qtest(q.build(), new int[] {0, 1, 2});
|
||||
}
|
||||
|
||||
public void test2() throws Exception {
|
||||
|
||||
BooleanQuery.Builder q = new BooleanQuery.Builder();
|
||||
|
||||
PhraseQuery phraseQuery = new PhraseQuery(1, FIELD, "w1", "w2");
|
||||
q.add(phraseQuery, Occur.MUST);
|
||||
q.add(snear(st("w2"), sor("w5", "zz"), 4, true), Occur.SHOULD);
|
||||
q.add(snear(sf("w3", 2), st("w2"), st("w3"), 5, true), Occur.SHOULD);
|
||||
|
||||
Query t =
|
||||
new BooleanQuery.Builder()
|
||||
.add(new TermQuery(new Term(FIELD, "xx")), Occur.MUST)
|
||||
.add(matchTheseItems(new int[] {1, 3}), Occur.FILTER)
|
||||
.build();
|
||||
q.add(new BoostQuery(t, 1000), Occur.SHOULD);
|
||||
|
||||
t = new ConstantScoreQuery(matchTheseItems(new int[] {0, 2}));
|
||||
q.add(new BoostQuery(t, 20), Occur.SHOULD);
|
||||
|
||||
List<Query> disjuncts = new ArrayList<>();
|
||||
disjuncts.add(snear(st("w2"), sor("w5", "zz"), 4, true));
|
||||
disjuncts.add(new TermQuery(new Term(FIELD, "QQ")));
|
||||
|
||||
BooleanQuery.Builder xxYYZZ = new BooleanQuery.Builder();
|
||||
;
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "xx")), Occur.SHOULD);
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "yy")), Occur.SHOULD);
|
||||
xxYYZZ.add(new TermQuery(new Term(FIELD, "zz")), Occur.MUST_NOT);
|
||||
|
||||
disjuncts.add(xxYYZZ.build());
|
||||
|
||||
BooleanQuery.Builder xxW1 = new BooleanQuery.Builder();
|
||||
;
|
||||
xxW1.add(new TermQuery(new Term(FIELD, "xx")), Occur.MUST_NOT);
|
||||
xxW1.add(new TermQuery(new Term(FIELD, "w1")), Occur.MUST_NOT);
|
||||
|
||||
disjuncts.add(xxW1.build());
|
||||
|
||||
DisjunctionMaxQuery dm2 =
|
||||
new DisjunctionMaxQuery(
|
||||
Arrays.asList(
|
||||
new TermQuery(new Term(FIELD, "w1")),
|
||||
new TermQuery(new Term(FIELD, "w2")),
|
||||
new TermQuery(new Term(FIELD, "w3"))),
|
||||
0.5f);
|
||||
disjuncts.add(dm2);
|
||||
|
||||
q.add(new DisjunctionMaxQuery(disjuncts, 0.2f), Occur.SHOULD);
|
||||
|
||||
BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
||||
;
|
||||
builder.setMinimumNumberShouldMatch(2);
|
||||
builder.add(snear("w1", "w2", 1, true), Occur.SHOULD);
|
||||
builder.add(snear("w2", "w3", 1, true), Occur.SHOULD);
|
||||
builder.add(snear("w1", "w3", 3, true), Occur.SHOULD);
|
||||
BooleanQuery b = builder.build();
|
||||
|
||||
q.add(new BoostQuery(b, 0), Occur.SHOULD);
|
||||
|
||||
qtest(q.build(), new int[] {0, 1, 2});
|
||||
}
|
||||
}
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import org.apache.lucene.search.CheckHits;
|
||||
import org.apache.lucene.search.Query;
|
|
@ -14,9 +14,9 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.*;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.*;
|
||||
|
||||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.analysis.MockAnalyzer;
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.MatchesTestBase;
|
||||
import org.apache.lucene.search.Query;
|
||||
|
||||
public class TestSpanMatches extends MatchesTestBase {
|
||||
|
||||
@Override
|
||||
protected String[] getDocuments() {
|
||||
return new String[] {
|
||||
"w1 w2 w3 w4 w5",
|
||||
"w1 w3 w2 w3 zz",
|
||||
"w1 xx w2 yy w4",
|
||||
"w1 w2 w1 w4 w2 w3",
|
||||
"a phrase sentence with many phrase sentence iterations of a phrase sentence",
|
||||
"nothing matches this document"
|
||||
};
|
||||
}
|
||||
|
||||
public void testSpanQuery() throws IOException {
|
||||
SpanQuery subq =
|
||||
SpanNearQuery.newOrderedNearQuery(FIELD_WITH_OFFSETS)
|
||||
.addClause(new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "with")))
|
||||
.addClause(new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "many")))
|
||||
.build();
|
||||
Query q =
|
||||
SpanNearQuery.newOrderedNearQuery(FIELD_WITH_OFFSETS)
|
||||
.addClause(new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "sentence")))
|
||||
.addClause(
|
||||
new SpanOrQuery(
|
||||
subq, new SpanTermQuery(new Term(FIELD_WITH_OFFSETS, "iterations"))))
|
||||
.build();
|
||||
checkMatches(
|
||||
q, FIELD_WITH_OFFSETS, new int[][] {{0}, {1}, {2}, {3}, {4, 2, 4, 9, 27, 6, 7, 35, 54}});
|
||||
checkLabelCount(q, FIELD_WITH_OFFSETS, new int[] {0, 0, 0, 0, 1});
|
||||
checkTermMatches(
|
||||
q,
|
||||
FIELD_WITH_OFFSETS,
|
||||
new TermMatch[][][] {
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
{
|
||||
{new TermMatch(2, 9, 17), new TermMatch(3, 18, 22), new TermMatch(4, 23, 27)},
|
||||
{new TermMatch(6, 35, 43), new TermMatch(7, 44, 54)}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.document.Document;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.document.Document;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.document.Document;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.QueryUtils;
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.BoostQuery;
|
||||
import org.apache.lucene.search.PhraseQuery;
|
||||
import org.apache.lucene.search.PrefixQuery;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.QueryVisitor;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
public class TestSpanQueryVisitor extends LuceneTestCase {
|
||||
|
||||
private static final Query query =
|
||||
new BooleanQuery.Builder()
|
||||
.add(new TermQuery(new Term("field1", "t1")), BooleanClause.Occur.MUST)
|
||||
.add(
|
||||
new BooleanQuery.Builder()
|
||||
.add(new TermQuery(new Term("field1", "tm2")), BooleanClause.Occur.SHOULD)
|
||||
.add(
|
||||
new BoostQuery(new TermQuery(new Term("field1", "tm3")), 2),
|
||||
BooleanClause.Occur.SHOULD)
|
||||
.build(),
|
||||
BooleanClause.Occur.MUST)
|
||||
.add(
|
||||
new BoostQuery(
|
||||
new PhraseQuery.Builder()
|
||||
.add(new Term("field1", "term4"))
|
||||
.add(new Term("field1", "term5"))
|
||||
.build(),
|
||||
3),
|
||||
BooleanClause.Occur.MUST)
|
||||
.add(
|
||||
new SpanNearQuery(
|
||||
new SpanQuery[] {
|
||||
new SpanTermQuery(new Term("field1", "term6")),
|
||||
new SpanTermQuery(new Term("field1", "term7"))
|
||||
},
|
||||
2,
|
||||
true),
|
||||
BooleanClause.Occur.MUST)
|
||||
.add(new TermQuery(new Term("field1", "term8")), BooleanClause.Occur.MUST_NOT)
|
||||
.add(new PrefixQuery(new Term("field1", "term9")), BooleanClause.Occur.SHOULD)
|
||||
.add(
|
||||
new BoostQuery(
|
||||
new BooleanQuery.Builder()
|
||||
.add(
|
||||
new BoostQuery(new TermQuery(new Term("field2", "term10")), 3),
|
||||
BooleanClause.Occur.MUST)
|
||||
.build(),
|
||||
2),
|
||||
BooleanClause.Occur.SHOULD)
|
||||
.build();
|
||||
|
||||
public void testExtractTermsEquivalent() {
|
||||
Set<Term> terms = new HashSet<>();
|
||||
Set<Term> expected =
|
||||
new HashSet<>(
|
||||
Arrays.asList(
|
||||
new Term("field1", "t1"), new Term("field1", "tm2"),
|
||||
new Term("field1", "tm3"), new Term("field1", "term4"),
|
||||
new Term("field1", "term5"), new Term("field1", "term6"),
|
||||
new Term("field1", "term7"), new Term("field2", "term10")));
|
||||
query.visit(QueryVisitor.termCollector(terms));
|
||||
assertThat(terms, equalTo(expected));
|
||||
}
|
||||
}
|
|
@ -14,9 +14,9 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.*;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.*;
|
||||
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.BooleanClause.Occur;
|
|
@ -0,0 +1,163 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.FieldType;
|
||||
import org.apache.lucene.document.TextField;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.TopDocs;
|
||||
import org.apache.lucene.search.similarities.AfterEffect;
|
||||
import org.apache.lucene.search.similarities.AfterEffectB;
|
||||
import org.apache.lucene.search.similarities.AfterEffectL;
|
||||
import org.apache.lucene.search.similarities.AxiomaticF1EXP;
|
||||
import org.apache.lucene.search.similarities.AxiomaticF1LOG;
|
||||
import org.apache.lucene.search.similarities.AxiomaticF2EXP;
|
||||
import org.apache.lucene.search.similarities.AxiomaticF2LOG;
|
||||
import org.apache.lucene.search.similarities.AxiomaticF3EXP;
|
||||
import org.apache.lucene.search.similarities.AxiomaticF3LOG;
|
||||
import org.apache.lucene.search.similarities.BM25Similarity;
|
||||
import org.apache.lucene.search.similarities.BasicModel;
|
||||
import org.apache.lucene.search.similarities.BasicModelG;
|
||||
import org.apache.lucene.search.similarities.BasicModelIF;
|
||||
import org.apache.lucene.search.similarities.BasicModelIn;
|
||||
import org.apache.lucene.search.similarities.BasicModelIne;
|
||||
import org.apache.lucene.search.similarities.BooleanSimilarity;
|
||||
import org.apache.lucene.search.similarities.ClassicSimilarity;
|
||||
import org.apache.lucene.search.similarities.DFISimilarity;
|
||||
import org.apache.lucene.search.similarities.DFRSimilarity;
|
||||
import org.apache.lucene.search.similarities.Distribution;
|
||||
import org.apache.lucene.search.similarities.DistributionLL;
|
||||
import org.apache.lucene.search.similarities.DistributionSPL;
|
||||
import org.apache.lucene.search.similarities.IBSimilarity;
|
||||
import org.apache.lucene.search.similarities.Independence;
|
||||
import org.apache.lucene.search.similarities.IndependenceChiSquared;
|
||||
import org.apache.lucene.search.similarities.IndependenceSaturated;
|
||||
import org.apache.lucene.search.similarities.IndependenceStandardized;
|
||||
import org.apache.lucene.search.similarities.LMDirichletSimilarity;
|
||||
import org.apache.lucene.search.similarities.LMJelinekMercerSimilarity;
|
||||
import org.apache.lucene.search.similarities.Lambda;
|
||||
import org.apache.lucene.search.similarities.LambdaDF;
|
||||
import org.apache.lucene.search.similarities.LambdaTTF;
|
||||
import org.apache.lucene.search.similarities.Normalization;
|
||||
import org.apache.lucene.search.similarities.NormalizationH1;
|
||||
import org.apache.lucene.search.similarities.NormalizationH2;
|
||||
import org.apache.lucene.search.similarities.NormalizationH3;
|
||||
import org.apache.lucene.search.similarities.NormalizationZ;
|
||||
import org.apache.lucene.search.similarities.Similarity;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
public class TestSpanSimilarity extends LuceneTestCase {
|
||||
|
||||
List<Similarity> sims;
|
||||
|
||||
/** The DFR basic models to test. */
|
||||
static BasicModel[] BASIC_MODELS = {
|
||||
new BasicModelG(), new BasicModelIF(), new BasicModelIn(), new BasicModelIne()
|
||||
};
|
||||
/** The DFR aftereffects to test. */
|
||||
static AfterEffect[] AFTER_EFFECTS = {new AfterEffectB(), new AfterEffectL()};
|
||||
|
||||
static Normalization[] NORMALIZATIONS = {
|
||||
new NormalizationH1(),
|
||||
new NormalizationH2(),
|
||||
new NormalizationH3(),
|
||||
new NormalizationZ(),
|
||||
new Normalization.NoNormalization()
|
||||
};
|
||||
/** The distributions for IB. */
|
||||
static Distribution[] DISTRIBUTIONS = {new DistributionLL(), new DistributionSPL()};
|
||||
/** Lambdas for IB. */
|
||||
static Lambda[] LAMBDAS = {new LambdaDF(), new LambdaTTF()};
|
||||
/** Independence measures for DFI */
|
||||
static Independence[] INDEPENDENCE_MEASURES = {
|
||||
new IndependenceStandardized(), new IndependenceSaturated(), new IndependenceChiSquared()
|
||||
};
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
sims = new ArrayList<>();
|
||||
sims.add(new ClassicSimilarity());
|
||||
sims.add(new BM25Similarity());
|
||||
sims.add(new BooleanSimilarity());
|
||||
sims.add(new AxiomaticF1EXP());
|
||||
sims.add(new AxiomaticF1LOG());
|
||||
sims.add(new AxiomaticF2EXP());
|
||||
sims.add(new AxiomaticF2LOG());
|
||||
sims.add(new AxiomaticF3EXP(0.25f, 3));
|
||||
sims.add(new AxiomaticF3LOG(0.25f, 3));
|
||||
// TODO: not great that we dup this all with TestSimilarityBase
|
||||
for (BasicModel basicModel : BASIC_MODELS) {
|
||||
for (AfterEffect afterEffect : AFTER_EFFECTS) {
|
||||
for (Normalization normalization : NORMALIZATIONS) {
|
||||
sims.add(new DFRSimilarity(basicModel, afterEffect, normalization));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Distribution distribution : DISTRIBUTIONS) {
|
||||
for (Lambda lambda : LAMBDAS) {
|
||||
for (Normalization normalization : NORMALIZATIONS) {
|
||||
sims.add(new IBSimilarity(distribution, lambda, normalization));
|
||||
}
|
||||
}
|
||||
}
|
||||
sims.add(new LMDirichletSimilarity());
|
||||
sims.add(new LMJelinekMercerSimilarity(0.1f));
|
||||
sims.add(new LMJelinekMercerSimilarity(0.7f));
|
||||
for (Independence independence : INDEPENDENCE_MEASURES) {
|
||||
sims.add(new DFISimilarity(independence));
|
||||
}
|
||||
}
|
||||
|
||||
/** make sure all sims work with spanOR(termX, termY) where termY does not exist */
|
||||
public void testCrazySpans() throws Exception {
|
||||
// historically this was a problem, but sim's no longer have to score terms that dont exist
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
FieldType ft = new FieldType(TextField.TYPE_NOT_STORED);
|
||||
doc.add(newField("foo", "bar", ft));
|
||||
iw.addDocument(doc);
|
||||
IndexReader ir = iw.getReader();
|
||||
iw.close();
|
||||
IndexSearcher is = newSearcher(ir);
|
||||
|
||||
for (Similarity sim : sims) {
|
||||
is.setSimilarity(sim);
|
||||
SpanTermQuery s1 = new SpanTermQuery(new Term("foo", "bar"));
|
||||
SpanTermQuery s2 = new SpanTermQuery(new Term("foo", "baz"));
|
||||
Query query = new SpanOrQuery(s1, s2);
|
||||
TopDocs td = is.search(query, 10);
|
||||
assertEquals(1, td.totalHits.value);
|
||||
float score = td.scoreDocs[0].score;
|
||||
assertFalse("negative score for " + sim, score < 0.0f);
|
||||
assertFalse("inf score for " + sim, Float.isInfinite(score));
|
||||
assertFalse("nan score for " + sim, Float.isNaN(score));
|
||||
}
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
}
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.document.Document;
|
|
@ -14,15 +14,15 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.assertFinished;
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.assertNext;
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.spanNearOrderedQuery;
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.spanNearUnorderedQuery;
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.spanNotQuery;
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.spanOrQuery;
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.spanTermQuery;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.assertFinished;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.assertNext;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.spanNearOrderedQuery;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.spanNearUnorderedQuery;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.spanNotQuery;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.spanOrQuery;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.spanTermQuery;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.analysis.MockAnalyzer;
|
||||
|
@ -102,7 +102,7 @@ public class TestSpans extends LuceneTestCase {
|
|||
|
||||
private void orderedSlopTest3SQ(
|
||||
SpanQuery q1, SpanQuery q2, SpanQuery q3, int slop, int[] expectedDocs) throws IOException {
|
||||
SpanQuery query = spanNearOrderedQuery(slop, q1, q2, q3);
|
||||
SpanQuery query = SpanTestUtil.spanNearOrderedQuery(slop, q1, q2, q3);
|
||||
checkHits(query, expectedDocs);
|
||||
}
|
||||
|
||||
|
@ -221,7 +221,7 @@ public class TestSpans extends LuceneTestCase {
|
|||
assertNext(spans, 10, 0, 2);
|
||||
assertFinished(spans);
|
||||
|
||||
senq = spanNearUnorderedQuery(1, senq, spanTermQuery(field, "u2"));
|
||||
senq = SpanTestUtil.spanNearUnorderedQuery(1, senq, spanTermQuery(field, "u2"));
|
||||
spans =
|
||||
senq.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f)
|
||||
.getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
|
||||
|
@ -312,9 +312,9 @@ public class TestSpans extends LuceneTestCase {
|
|||
// LUCENE-1404
|
||||
private SpanQuery createSpan(int slop, boolean ordered, SpanQuery[] clauses) {
|
||||
if (ordered) {
|
||||
return spanNearOrderedQuery(slop, clauses);
|
||||
return SpanTestUtil.spanNearOrderedQuery(slop, clauses);
|
||||
} else {
|
||||
return spanNearUnorderedQuery(slop, clauses);
|
||||
return SpanTestUtil.spanNearUnorderedQuery(slop, clauses);
|
||||
}
|
||||
}
|
||||
|
|
@ -14,9 +14,9 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.lucene.search.spans;
|
||||
package org.apache.lucene.queries.spans;
|
||||
|
||||
import static org.apache.lucene.search.spans.SpanTestUtil.*;
|
||||
import static org.apache.lucene.queries.spans.SpanTestUtil.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.lucene.analysis.MockAnalyzer;
|
|
@ -24,6 +24,12 @@ import java.util.Objects;
|
|||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanBoostQuery;
|
||||
import org.apache.lucene.queries.spans.SpanNearQuery;
|
||||
import org.apache.lucene.queries.spans.SpanNotQuery;
|
||||
import org.apache.lucene.queries.spans.SpanOrQuery;
|
||||
import org.apache.lucene.queries.spans.SpanQuery;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.queryparser.classic.ParseException;
|
||||
import org.apache.lucene.queryparser.classic.QueryParser;
|
||||
import org.apache.lucene.search.BooleanClause;
|
||||
|
@ -38,12 +44,6 @@ import org.apache.lucene.search.Query;
|
|||
import org.apache.lucene.search.QueryVisitor;
|
||||
import org.apache.lucene.search.SynonymQuery;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.search.spans.SpanBoostQuery;
|
||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||
import org.apache.lucene.search.spans.SpanNotQuery;
|
||||
import org.apache.lucene.search.spans.SpanOrQuery;
|
||||
import org.apache.lucene.search.spans.SpanQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
|
||||
/**
|
||||
* QueryParser which permits complex phrase query syntax eg "(john jon jonathan~) peters*".
|
||||
|
|
|
@ -25,8 +25,8 @@ package org.apache.lucene.queryparser.surround.query;
|
|||
*/
|
||||
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.queries.spans.SpanTermQuery;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
|
||||
/** Factory for creating basic term queries */
|
||||
public class BasicQueryFactory {
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue