Search in sources :

Example 6 with ContentPath

use of org.elasticsearch.index.mapper.ContentPath in project crate by crate.

the class TransportShardUpsertActionTest method testValidateMapping.

@Test
public void testValidateMapping() throws Exception {
    /**
         * create a mapping which contains an invalid column name
         * {
         *      "valid": {},
         *      "_invalid": {}
         * }
         */
    expectedException.expect(IllegalArgumentException.class);
    expectedException.expectMessage("Column name must not start with '_'");
    Mapper.BuilderContext builderContext = new Mapper.BuilderContext(null, new ContentPath());
    Mapper.Builder validInnerMapper = new ObjectMapper.Builder("valid");
    Mapper.Builder invalidInnerMapper = new ObjectMapper.Builder("_invalid");
    Mapper outerMapper = new ObjectMapper.Builder("outer").add(validInnerMapper).add(invalidInnerMapper).build(builderContext);
    TransportShardUpsertAction.validateMapping(Arrays.asList(outerMapper).iterator());
}
Also used : ObjectMapper(org.elasticsearch.index.mapper.object.ObjectMapper) Mapper(org.elasticsearch.index.mapper.Mapper) MapBuilder(org.elasticsearch.common.collect.MapBuilder) ContentPath(org.elasticsearch.index.mapper.ContentPath) ObjectMapper(org.elasticsearch.index.mapper.object.ObjectMapper) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 7 with ContentPath

use of org.elasticsearch.index.mapper.ContentPath in project elasticsearch by elastic.

the class AbstractSortTestCase method createMockShardContext.

protected QueryShardContext createMockShardContext() {
    Index index = new Index(randomAsciiOfLengthBetween(1, 10), "_na_");
    IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(index, Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build());
    IndicesFieldDataCache cache = new IndicesFieldDataCache(Settings.EMPTY, null);
    IndexFieldDataService ifds = new IndexFieldDataService(IndexSettingsModule.newIndexSettings("test", Settings.EMPTY), cache, null, null);
    BitsetFilterCache bitsetFilterCache = new BitsetFilterCache(idxSettings, new BitsetFilterCache.Listener() {

        @Override
        public void onRemoval(ShardId shardId, Accountable accountable) {
        }

        @Override
        public void onCache(ShardId shardId, Accountable accountable) {
        }
    });
    long nowInMillis = randomNonNegativeLong();
    return new QueryShardContext(0, idxSettings, bitsetFilterCache, ifds, null, null, scriptService, xContentRegistry(), null, null, () -> nowInMillis) {

        @Override
        public MappedFieldType fieldMapper(String name) {
            return provideMappedFieldType(name);
        }

        @Override
        public ObjectMapper getObjectMapper(String name) {
            BuilderContext context = new BuilderContext(this.getIndexSettings().getSettings(), new ContentPath());
            return new ObjectMapper.Builder<>(name).nested(Nested.newNested(false, false)).build(context);
        }
    };
}
Also used : 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) Accountable(org.apache.lucene.util.Accountable) Index(org.elasticsearch.index.Index) ContentPath(org.elasticsearch.index.mapper.ContentPath) ShardId(org.elasticsearch.index.shard.ShardId) IndicesFieldDataCache(org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache) IndexFieldDataService(org.elasticsearch.index.fielddata.IndexFieldDataService) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext) BitsetFilterCache(org.elasticsearch.index.cache.bitset.BitsetFilterCache)

Example 8 with ContentPath

use of org.elasticsearch.index.mapper.ContentPath 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)

Example 9 with ContentPath

use of org.elasticsearch.index.mapper.ContentPath in project elasticsearch by elastic.

the class FilterFieldDataTests method testFilterByFrequency.

public void testFilterByFrequency() throws Exception {
    Random random = random();
    for (int i = 0; i < 1000; i++) {
        Document d = new Document();
        d.add(new StringField("id", "" + i, Field.Store.NO));
        if (i % 100 == 0) {
            d.add(new StringField("high_freq", "100", Field.Store.NO));
            d.add(new StringField("low_freq", "100", Field.Store.NO));
            d.add(new StringField("med_freq", "100", Field.Store.NO));
        }
        if (i % 10 == 0) {
            d.add(new StringField("high_freq", "10", Field.Store.NO));
            d.add(new StringField("med_freq", "10", Field.Store.NO));
        }
        if (i % 5 == 0) {
            d.add(new StringField("high_freq", "5", Field.Store.NO));
        }
        writer.addDocument(d);
    }
    writer.forceMerge(1, true);
    List<LeafReaderContext> contexts = refreshReader();
    final BuilderContext builderCtx = new BuilderContext(indexService.getIndexSettings().getSettings(), new ContentPath(1));
    {
        ifdService.clear();
        MappedFieldType ft = new TextFieldMapper.Builder("high_freq").fielddata(true).fielddataFrequencyFilter(0, random.nextBoolean() ? 100 : 0.5d, 0).build(builderCtx).fieldType();
        IndexOrdinalsFieldData fieldData = ifdService.getForField(ft);
        for (LeafReaderContext context : contexts) {
            AtomicOrdinalsFieldData loadDirect = fieldData.loadDirect(context);
            RandomAccessOrds bytesValues = loadDirect.getOrdinalsValues();
            assertThat(2L, equalTo(bytesValues.getValueCount()));
            assertThat(bytesValues.lookupOrd(0).utf8ToString(), equalTo("10"));
            assertThat(bytesValues.lookupOrd(1).utf8ToString(), equalTo("100"));
        }
    }
    {
        ifdService.clear();
        MappedFieldType ft = new TextFieldMapper.Builder("high_freq").fielddata(true).fielddataFrequencyFilter(random.nextBoolean() ? 101 : 101d / 200.0d, 201, 100).build(builderCtx).fieldType();
        IndexOrdinalsFieldData fieldData = ifdService.getForField(ft);
        for (LeafReaderContext context : contexts) {
            AtomicOrdinalsFieldData loadDirect = fieldData.loadDirect(context);
            RandomAccessOrds bytesValues = loadDirect.getOrdinalsValues();
            assertThat(1L, equalTo(bytesValues.getValueCount()));
            assertThat(bytesValues.lookupOrd(0).utf8ToString(), equalTo("5"));
        }
    }
    {
        // test # docs with value
        ifdService.clear();
        MappedFieldType ft = new TextFieldMapper.Builder("med_freq").fielddata(true).fielddataFrequencyFilter(random.nextBoolean() ? 101 : 101d / 200.0d, Integer.MAX_VALUE, 101).build(builderCtx).fieldType();
        IndexOrdinalsFieldData fieldData = ifdService.getForField(ft);
        for (LeafReaderContext context : contexts) {
            AtomicOrdinalsFieldData loadDirect = fieldData.loadDirect(context);
            RandomAccessOrds bytesValues = loadDirect.getOrdinalsValues();
            assertThat(2L, equalTo(bytesValues.getValueCount()));
            assertThat(bytesValues.lookupOrd(0).utf8ToString(), equalTo("10"));
            assertThat(bytesValues.lookupOrd(1).utf8ToString(), equalTo("100"));
        }
    }
    {
        ifdService.clear();
        MappedFieldType ft = new TextFieldMapper.Builder("med_freq").fielddata(true).fielddataFrequencyFilter(random.nextBoolean() ? 101 : 101d / 200.0d, Integer.MAX_VALUE, 101).build(builderCtx).fieldType();
        IndexOrdinalsFieldData fieldData = ifdService.getForField(ft);
        for (LeafReaderContext context : contexts) {
            AtomicOrdinalsFieldData loadDirect = fieldData.loadDirect(context);
            RandomAccessOrds bytesValues = loadDirect.getOrdinalsValues();
            assertThat(2L, equalTo(bytesValues.getValueCount()));
            assertThat(bytesValues.lookupOrd(0).utf8ToString(), equalTo("10"));
            assertThat(bytesValues.lookupOrd(1).utf8ToString(), equalTo("100"));
        }
    }
}
Also used : RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds) ContentPath(org.elasticsearch.index.mapper.ContentPath) Document(org.apache.lucene.document.Document) Random(java.util.Random) StringField(org.apache.lucene.document.StringField) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper)

Aggregations

ContentPath (org.elasticsearch.index.mapper.ContentPath)9 BuilderContext (org.elasticsearch.index.mapper.Mapper.BuilderContext)6 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)4 TextFieldMapper (org.elasticsearch.index.mapper.TextFieldMapper)4 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 IndexSettings (org.elasticsearch.index.IndexSettings)3 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)3 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)3 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)3 Document (org.apache.lucene.document.Document)2 StringField (org.apache.lucene.document.StringField)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)2 Query (org.apache.lucene.search.Query)2 Accountable (org.apache.lucene.util.Accountable)2 Settings (org.elasticsearch.common.settings.Settings)2 Index (org.elasticsearch.index.Index)2 IndexService (org.elasticsearch.index.IndexService)2 NumberFieldMapper (org.elasticsearch.index.mapper.NumberFieldMapper)2 IdsQueryBuilder (org.elasticsearch.index.query.IdsQueryBuilder)2 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)2