|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.search.fetch; |
| 10 | + |
| 11 | +import org.apache.lucene.search.IndexSearcher; |
| 12 | +import org.elasticsearch.common.text.Text; |
| 13 | +import org.elasticsearch.index.mapper.MapperService; |
| 14 | +import org.elasticsearch.index.mapper.MapperServiceTestCase; |
| 15 | +import org.elasticsearch.index.mapper.ParsedDocument; |
| 16 | +import org.elasticsearch.index.query.ParsedQuery; |
| 17 | +import org.elasticsearch.index.query.SearchExecutionContext; |
| 18 | +import org.elasticsearch.search.SearchHit; |
| 19 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
| 20 | +import org.elasticsearch.search.fetch.subphase.highlight.FastVectorHighlighter; |
| 21 | +import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; |
| 22 | +import org.elasticsearch.search.fetch.subphase.highlight.HighlightPhase; |
| 23 | +import org.elasticsearch.search.fetch.subphase.highlight.Highlighter; |
| 24 | +import org.elasticsearch.search.fetch.subphase.highlight.PlainHighlighter; |
| 25 | +import org.elasticsearch.search.fetch.subphase.highlight.UnifiedHighlighter; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Set; |
| 33 | +import java.util.stream.Collectors; |
| 34 | + |
| 35 | +import static org.mockito.Mockito.mock; |
| 36 | +import static org.mockito.Mockito.when; |
| 37 | + |
| 38 | +public class HighlighterTestCase extends MapperServiceTestCase { |
| 39 | + |
| 40 | + protected Map<String, Highlighter> getHighlighters() { |
| 41 | + Map<String, Highlighter> highlighters = new HashMap<>(); |
| 42 | + highlighters.put("unified", new UnifiedHighlighter()); |
| 43 | + highlighters.put("fvh", new FastVectorHighlighter(getIndexSettings())); |
| 44 | + highlighters.put("plain", new PlainHighlighter()); |
| 45 | + return highlighters; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Runs the highlight phase for a search over a specific document |
| 50 | + * @param mapperService the Mappings to use for highlighting |
| 51 | + * @param doc a parsed document to highlight |
| 52 | + * @param search the search to highlight |
| 53 | + */ |
| 54 | + protected final Map<String, HighlightField> highlight(MapperService mapperService, ParsedDocument doc, SearchSourceBuilder search) |
| 55 | + throws IOException { |
| 56 | + Map<String, HighlightField> highlights = new HashMap<>(); |
| 57 | + withLuceneIndex(mapperService, iw -> iw.addDocument(doc.rootDoc()), ir -> { |
| 58 | + SearchExecutionContext context = createSearchExecutionContext(mapperService, new IndexSearcher(ir)); |
| 59 | + HighlightPhase highlightPhase = new HighlightPhase(getHighlighters()); |
| 60 | + FetchSubPhaseProcessor processor = highlightPhase.getProcessor(fetchContext(context, search)); |
| 61 | + FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext( |
| 62 | + new SearchHit(0, "id", new Text("_doc"), null, null), |
| 63 | + ir.leaves().get(0), |
| 64 | + 0 |
| 65 | + ); |
| 66 | + processor.process(hitContext); |
| 67 | + highlights.putAll(hitContext.hit().getHighlightFields()); |
| 68 | + }); |
| 69 | + return highlights; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Given a set of highlights, assert that any particular field has the expected fragments |
| 74 | + */ |
| 75 | + protected static void assertHighlights(Map<String, HighlightField> highlights, String field, String... fragments) { |
| 76 | + assertNotNull(highlights.get(field)); |
| 77 | + Set<String> actualFragments = Arrays.stream(highlights.get(field).getFragments()).map(Text::toString).collect(Collectors.toSet()); |
| 78 | + Set<String> expectedFragments = new HashSet<>(Arrays.asList(fragments)); |
| 79 | + assertEquals(expectedFragments, actualFragments); |
| 80 | + } |
| 81 | + |
| 82 | + private static FetchContext fetchContext(SearchExecutionContext context, SearchSourceBuilder search) throws IOException { |
| 83 | + FetchContext fetchContext = mock(FetchContext.class); |
| 84 | + when(fetchContext.highlight()).thenReturn(search.highlighter().build(context)); |
| 85 | + when(fetchContext.parsedQuery()).thenReturn(new ParsedQuery(search.query().toQuery(context))); |
| 86 | + when(fetchContext.getSearchExecutionContext()).thenReturn(context); |
| 87 | + return fetchContext; |
| 88 | + } |
| 89 | +} |
0 commit comments