Search in sources :

Example 6 with MapperService

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

the class MultiFieldTests method testMultiFieldWithDot.

public void testMultiFieldWithDot() throws IOException {
    XContentBuilder mapping = jsonBuilder();
    mapping.startObject().startObject("my_type").startObject("properties").startObject("city").field("type", "text").startObject("fields").startObject("raw.foo").field("type", "text").field("index", "not_analyzed").endObject().endObject().endObject().endObject().endObject().endObject();
    MapperService mapperService = createIndex("test").mapperService();
    try {
        mapperService.documentMapperParser().parse("my_type", new CompressedXContent(mapping.string()));
        fail("this should throw an exception because one field contains a dot");
    } catch (MapperParsingException e) {
        assertThat(e.getMessage(), equalTo("Field name [raw.foo] which is a multi field of [city] cannot contain '.'"));
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 7 with MapperService

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

the class JavaMultiFieldMergeTests method testUpgradeFromMultiFieldTypeToMultiFields.

public void testUpgradeFromMultiFieldTypeToMultiFields() throws Exception {
    String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping1.json");
    MapperService mapperService = createIndex("test").mapperService();
    DocumentMapper docMapper = mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), nullValue());
    BytesReference json = XContentFactory.jsonBuilder().startObject().field("name", "some name").endObject().bytes();
    Document doc = docMapper.parse("test", "person", "1", json).rootDoc();
    IndexableField f = doc.getField("name");
    assertThat(f, notNullValue());
    f = doc.getField("name.indexed");
    assertThat(f, nullValue());
    mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/upgrade1.json");
    docMapper = mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed2"), nullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed3"), nullValue());
    doc = docMapper.parse("test", "person", "1", json).rootDoc();
    f = doc.getField("name");
    assertThat(f, notNullValue());
    f = doc.getField("name.indexed");
    assertThat(f, notNullValue());
    mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/upgrade2.json");
    docMapper = mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed2"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed3"), nullValue());
    mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/upgrade3.json");
    try {
        mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("mapper [name] has different [index] values"));
        assertThat(e.getMessage(), containsString("mapper [name] has different [store] values"));
    }
    // There are conflicts, so the `name.not_indexed3` has not been added
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed2"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed3"), nullValue());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexableField(org.apache.lucene.index.IndexableField) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.elasticsearch.index.mapper.ParseContext.Document) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 8 with MapperService

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

the class RangeQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    if (getCurrentTypes().length == 0 || (queryBuilder.fieldName().equals(DATE_FIELD_NAME) == false && queryBuilder.fieldName().equals(INT_FIELD_NAME) == false && queryBuilder.fieldName().equals(DATE_RANGE_FIELD_NAME) == false && queryBuilder.fieldName().equals(INT_RANGE_FIELD_NAME) == false)) {
        assertThat(query, instanceOf(TermRangeQuery.class));
        TermRangeQuery termRangeQuery = (TermRangeQuery) query;
        assertThat(termRangeQuery.getField(), equalTo(queryBuilder.fieldName()));
        assertThat(termRangeQuery.getLowerTerm(), equalTo(BytesRefs.toBytesRef(queryBuilder.from())));
        assertThat(termRangeQuery.getUpperTerm(), equalTo(BytesRefs.toBytesRef(queryBuilder.to())));
        assertThat(termRangeQuery.includesLower(), equalTo(queryBuilder.includeLower()));
        assertThat(termRangeQuery.includesUpper(), equalTo(queryBuilder.includeUpper()));
    } else if (queryBuilder.fieldName().equals(DATE_FIELD_NAME)) {
        assertThat(query, instanceOf(IndexOrDocValuesQuery.class));
        query = ((IndexOrDocValuesQuery) query).getIndexQuery();
        assertThat(query, instanceOf(PointRangeQuery.class));
        MapperService mapperService = context.getQueryShardContext().getMapperService();
        MappedFieldType mappedFieldType = mapperService.fullName(DATE_FIELD_NAME);
        final Long fromInMillis;
        final Long toInMillis;
        // we have to normalize the incoming value into milliseconds since it could be literally anything
        if (mappedFieldType instanceof DateFieldMapper.DateFieldType) {
            fromInMillis = queryBuilder.from() == null ? null : ((DateFieldMapper.DateFieldType) mappedFieldType).parseToMilliseconds(queryBuilder.from(), queryBuilder.includeLower(), queryBuilder.getDateTimeZone(), queryBuilder.getForceDateParser(), context.getQueryShardContext());
            toInMillis = queryBuilder.to() == null ? null : ((DateFieldMapper.DateFieldType) mappedFieldType).parseToMilliseconds(queryBuilder.to(), queryBuilder.includeUpper(), queryBuilder.getDateTimeZone(), queryBuilder.getForceDateParser(), context.getQueryShardContext());
        } else {
            fromInMillis = toInMillis = null;
            fail("unexpected mapped field type: [" + mappedFieldType.getClass() + "] " + mappedFieldType.toString());
        }
        Long min = fromInMillis;
        Long max = toInMillis;
        long minLong, maxLong;
        if (min == null) {
            minLong = Long.MIN_VALUE;
        } else {
            minLong = min.longValue();
            if (queryBuilder.includeLower() == false && minLong != Long.MAX_VALUE) {
                minLong++;
            }
        }
        if (max == null) {
            maxLong = Long.MAX_VALUE;
        } else {
            maxLong = max.longValue();
            if (queryBuilder.includeUpper() == false && maxLong != Long.MIN_VALUE) {
                maxLong--;
            }
        }
        assertEquals(LongPoint.newRangeQuery(DATE_FIELD_NAME, minLong, maxLong), query);
    } else if (queryBuilder.fieldName().equals(INT_FIELD_NAME)) {
        assertThat(query, instanceOf(IndexOrDocValuesQuery.class));
        query = ((IndexOrDocValuesQuery) query).getIndexQuery();
        assertThat(query, instanceOf(PointRangeQuery.class));
        Integer min = (Integer) queryBuilder.from();
        Integer max = (Integer) queryBuilder.to();
        int minInt, maxInt;
        if (min == null) {
            minInt = Integer.MIN_VALUE;
        } else {
            minInt = min.intValue();
            if (queryBuilder.includeLower() == false && minInt != Integer.MAX_VALUE) {
                minInt++;
            }
        }
        if (max == null) {
            maxInt = Integer.MAX_VALUE;
        } else {
            maxInt = max.intValue();
            if (queryBuilder.includeUpper() == false && maxInt != Integer.MIN_VALUE) {
                maxInt--;
            }
        }
    } else if (queryBuilder.fieldName().equals(DATE_RANGE_FIELD_NAME) || queryBuilder.fieldName().equals(INT_RANGE_FIELD_NAME)) {
    // todo can't check RangeFieldQuery because its currently package private (this will change)
    } else {
        throw new UnsupportedOperationException();
    }
}
Also used : DateFieldMapper(org.elasticsearch.index.mapper.DateFieldMapper) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) MapperService(org.elasticsearch.index.mapper.MapperService) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint)

Example 9 with MapperService

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

the class QueryShardContextTests method testFailIfFieldMappingNotFound.

public void testFailIfFieldMappingNotFound() {
    IndexMetaData.Builder indexMetadata = new IndexMetaData.Builder("index");
    indexMetadata.settings(Settings.builder().put("index.version.created", Version.CURRENT).put("index.number_of_shards", 1).put("index.number_of_replicas", 1));
    IndexSettings indexSettings = new IndexSettings(indexMetadata.build(), Settings.EMPTY);
    MapperService mapperService = mock(MapperService.class);
    when(mapperService.getIndexSettings()).thenReturn(indexSettings);
    final long nowInMillis = randomNonNegativeLong();
    QueryShardContext context = new QueryShardContext(0, indexSettings, null, null, mapperService, null, null, xContentRegistry(), null, null, () -> nowInMillis);
    context.setAllowUnmappedFields(false);
    MappedFieldType fieldType = new TextFieldMapper.TextFieldType();
    MappedFieldType result = context.failIfFieldMappingNotFound("name", fieldType);
    assertThat(result, sameInstance(fieldType));
    QueryShardException e = expectThrows(QueryShardException.class, () -> context.failIfFieldMappingNotFound("name", null));
    assertEquals("No field mapping can be found for the field with name [name]", e.getMessage());
    context.setAllowUnmappedFields(true);
    result = context.failIfFieldMappingNotFound("name", fieldType);
    assertThat(result, sameInstance(fieldType));
    result = context.failIfFieldMappingNotFound("name", null);
    assertThat(result, nullValue());
    context.setAllowUnmappedFields(false);
    context.setMapUnmappedFieldAsString(true);
    result = context.failIfFieldMappingNotFound("name", fieldType);
    assertThat(result, sameInstance(fieldType));
    result = context.failIfFieldMappingNotFound("name", null);
    assertThat(result, notNullValue());
    assertThat(result, instanceOf(TextFieldMapper.TextFieldType.class));
    assertThat(result.name(), equalTo("name"));
}
Also used : IndexSettings(org.elasticsearch.index.IndexSettings) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) MapperService(org.elasticsearch.index.mapper.MapperService) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 10 with MapperService

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

the class MultiMatchQueryTests method setup.

@Before
public void setup() throws IOException {
    Settings settings = Settings.builder().put("index.analysis.filter.syns.type", "synonym").putArray("index.analysis.filter.syns.synonyms", "quick,fast").put("index.analysis.analyzer.syns.tokenizer", "standard").put("index.analysis.analyzer.syns.filter", "syns").build();
    IndexService indexService = createIndex("test", settings);
    MapperService mapperService = indexService.mapperService();
    String mapping = "{\n" + "    \"person\":{\n" + "        \"properties\":{\n" + "            \"name\":{\n" + "                  \"properties\":{\n" + "                        \"first\": {\n" + "                            \"type\":\"text\",\n" + "                            \"analyzer\":\"syns\"\n" + "                        }," + "                        \"last\": {\n" + "                            \"type\":\"text\",\n" + "                            \"analyzer\":\"syns\"\n" + "                        }" + "                   }" + "            }\n" + "        }\n" + "    }\n" + "}";
    mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    this.indexService = indexService;
}
Also used : IndexService(org.elasticsearch.index.IndexService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Settings(org.elasticsearch.common.settings.Settings) MapperService(org.elasticsearch.index.mapper.MapperService) Before(org.junit.Before)

Aggregations

MapperService (org.elasticsearch.index.mapper.MapperService)46 Settings (org.elasticsearch.common.settings.Settings)16 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)14 IndexSettings (org.elasticsearch.index.IndexSettings)13 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)12 IOException (java.io.IOException)10 Store (org.elasticsearch.index.store.Store)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)8 Index (org.elasticsearch.index.Index)8 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 IndexService (org.elasticsearch.index.IndexService)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 IndexAnalyzers (org.elasticsearch.index.analysis.IndexAnalyzers)6 Collections (java.util.Collections)5 HashSet (java.util.HashSet)5 List (java.util.List)5 Analyzer (org.apache.lucene.analysis.Analyzer)5