Search in sources :

Example 31 with MappedFieldType

use of org.elasticsearch.index.mapper.MappedFieldType 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 32 with MappedFieldType

use of org.elasticsearch.index.mapper.MappedFieldType 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 33 with MappedFieldType

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

the class SimpleQueryParserTests method testQuoteFieldSuffix.

public void testQuoteFieldSuffix() {
    SimpleQueryParser.Settings sqpSettings = new SimpleQueryParser.Settings();
    sqpSettings.quoteFieldSuffix(".quote");
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_INDEX_UUID, "some_uuid").put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build();
    IndexMetaData indexState = IndexMetaData.builder("index").settings(indexSettings).build();
    IndexSettings settings = new IndexSettings(indexState, Settings.EMPTY);
    QueryShardContext mockShardContext = new QueryShardContext(0, settings, null, null, null, null, null, xContentRegistry(), null, null, System::currentTimeMillis) {

        @Override
        public MappedFieldType fieldMapper(String name) {
            return new MockFieldMapper.FakeFieldType();
        }
    };
    SimpleQueryParser parser = new SimpleQueryParser(new StandardAnalyzer(), Collections.singletonMap("foo", 1f), -1, sqpSettings, mockShardContext);
    assertEquals(new TermQuery(new Term("foo", "bar")), parser.parse("bar"));
    assertEquals(new TermQuery(new Term("foo.quote", "bar")), parser.parse("\"bar\""));
    // Now check what happens if foo.quote does not exist
    mockShardContext = new QueryShardContext(0, settings, null, null, null, null, null, xContentRegistry(), null, null, System::currentTimeMillis) {

        @Override
        public MappedFieldType fieldMapper(String name) {
            if (name.equals("foo.quote")) {
                return null;
            }
            return new MockFieldMapper.FakeFieldType();
        }
    };
    parser = new SimpleQueryParser(new StandardAnalyzer(), Collections.singletonMap("foo", 1f), -1, sqpSettings, mockShardContext);
    assertEquals(new TermQuery(new Term("foo", "bar")), parser.parse("bar"));
    assertEquals(new TermQuery(new Term("foo", "bar")), parser.parse("\"bar\""));
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) IndexSettings(org.elasticsearch.index.IndexSettings) MockFieldMapper(org.elasticsearch.index.mapper.MockFieldMapper) Term(org.apache.lucene.index.Term) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 34 with MappedFieldType

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

the class SpanTermQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(SpanTermQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    assertThat(query, instanceOf(SpanTermQuery.class));
    SpanTermQuery spanTermQuery = (SpanTermQuery) query;
    assertThat(spanTermQuery.getTerm().field(), equalTo(queryBuilder.fieldName()));
    MappedFieldType mapper = context.getQueryShardContext().fieldMapper(queryBuilder.fieldName());
    if (mapper != null) {
        Term term = ((TermQuery) mapper.termQuery(queryBuilder.value(), null)).getTerm();
        assertThat(spanTermQuery.getTerm(), equalTo(term));
    } else {
        assertThat(spanTermQuery.getTerm().bytes(), equalTo(BytesRefs.toBytesRef(queryBuilder.value())));
    }
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 35 with MappedFieldType

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

the class SimilarityTests method testResolveSimilaritiesFromMapping_DFI.

public void testResolveSimilaritiesFromMapping_DFI() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field1").field("type", "text").field("similarity", "my_similarity").endObject().endObject().endObject().endObject().string();
    Settings indexSettings = Settings.builder().put("index.similarity.my_similarity.type", "DFI").put("index.similarity.my_similarity.independence_measure", "chisquared").build();
    IndexService indexService = createIndex("foo", indexSettings);
    DocumentMapper documentMapper = indexService.mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    MappedFieldType fieldType = documentMapper.mappers().getMapper("field1").fieldType();
    assertThat(fieldType.similarity(), instanceOf(DFISimilarityProvider.class));
    DFISimilarity similarity = (DFISimilarity) fieldType.similarity().get();
    assertThat(similarity.getIndependence(), instanceOf(IndependenceChiSquared.class));
}
Also used : IndexService(org.elasticsearch.index.IndexService) DFISimilarity(org.apache.lucene.search.similarities.DFISimilarity) IndependenceChiSquared(org.apache.lucene.search.similarities.IndependenceChiSquared) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)130 IndexSearcher (org.apache.lucene.search.IndexSearcher)34 IndexReader (org.apache.lucene.index.IndexReader)33 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)29 Directory (org.apache.lucene.store.Directory)29 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)26 Document (org.apache.lucene.document.Document)23 Query (org.apache.lucene.search.Query)21 Term (org.apache.lucene.index.Term)18 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)17 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)12 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)11 IndexableField (org.apache.lucene.index.IndexableField)9 TermQuery (org.apache.lucene.search.TermQuery)9 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)9 ArrayList (java.util.ArrayList)8 Analyzer (org.apache.lucene.analysis.Analyzer)8 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)8 IndexNumericFieldData (org.elasticsearch.index.fielddata.IndexNumericFieldData)8 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)8