Search in sources :

Example 1 with FieldOptions

use of org.opensearch.search.fetch.subphase.highlight.SearchHighlightContext.FieldOptions in project OpenSearch by opensearch-project.

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.opensearch.search.fetch.subphase.highlight.SearchHighlightContext.FieldOptions)

Example 2 with FieldOptions

use of org.opensearch.search.fetch.subphase.highlight.SearchHighlightContext.FieldOptions in project OpenSearch by opensearch-project.

the class FastVectorHighlighter method fragmentsBuilderSupplier.

private Function<SourceLookup, FragmentsBuilder> fragmentsBuilderSupplier(SearchHighlightContext.Field field, MappedFieldType fieldType, boolean forceSource) {
    BoundaryScanner boundaryScanner = getBoundaryScanner(field);
    FieldOptions options = field.fieldOptions();
    Function<SourceLookup, BaseFragmentsBuilder> supplier;
    if (!forceSource && fieldType.isStored()) {
        if (options.numberOfFragments() != 0 && options.scoreOrdered()) {
            supplier = ignored -> new ScoreOrderFragmentsBuilder(options.preTags(), options.postTags(), boundaryScanner);
        } else {
            supplier = ignored -> new SimpleFragmentsBuilder(fieldType, options.preTags(), options.postTags(), boundaryScanner);
        }
    } else {
        if (options.numberOfFragments() != 0 && options.scoreOrdered()) {
            supplier = lookup -> new SourceScoreOrderFragmentsBuilder(fieldType, lookup, options.preTags(), options.postTags(), boundaryScanner);
        } else {
            supplier = lookup -> new SourceSimpleFragmentsBuilder(fieldType, lookup, options.preTags(), options.postTags(), boundaryScanner);
        }
    }
    return lookup -> {
        BaseFragmentsBuilder builder = supplier.apply(lookup);
        builder.setDiscreteMultiValueHighlighting(termVectorMultiValue);
        return builder;
    };
}
Also used : HashMap(java.util.HashMap) CustomFieldQuery(org.apache.lucene.search.vectorhighlight.CustomFieldQuery) Function(java.util.function.Function) FieldFragList(org.apache.lucene.search.vectorhighlight.FieldFragList) SourceLookup(org.opensearch.search.lookup.SourceLookup) SingleFragListBuilder(org.apache.lucene.search.vectorhighlight.SingleFragListBuilder) SimpleFragListBuilder(org.apache.lucene.search.vectorhighlight.SimpleFragListBuilder) Locale(java.util.Locale) Map(java.util.Map) BoundaryScanner(org.apache.lucene.search.vectorhighlight.BoundaryScanner) BreakIteratorBoundaryScanner(org.apache.lucene.search.vectorhighlight.BreakIteratorBoundaryScanner) SimpleBoundaryScanner(org.apache.lucene.search.vectorhighlight.SimpleBoundaryScanner) FragmentsBuilder(org.apache.lucene.search.vectorhighlight.FragmentsBuilder) FetchSubPhase(org.opensearch.search.fetch.FetchSubPhase) Setting(org.opensearch.common.settings.Setting) CollectionUtils(org.opensearch.common.util.CollectionUtils) ScoreOrderFragmentsBuilder(org.apache.lucene.search.vectorhighlight.ScoreOrderFragmentsBuilder) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) TextSearchInfo(org.opensearch.index.mapper.TextSearchInfo) FieldOptions(org.opensearch.search.fetch.subphase.highlight.SearchHighlightContext.FieldOptions) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) BaseFragmentsBuilder(org.apache.lucene.search.vectorhighlight.BaseFragmentsBuilder) FieldQuery(org.apache.lucene.search.vectorhighlight.FieldQuery) Encoder(org.apache.lucene.search.highlight.Encoder) BreakIterator(java.text.BreakIterator) Field(org.opensearch.search.fetch.subphase.highlight.SearchHighlightContext.Field) FragListBuilder(org.apache.lucene.search.vectorhighlight.FragListBuilder) Collections(java.util.Collections) SimpleFieldFragList(org.apache.lucene.search.vectorhighlight.SimpleFieldFragList) Text(org.opensearch.common.text.Text) SourceLookup(org.opensearch.search.lookup.SourceLookup) BoundaryScanner(org.apache.lucene.search.vectorhighlight.BoundaryScanner) BreakIteratorBoundaryScanner(org.apache.lucene.search.vectorhighlight.BreakIteratorBoundaryScanner) SimpleBoundaryScanner(org.apache.lucene.search.vectorhighlight.SimpleBoundaryScanner) FieldOptions(org.opensearch.search.fetch.subphase.highlight.SearchHighlightContext.FieldOptions) BaseFragmentsBuilder(org.apache.lucene.search.vectorhighlight.BaseFragmentsBuilder) ScoreOrderFragmentsBuilder(org.apache.lucene.search.vectorhighlight.ScoreOrderFragmentsBuilder)

Example 3 with FieldOptions

use of org.opensearch.search.fetch.subphase.highlight.SearchHighlightContext.FieldOptions in project OpenSearch by opensearch-project.

the class HighlightBuilder method build.

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

Example 4 with FieldOptions

use of org.opensearch.search.fetch.subphase.highlight.SearchHighlightContext.FieldOptions in project OpenSearch by opensearch-project.

the class HighlightBuilderTests method testBuildSearchContextHighlight.

/**
 * test that build() outputs a {@link SearchHighlightContext} 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(randomAlphaOfLengthBetween(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, BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry(), namedWriteableRegistry, null, null, System::currentTimeMillis, null, null, () -> true, null) {

        @Override
        public MappedFieldType fieldMapper(String name) {
            TextFieldMapper.Builder builder = new TextFieldMapper.Builder(name, createDefaultIndexAnalyzers());
            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();
        highlightBuilder = Rewriteable.rewrite(highlightBuilder, mockShardContext);
        SearchHighlightContext highlight = highlightBuilder.build(mockShardContext);
        for (SearchHighlightContext.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 = Rewriteable.rewrite(fieldBuilder.highlightQuery, mockShardContext).toQuery(mockShardContext);
            } else if (highlightBuilder.highlightQuery != null) {
                expectedValue = Rewriteable.rewrite(highlightBuilder.highlightQuery, mockShardContext).toQuery(mockShardContext);
            }
            assertEquals(expectedValue, fieldOptions.highlightQuery());
        }
    }
}
Also used : Query(org.apache.lucene.search.Query) IndexSettings(org.opensearch.index.IndexSettings) QueryBuilder(org.opensearch.index.query.QueryBuilder) IdsQueryBuilder(org.opensearch.index.query.IdsQueryBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) FieldOptions(org.opensearch.search.fetch.subphase.highlight.SearchHighlightContext.FieldOptions) Index(org.opensearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) ContentPath(org.opensearch.index.mapper.ContentPath) Field(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field) Function(java.util.function.Function) TreeSet(java.util.TreeSet) QueryShardContext(org.opensearch.index.query.QueryShardContext) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) TextFieldMapper(org.opensearch.index.mapper.TextFieldMapper)

Aggregations

FieldOptions (org.opensearch.search.fetch.subphase.highlight.SearchHighlightContext.FieldOptions)4 Locale (java.util.Locale)2 Function (java.util.function.Function)2 BreakIteratorBoundaryScanner (org.apache.lucene.search.vectorhighlight.BreakIteratorBoundaryScanner)2 SimpleBoundaryScanner (org.apache.lucene.search.vectorhighlight.SimpleBoundaryScanner)2 Settings (org.opensearch.common.settings.Settings)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 QueryBuilder (org.opensearch.index.query.QueryBuilder)2 IOException (java.io.IOException)1 BreakIterator (java.text.BreakIterator)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 Query (org.apache.lucene.search.Query)1 Encoder (org.apache.lucene.search.highlight.Encoder)1 BaseFragmentsBuilder (org.apache.lucene.search.vectorhighlight.BaseFragmentsBuilder)1 BoundaryScanner (org.apache.lucene.search.vectorhighlight.BoundaryScanner)1