Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.uhighlight.UnifiedHighlighter.HighlightFlag;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.LuceneTestCase;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -1082,4 +1083,79 @@ protected TokenStreamComponents createComponents(String fieldName) {

ir.close();
}

public void testOverlappingWildcardInterval() throws Exception {
String text = "Compare Computer Science";

RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);

Field body = new Field("body", text, fieldType);
Document document = new Document();
document.add(body);
iw.addDocument(document);
IndexReader ir = iw.getReader();
iw.close();
IndexSearcher searcher = newSearcher(ir);
Query query =
new IntervalQuery(
"body",
Intervals.maxgaps(
3,
Intervals.ordered(
Intervals.wildcard(new BytesRef("comp*")), Intervals.term("science"))));
TopDocs topDocs = searcher.search(query, 10);
assertEquals(1, topDocs.totalHits.value);
UnifiedHighlighter highlighter =
new UnifiedHighlighter(searcher, indexAnalyzer) {
@Override
protected Set<HighlightFlag> getFlags(String field) {
Set<HighlightFlag> flags = super.getFlags(field);
flags.add(HighlightFlag.WEIGHT_MATCHES);
return flags;
}
};

String snippets[] = highlighter.highlight("body", query, topDocs, 5);
assertEquals(1, snippets.length);
ir.close();
}

public void testRepeatingIntervals() throws Exception {
String text = "a a b a b a";

RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);

Field body = new Field("body", text, fieldType);
Document document = new Document();
document.add(body);
iw.addDocument(document);
IndexReader ir = iw.getReader();
iw.close();
IndexSearcher searcher = newSearcher(ir);
Query query =
new IntervalQuery(
"body",
Intervals.ordered(
Intervals.term("a"),
Intervals.term("b"),
Intervals.term("a"),
Intervals.term("b"),
Intervals.term("a")));
TopDocs topDocs = searcher.search(query, 10);
assertEquals(1, topDocs.totalHits.value);
UnifiedHighlighter highlighter =
new UnifiedHighlighter(searcher, indexAnalyzer) {
@Override
protected Set<HighlightFlag> getFlags(String field) {
Set<HighlightFlag> flags = super.getFlags(field);
flags.add(HighlightFlag.WEIGHT_MATCHES);
return flags;
}
};

String snippets[] = highlighter.highlight("body", query, topDocs, 5);
assertEquals(1, snippets.length);
assertEquals("a <b>a</b> <b>b</b> <b>a</b> <b>b</b> <b>a</b>", snippets[0]);
ir.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public int nextInterval() throws IOException {
}
start = subIterators.get(0).start();
end = subIterators.get(subIterators.size() - 1).end();
matchFound();
return start;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.util.ArrayUtil;

class CachingMatchesIterator extends FilterMatchesIterator implements IntervalMatchesIterator {
class CachingMatchesIterator extends FilterMatchesIterator
implements IntervalMatchesIterator, MinimizingAwareIntervalIterator {

private boolean positioned = false;
private boolean matchCached;
private int[] posAndOffsets = new int[4 * 4];
private Query[] matchingQueries = new Query[4];
private int count = 0;
Expand All @@ -34,7 +35,9 @@ class CachingMatchesIterator extends FilterMatchesIterator implements IntervalMa
super(in);
}

private void cache() throws IOException {
@Override
public void matchFound() throws IOException {
if (matchCached) return;
count = 0;
MatchesIterator mi = in.getSubMatches();
if (mi == null) {
Expand All @@ -58,36 +61,27 @@ private void cache() throws IOException {
count++;
}
}
matchCached = true;
}

@Override
public boolean next() throws IOException {
if (positioned == false) {
positioned = true;
} else {
cache();
}
matchCached = false;
return in.next();
}

int startOffset(int endPos) throws IOException {
if (endPosition() <= endPos) {
return in.startOffset();
}
@Override
public int startOffset() throws IOException {
return posAndOffsets[2];
}

int endOffset(int endPos) throws IOException {
if (endPosition() <= endPos) {
return in.endOffset();
}
return posAndOffsets[count * 4 + 3];
@Override
public int endOffset() throws IOException {
return posAndOffsets[(count - 1) * 4 + 3];
}

MatchesIterator getSubMatches(int endPos) throws IOException {
if (endPosition() <= endPos) {
cache();
}
@Override
public MatchesIterator getSubMatches() throws IOException {
return new MatchesIterator() {

int upto = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.lucene.queries.intervals;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.search.ConjunctionUtils;
import org.apache.lucene.search.DocIdSetIterator;
Expand All @@ -26,18 +27,36 @@ abstract class ConjunctionIntervalIterator extends IntervalIterator {

final DocIdSetIterator approximation;
final List<IntervalIterator> subIterators;
final List<MinimizingAwareIntervalIterator> minimizingAware;
final float cost;

ConjunctionIntervalIterator(List<IntervalIterator> subIterators) {
this.approximation = ConjunctionUtils.intersectIterators(subIterators);
this.subIterators = subIterators;

List<MinimizingAwareIntervalIterator> minimizingAware = null;
float costsum = 0;
for (IntervalIterator it : subIterators) {
costsum += it.matchCost();
if (it instanceof MinimizingAwareIntervalIterator) {
if (minimizingAware == null) {
minimizingAware = new ArrayList<>(subIterators.size());
}
minimizingAware.add((MinimizingAwareIntervalIterator) it);
}
}
this.minimizingAware = minimizingAware;
this.cost = costsum;
}

protected final void matchFound() throws IOException {
if (minimizingAware != null) {
for (MinimizingAwareIntervalIterator sub : minimizingAware) {
sub.matchFound();
}
}
}

@Override
public int docID() {
return approximation.docID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ public final IntervalMatchesIterator matches(String field, LeafReaderContext ctx
if (it.nextInterval() == IntervalIterator.NO_MORE_INTERVALS) {
return null;
}
return isMinimizing
? new MinimizingConjunctionMatchesIterator(it, subs)
: new ConjunctionMatchesIterator(it, subs);
return new ConjunctionMatchesIterator(it, subs);
}

private static class ConjunctionMatchesIterator implements IntervalMatchesIterator {
Expand Down
Loading