Skip to content

Commit

Permalink
tidy-ed
Browse files Browse the repository at this point in the history
  • Loading branch information
vletard committed Mar 6, 2024
1 parent e2a961a commit 8801851
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
Expand Up @@ -100,11 +100,8 @@ public class UnifiedHighlighter {

public static final int DEFAULT_CACHE_CHARS_THRESHOLD = 524288; // ~ 1 MB (2 byte chars)

public static final Set<Class<? extends Query>> QUERIES_WITH_NO_HL_EFFECT = Set.of(
MatchAllDocsQuery.class,
MatchNoDocsQuery.class,
FunctionQuery.class
);
public static final Set<Class<? extends Query>> QUERIES_WITH_NO_HL_EFFECT =
Set.of(MatchAllDocsQuery.class, MatchNoDocsQuery.class, FunctionQuery.class);

protected static final LabelledCharArrayMatcher[] ZERO_LEN_AUTOMATA_ARRAY =
new LabelledCharArrayMatcher[0];
Expand Down Expand Up @@ -1138,7 +1135,8 @@ public boolean acceptField(String field) {
public void visitLeaf(Query query) {
if (MultiTermHighlighting.canExtractAutomataFromLeafQuery(query) == false) {
boolean no_effect_query = false;
for (Class<? extends Query> queryType: UnifiedHighlighter.QUERIES_WITH_NO_HL_EFFECT) {
for (Class<? extends Query> queryType :
UnifiedHighlighter.QUERIES_WITH_NO_HL_EFFECT) {
if (queryType.isInstance(query)) {
no_effect_query = true;
break;
Expand Down
Expand Up @@ -1669,15 +1669,17 @@ public void testQueryWithLongTerm() throws IOException {

public void testPostingsOffsetStrategy() throws Exception {
if (this.fieldType.indexOptions() != IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS
|| this.fieldType.storeTermVectors())
|| this.fieldType.storeTermVectors()) {
// ignore if fieldType is not POSTINGS only
return;
}

final UnifiedHighlighter.OffsetSource expectedOffsetSource;
if (this.fieldType.storeTermVectors())
if (this.fieldType.storeTermVectors()) {
expectedOffsetSource = UnifiedHighlighter.OffsetSource.POSTINGS_WITH_TERM_VECTORS;
else
} else {
expectedOffsetSource = UnifiedHighlighter.OffsetSource.POSTINGS;
}

RandomIndexWriter iw = newIndexOrderPreservingWriter();

Expand All @@ -1700,22 +1702,40 @@ public void testPostingsOffsetStrategy() throws Exception {
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);

Set<Term> queryTerms = UnifiedHighlighter.extractTerms(query);
FieldHighlighter fieldHighlighter = highlighter.getFieldHighlighter("body", query, queryTerms, 1);
assertEquals(expectedOffsetSource, fieldHighlighter.getOffsetSource()); // TermQuery is compatible with POSTINGS offset strategy
FieldHighlighter fieldHighlighter =
highlighter.getFieldHighlighter("body", query, queryTerms, 1);
assertEquals(
expectedOffsetSource,
fieldHighlighter
.getOffsetSource()); // TermQuery is compatible with POSTINGS offset strategy

String[] snippets = highlighter.highlight("body", query, topDocs);

for (Query noEffectQuery: new Query[] {new MatchAllDocsQuery(), new MatchNoDocsQuery(), new FunctionQuery(new ConstValueSource(5))}) {
final Query booleanQuery = new BooleanQuery.Builder()
.add(noEffectQuery, BooleanClause.Occur.MUST)
.add(query, BooleanClause.Occur.MUST)
.build();
for (Query noEffectQuery :
new Query[] {
new MatchAllDocsQuery(),
new MatchNoDocsQuery(),
new FunctionQuery(new ConstValueSource(5))
}) {
final Query booleanQuery =
new BooleanQuery.Builder()
.add(noEffectQuery, BooleanClause.Occur.MUST)
.add(query, BooleanClause.Occur.MUST)
.build();
queryTerms = UnifiedHighlighter.extractTerms(booleanQuery);
fieldHighlighter = highlighter.getFieldHighlighter("body", booleanQuery, queryTerms, 1);
assertEquals(noEffectQuery.getClass().toString(), expectedOffsetSource, fieldHighlighter.getOffsetSource()); // combining to a query with no effet (on highlighting) should lead to the same highlighter behavior
assertEquals(
noEffectQuery.getClass().toString(),
expectedOffsetSource,
fieldHighlighter
.getOffsetSource()); // combining to a query with no effet (on highlighting) should
// lead to the same highlighter behavior

String[] bqSnippets = highlighter.highlight("body", query, topDocs);
assertArrayEquals(bqSnippets.toString(), snippets, bqSnippets); // ensuring that the combined query does produce the same output
assertArrayEquals(
Arrays.toString(bqSnippets),
snippets,
bqSnippets); // ensuring that the combined query does produce the same output
}

ir.close();
Expand Down

0 comments on commit 8801851

Please sign in to comment.