Search in sources :

Example 26 with MappedFieldType

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

the class IndexFieldDataServiceTests method testFieldDataCacheListener.

public void testFieldDataCacheListener() throws Exception {
    final IndexService indexService = createIndex("test");
    final IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    // copy the ifdService since we can set the listener only once.
    final IndexFieldDataService ifdService = new IndexFieldDataService(indexService.getIndexSettings(), indicesService.getIndicesFieldDataCache(), indicesService.getCircuitBreakerService(), indexService.mapperService());
    final BuilderContext ctx = new BuilderContext(indexService.getIndexSettings().getSettings(), new ContentPath(1));
    final MappedFieldType mapper1 = new TextFieldMapper.Builder("s").fielddata(true).build(ctx).fieldType();
    final IndexWriter writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(new KeywordAnalyzer()));
    Document doc = new Document();
    doc.add(new StringField("s", "thisisastring", Store.NO));
    writer.addDocument(doc);
    DirectoryReader open = DirectoryReader.open(writer);
    final boolean wrap = randomBoolean();
    final IndexReader reader = wrap ? ElasticsearchDirectoryReader.wrap(open, new ShardId("test", "_na_", 1)) : open;
    final AtomicInteger onCacheCalled = new AtomicInteger();
    final AtomicInteger onRemovalCalled = new AtomicInteger();
    ifdService.setListener(new IndexFieldDataCache.Listener() {

        @Override
        public void onCache(ShardId shardId, String fieldName, Accountable ramUsage) {
            if (wrap) {
                assertEquals(new ShardId("test", "_na_", 1), shardId);
            } else {
                assertNull(shardId);
            }
            onCacheCalled.incrementAndGet();
        }

        @Override
        public void onRemoval(ShardId shardId, String fieldName, boolean wasEvicted, long sizeInBytes) {
            if (wrap) {
                assertEquals(new ShardId("test", "_na_", 1), shardId);
            } else {
                assertNull(shardId);
            }
            onRemovalCalled.incrementAndGet();
        }
    });
    IndexFieldData<?> ifd = ifdService.getForField(mapper1);
    LeafReaderContext leafReaderContext = reader.getContext().leaves().get(0);
    AtomicFieldData load = ifd.load(leafReaderContext);
    assertEquals(1, onCacheCalled.get());
    assertEquals(0, onRemovalCalled.get());
    reader.close();
    load.close();
    writer.close();
    assertEquals(1, onCacheCalled.get());
    assertEquals(1, onRemovalCalled.get());
    ifdService.clear();
}
Also used : IndexService(org.elasticsearch.index.IndexService) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.apache.lucene.document.Document) ShardId(org.elasticsearch.index.shard.ShardId) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) Accountable(org.apache.lucene.util.Accountable) IndicesService(org.elasticsearch.indices.IndicesService) ContentPath(org.elasticsearch.index.mapper.ContentPath) RAMDirectory(org.apache.lucene.store.RAMDirectory) IndexWriter(org.apache.lucene.index.IndexWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 27 with MappedFieldType

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

the class AbstractFieldDataTestCase method getForField.

public <IFD extends IndexFieldData<?>> IFD getForField(String type, String fieldName, boolean docValues) {
    final MappedFieldType fieldType;
    final BuilderContext context = new BuilderContext(indexService.getIndexSettings().getSettings(), new ContentPath(1));
    if (type.equals("string")) {
        if (docValues) {
            fieldType = new KeywordFieldMapper.Builder(fieldName).build(context).fieldType();
        } else {
            fieldType = new TextFieldMapper.Builder(fieldName).fielddata(true).build(context).fieldType();
        }
    } else if (type.equals("float")) {
        fieldType = new NumberFieldMapper.Builder(fieldName, NumberFieldMapper.NumberType.FLOAT).docValues(docValues).build(context).fieldType();
    } else if (type.equals("double")) {
        fieldType = new NumberFieldMapper.Builder(fieldName, NumberFieldMapper.NumberType.DOUBLE).docValues(docValues).build(context).fieldType();
    } else if (type.equals("long")) {
        fieldType = new NumberFieldMapper.Builder(fieldName, NumberFieldMapper.NumberType.LONG).docValues(docValues).build(context).fieldType();
    } else if (type.equals("int")) {
        fieldType = new NumberFieldMapper.Builder(fieldName, NumberFieldMapper.NumberType.INTEGER).docValues(docValues).build(context).fieldType();
    } else if (type.equals("short")) {
        fieldType = new NumberFieldMapper.Builder(fieldName, NumberFieldMapper.NumberType.SHORT).docValues(docValues).build(context).fieldType();
    } else if (type.equals("byte")) {
        fieldType = new NumberFieldMapper.Builder(fieldName, NumberFieldMapper.NumberType.BYTE).docValues(docValues).build(context).fieldType();
    } else if (type.equals("geo_point")) {
        fieldType = new GeoPointFieldMapper.Builder(fieldName).docValues(docValues).build(context).fieldType();
    } else if (type.equals("_parent")) {
        fieldType = new ParentFieldMapper.Builder("_type").type(fieldName).build(context).fieldType();
    } else if (type.equals("binary")) {
        fieldType = new BinaryFieldMapper.Builder(fieldName).docValues(docValues).build(context).fieldType();
    } else {
        throw new UnsupportedOperationException(type);
    }
    return ifdService.getForField(fieldType);
}
Also used : NumberFieldMapper(org.elasticsearch.index.mapper.NumberFieldMapper) GeoPointFieldMapper(org.elasticsearch.index.mapper.GeoPointFieldMapper) ContentPath(org.elasticsearch.index.mapper.ContentPath) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext) BinaryFieldMapper(org.elasticsearch.index.mapper.BinaryFieldMapper)

Example 28 with MappedFieldType

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

the class CompletionFieldTypeTests method setupProperties.

@Before
public void setupProperties() {
    addModifier(new Modifier("preserve_separators", false) {

        @Override
        public void modify(MappedFieldType ft) {
            CompletionFieldMapper.CompletionFieldType cft = (CompletionFieldMapper.CompletionFieldType) ft;
            cft.setPreserveSep(false);
        }
    });
    addModifier(new Modifier("preserve_position_increments", false) {

        @Override
        public void modify(MappedFieldType ft) {
            CompletionFieldMapper.CompletionFieldType cft = (CompletionFieldMapper.CompletionFieldType) ft;
            cft.setPreservePositionIncrements(false);
        }
    });
    addModifier(new Modifier("context_mappings", false) {

        @Override
        public void modify(MappedFieldType ft) {
            CompletionFieldMapper.CompletionFieldType cft = (CompletionFieldMapper.CompletionFieldType) ft;
            ContextMappings contextMappings = new ContextMappings(Arrays.asList(ContextBuilder.category("foo").build(), ContextBuilder.geo("geo").build()));
            cft.setContextMappings(contextMappings);
        }
    });
}
Also used : CompletionFieldMapper(org.elasticsearch.index.mapper.CompletionFieldMapper) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) ContextMappings(org.elasticsearch.search.suggest.completion.context.ContextMappings) Before(org.junit.Before)

Example 29 with MappedFieldType

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

the class TextFieldTypeTests method testFuzzyQuery.

public void testFuzzyQuery() {
    MappedFieldType ft = createDefaultFieldType();
    ft.setName("field");
    ft.setIndexOptions(IndexOptions.DOCS);
    assertEquals(new FuzzyQuery(new Term("field", "foo"), 2, 1, 50, true), ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true));
    ft.setIndexOptions(IndexOptions.NONE);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true));
    assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}
Also used : FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 30 with MappedFieldType

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

the class MatchQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(MatchQueryBuilder queryBuilder, Query query, SearchContext searchContext) throws IOException {
    assertThat(query, notNullValue());
    if (query instanceof MatchAllDocsQuery) {
        assertThat(queryBuilder.zeroTermsQuery(), equalTo(ZeroTermsQuery.ALL));
        return;
    }
    switch(queryBuilder.type()) {
        case BOOLEAN:
            assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(ExtendedCommonTermsQuery.class)).or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class)).or(instanceOf(MatchNoDocsQuery.class)).or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
            break;
        case PHRASE:
            assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(PhraseQuery.class)).or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class)).or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
            break;
        case PHRASE_PREFIX:
            assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(MultiPhrasePrefixQuery.class)).or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class)).or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
            break;
    }
    QueryShardContext context = searchContext.getQueryShardContext();
    MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName());
    if (query instanceof TermQuery && fieldType != null) {
        String queryValue = queryBuilder.value().toString();
        if (queryBuilder.analyzer() == null || queryBuilder.analyzer().equals("simple")) {
            queryValue = queryValue.toLowerCase(Locale.ROOT);
        }
        Query expectedTermQuery = fieldType.termQuery(queryValue, context);
        assertEquals(expectedTermQuery, query);
    }
    if (query instanceof BooleanQuery) {
        BooleanQuery bq = (BooleanQuery) query;
        if (queryBuilder.minimumShouldMatch() != null) {
            // calculate expected minimumShouldMatch value
            int optionalClauses = 0;
            for (BooleanClause c : bq.clauses()) {
                if (c.getOccur() == BooleanClause.Occur.SHOULD) {
                    optionalClauses++;
                }
            }
            int msm = Queries.calculateMinShouldMatch(optionalClauses, queryBuilder.minimumShouldMatch());
            assertThat(bq.getMinimumNumberShouldMatch(), equalTo(msm));
        }
        if (queryBuilder.analyzer() == null && queryBuilder.value().toString().length() > 0) {
            assertEquals(bq.clauses().size(), queryBuilder.value().toString().split(" ").length);
        }
    }
    if (query instanceof ExtendedCommonTermsQuery) {
        assertTrue(queryBuilder.cutoffFrequency() != null);
        ExtendedCommonTermsQuery ectq = (ExtendedCommonTermsQuery) query;
        assertEquals(queryBuilder.cutoffFrequency(), ectq.getMaxTermFrequency(), Float.MIN_VALUE);
    }
    if (query instanceof FuzzyQuery) {
        assertTrue(queryBuilder.fuzziness() != null);
        FuzzyQuery fuzzyQuery = (FuzzyQuery) query;
        // depending on analyzer being set or not we can have term lowercased along the way, so to simplify test we just
        // compare lowercased terms here
        String originalTermLc = queryBuilder.value().toString().toLowerCase(Locale.ROOT);
        String actualTermLc = fuzzyQuery.getTerm().text().toLowerCase(Locale.ROOT);
        Matcher<String> termLcMatcher = equalTo(originalTermLc);
        if ("false".equals(originalTermLc) || "true".equals(originalTermLc)) {
            // Booleans become t/f when querying a boolean field
            termLcMatcher = either(termLcMatcher).or(equalTo(originalTermLc.substring(0, 1)));
        }
        assertThat(actualTermLc, termLcMatcher);
        assertThat(queryBuilder.prefixLength(), equalTo(fuzzyQuery.getPrefixLength()));
        assertThat(queryBuilder.fuzzyTranspositions(), equalTo(fuzzyQuery.getTranspositions()));
    }
    if (query instanceof PointRangeQuery) {
    // TODO
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) Query(org.apache.lucene.search.Query) ZeroTermsQuery(org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MatchQuery(org.elasticsearch.index.search.MatchQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Matchers.containsString(org.hamcrest.Matchers.containsString) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BooleanClause(org.apache.lucene.search.BooleanClause) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType)

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