Skip to content

Add a highlighter unit test base class #85719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2022
Merged
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
@@ -0,0 +1,41 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.search.fetch.subphase.highlight;

import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.HighlighterTestCase;

import java.io.IOException;
import java.util.Map;

public class CustomUnifiedHighlighterTests extends HighlighterTestCase {

public void testSimpleTermHighlighting() throws IOException {

MapperService mapperService = createMapperService("""
{ "_doc" : { "properties" : {
"field" : { "type" : "text" }
}}}
""");

ParsedDocument doc = mapperService.documentMapper().parse(source("""
{ "field" : "this is some text" }
"""));

SearchSourceBuilder search = new SearchSourceBuilder().query(QueryBuilders.termQuery("field", "some"))
.highlighter(new HighlightBuilder().field("field"));

Map<String, HighlightField> highlights = highlight(mapperService, doc, search);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth to have even higher level abstraction, and provide a mapping and a document as Strings to highlight method, and highlight method itself inside create MapperService and ParsedDocument?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did think about doing things that way, but I think this is slightly more flexible - it allows you to build mappings from XContent if you want to build something programmatically, for example. If it turns out that we're almost always using this pattern we can always add a sugar method that just takes strings.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explanation, Alan, makes sense.

assertHighlights(highlights, "field", "this is <em>some</em> text");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.search.fetch;

import org.apache.lucene.search.IndexSearcher;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.MapperServiceTestCase;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.query.ParsedQuery;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.FastVectorHighlighter;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightPhase;
import org.elasticsearch.search.fetch.subphase.highlight.Highlighter;
import org.elasticsearch.search.fetch.subphase.highlight.PlainHighlighter;
import org.elasticsearch.search.fetch.subphase.highlight.UnifiedHighlighter;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class HighlighterTestCase extends MapperServiceTestCase {

protected Map<String, Highlighter> getHighlighters() {
return Map.of(
"unified",
new UnifiedHighlighter(),
"fvh",
new FastVectorHighlighter(getIndexSettings()),
"plain",
new PlainHighlighter()
);
}

/**
* Runs the highlight phase for a search over a specific document
* @param mapperService the Mappings to use for highlighting
* @param doc a parsed document to highlight
* @param search the search to highlight
*/
protected final Map<String, HighlightField> highlight(MapperService mapperService, ParsedDocument doc, SearchSourceBuilder search)
throws IOException {
Map<String, HighlightField> highlights = new HashMap<>();
withLuceneIndex(mapperService, iw -> iw.addDocument(doc.rootDoc()), ir -> {
SearchExecutionContext context = createSearchExecutionContext(mapperService, new IndexSearcher(ir));
HighlightPhase highlightPhase = new HighlightPhase(getHighlighters());
FetchSubPhaseProcessor processor = highlightPhase.getProcessor(fetchContext(context, search));
FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext(new SearchHit(0, "id", null, null), ir.leaves().get(0), 0);
processor.process(hitContext);
highlights.putAll(hitContext.hit().getHighlightFields());
});
return highlights;
}

/**
* Given a set of highlights, assert that any particular field has the expected fragments
*/
protected static void assertHighlights(Map<String, HighlightField> highlights, String field, String... fragments) {
assertNotNull(highlights.get(field));
Set<String> actualFragments = Arrays.stream(highlights.get(field).getFragments()).map(Text::toString).collect(Collectors.toSet());
Set<String> expectedFragments = Set.of(fragments);
assertEquals(expectedFragments, actualFragments);
}

private static FetchContext fetchContext(SearchExecutionContext context, SearchSourceBuilder search) throws IOException {
FetchContext fetchContext = mock(FetchContext.class);
when(fetchContext.highlight()).thenReturn(search.highlighter().build(context));
when(fetchContext.parsedQuery()).thenReturn(new ParsedQuery(search.query().toQuery(context)));
when(fetchContext.getSearchExecutionContext()).thenReturn(context);
return fetchContext;
}
}