Search in sources :

Example 1 with FieldOptions

use of org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight.FieldOptions in project elasticsearch by elastic.

the class FastVectorHighlighter method getBoundaryScanner.

private static BoundaryScanner getBoundaryScanner(Field field) {
    final FieldOptions fieldOptions = field.fieldOptions();
    final Locale boundaryScannerLocale = fieldOptions.boundaryScannerLocale() != null ? fieldOptions.boundaryScannerLocale() : Locale.ROOT;
    final HighlightBuilder.BoundaryScannerType type = fieldOptions.boundaryScannerType() != null ? fieldOptions.boundaryScannerType() : HighlightBuilder.BoundaryScannerType.CHARS;
    switch(type) {
        case SENTENCE:
            if (boundaryScannerLocale != null) {
                return new BreakIteratorBoundaryScanner(BreakIterator.getSentenceInstance(boundaryScannerLocale));
            }
            return DEFAULT_SENTENCE_BOUNDARY_SCANNER;
        case WORD:
            if (boundaryScannerLocale != null) {
                return new BreakIteratorBoundaryScanner(BreakIterator.getWordInstance(boundaryScannerLocale));
            }
            return DEFAULT_WORD_BOUNDARY_SCANNER;
        case CHARS:
            if (fieldOptions.boundaryMaxScan() != SimpleBoundaryScanner.DEFAULT_MAX_SCAN || fieldOptions.boundaryChars() != SimpleBoundaryScanner.DEFAULT_BOUNDARY_CHARS) {
                return new SimpleBoundaryScanner(fieldOptions.boundaryMaxScan(), fieldOptions.boundaryChars());
            }
            return DEFAULT_SIMPLE_BOUNDARY_SCANNER;
        default:
            throw new IllegalArgumentException("Invalid boundary scanner type: " + type.toString());
    }
}
Also used : Locale(java.util.Locale) SimpleBoundaryScanner(org.apache.lucene.search.vectorhighlight.SimpleBoundaryScanner) BreakIteratorBoundaryScanner(org.apache.lucene.search.vectorhighlight.BreakIteratorBoundaryScanner) FieldOptions(org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight.FieldOptions)

Example 2 with FieldOptions

use of org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight.FieldOptions in project elasticsearch by elastic.

the class HighlightBuilder method build.

public SearchContextHighlight build(QueryShardContext context) throws IOException {
    // create template global options that are later merged with any partial field options
    final SearchContextHighlight.FieldOptions.Builder globalOptionsBuilder = new SearchContextHighlight.FieldOptions.Builder();
    globalOptionsBuilder.encoder(this.encoder);
    transferOptions(this, globalOptionsBuilder, context);
    // overwrite unset global options by default values
    globalOptionsBuilder.merge(defaultOptions);
    // create field options
    Collection<org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight.Field> fieldOptions = new ArrayList<>();
    for (Field field : this.fields) {
        final SearchContextHighlight.FieldOptions.Builder fieldOptionsBuilder = new SearchContextHighlight.FieldOptions.Builder();
        fieldOptionsBuilder.fragmentOffset(field.fragmentOffset);
        if (field.matchedFields != null) {
            Set<String> matchedFields = new HashSet<String>(field.matchedFields.length);
            Collections.addAll(matchedFields, field.matchedFields);
            fieldOptionsBuilder.matchedFields(matchedFields);
        }
        transferOptions(field, fieldOptionsBuilder, context);
        fieldOptions.add(new SearchContextHighlight.Field(field.name(), fieldOptionsBuilder.merge(globalOptionsBuilder.build()).build()));
    }
    return new SearchContextHighlight(fieldOptions);
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) ArrayList(java.util.ArrayList) FieldOptions(org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight.FieldOptions) ParseField(org.elasticsearch.common.ParseField) HashSet(java.util.HashSet)

Example 3 with FieldOptions

use of org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight.FieldOptions in project elasticsearch by elastic.

the class HighlightBuilderTests method testBuildSearchContextHighlight.

/**
     * test that build() outputs a {@link SearchContextHighlight} that is has similar parameters
     * than what we have in the random {@link HighlightBuilder}
     */
public void testBuildSearchContextHighlight() throws IOException {
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build();
    Index index = new Index(randomAsciiOfLengthBetween(1, 10), "_na_");
    IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(index, indexSettings);
    // shard context will only need indicesQueriesRegistry for building Query objects nested in highlighter
    QueryShardContext mockShardContext = new QueryShardContext(0, idxSettings, null, null, null, null, null, xContentRegistry(), null, null, System::currentTimeMillis) {

        @Override
        public MappedFieldType fieldMapper(String name) {
            TextFieldMapper.Builder builder = new TextFieldMapper.Builder(name);
            return builder.build(new Mapper.BuilderContext(idxSettings.getSettings(), new ContentPath(1))).fieldType();
        }
    };
    mockShardContext.setMapUnmappedFieldAsString(true);
    for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
        HighlightBuilder highlightBuilder = randomHighlighterBuilder();
        SearchContextHighlight highlight = highlightBuilder.build(mockShardContext);
        for (SearchContextHighlight.Field field : highlight.fields()) {
            String encoder = highlightBuilder.encoder() != null ? highlightBuilder.encoder() : HighlightBuilder.DEFAULT_ENCODER;
            assertEquals(encoder, field.fieldOptions().encoder());
            final Field fieldBuilder = getFieldBuilderByName(highlightBuilder, field.field());
            assertNotNull("expected a highlight builder for field " + field.field(), fieldBuilder);
            FieldOptions fieldOptions = field.fieldOptions();
            BiConsumer<Function<AbstractHighlighterBuilder<?>, Object>, Function<FieldOptions, Object>> checkSame = mergeBeforeChek(highlightBuilder, fieldBuilder, fieldOptions);
            checkSame.accept(AbstractHighlighterBuilder::boundaryChars, FieldOptions::boundaryChars);
            checkSame.accept(AbstractHighlighterBuilder::boundaryScannerType, FieldOptions::boundaryScannerType);
            checkSame.accept(AbstractHighlighterBuilder::boundaryMaxScan, FieldOptions::boundaryMaxScan);
            checkSame.accept(AbstractHighlighterBuilder::fragmentSize, FieldOptions::fragmentCharSize);
            checkSame.accept(AbstractHighlighterBuilder::fragmenter, FieldOptions::fragmenter);
            checkSame.accept(AbstractHighlighterBuilder::requireFieldMatch, FieldOptions::requireFieldMatch);
            checkSame.accept(AbstractHighlighterBuilder::noMatchSize, FieldOptions::noMatchSize);
            checkSame.accept(AbstractHighlighterBuilder::numOfFragments, FieldOptions::numberOfFragments);
            checkSame.accept(AbstractHighlighterBuilder::phraseLimit, FieldOptions::phraseLimit);
            checkSame.accept(AbstractHighlighterBuilder::highlighterType, FieldOptions::highlighterType);
            checkSame.accept(AbstractHighlighterBuilder::highlightFilter, FieldOptions::highlightFilter);
            checkSame.accept(AbstractHighlighterBuilder::preTags, FieldOptions::preTags);
            checkSame.accept(AbstractHighlighterBuilder::postTags, FieldOptions::postTags);
            checkSame.accept(AbstractHighlighterBuilder::options, FieldOptions::options);
            checkSame.accept(AbstractHighlighterBuilder::order, op -> op.scoreOrdered() ? Order.SCORE : Order.NONE);
            assertEquals(fieldBuilder.fragmentOffset, fieldOptions.fragmentOffset());
            if (fieldBuilder.matchedFields != null) {
                String[] copy = Arrays.copyOf(fieldBuilder.matchedFields, fieldBuilder.matchedFields.length);
                Arrays.sort(copy);
                assertArrayEquals(copy, new TreeSet<>(fieldOptions.matchedFields()).toArray(new String[fieldOptions.matchedFields().size()]));
            } else {
                assertNull(fieldOptions.matchedFields());
            }
            Query expectedValue = null;
            if (fieldBuilder.highlightQuery != null) {
                expectedValue = QueryBuilder.rewriteQuery(fieldBuilder.highlightQuery, mockShardContext).toQuery(mockShardContext);
            } else if (highlightBuilder.highlightQuery != null) {
                expectedValue = QueryBuilder.rewriteQuery(highlightBuilder.highlightQuery, mockShardContext).toQuery(mockShardContext);
            }
            assertEquals(expectedValue, fieldOptions.highlightQuery());
        }
    }
}
Also used : Query(org.apache.lucene.search.Query) IndexSettings(org.elasticsearch.index.IndexSettings) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) FieldOptions(org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight.FieldOptions) Index(org.elasticsearch.index.Index) ContentPath(org.elasticsearch.index.mapper.ContentPath) Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) Function(java.util.function.Function) TreeSet(java.util.TreeSet) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper)

Aggregations

FieldOptions (org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight.FieldOptions)3 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Locale (java.util.Locale)1 TreeSet (java.util.TreeSet)1 Function (java.util.function.Function)1 Query (org.apache.lucene.search.Query)1 BreakIteratorBoundaryScanner (org.apache.lucene.search.vectorhighlight.BreakIteratorBoundaryScanner)1 SimpleBoundaryScanner (org.apache.lucene.search.vectorhighlight.SimpleBoundaryScanner)1 ParseField (org.elasticsearch.common.ParseField)1 Settings (org.elasticsearch.common.settings.Settings)1 Index (org.elasticsearch.index.Index)1 IndexSettings (org.elasticsearch.index.IndexSettings)1 ContentPath (org.elasticsearch.index.mapper.ContentPath)1 TextFieldMapper (org.elasticsearch.index.mapper.TextFieldMapper)1 IdsQueryBuilder (org.elasticsearch.index.query.IdsQueryBuilder)1 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)1 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)1