-
Notifications
You must be signed in to change notification settings - Fork 25.3k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...java/org/elasticsearch/search/fetch/subphase/highlight/CustomUnifiedHighlighterTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
assertHighlights(highlights, "field", "this is <em>some</em> text"); | ||
} | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
test/framework/src/main/java/org/elasticsearch/search/fetch/HighlighterTestCase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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, andhighlight
method itself inside createMapperService
andParsedDocument
?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.