Search in sources :

Example 56 with MappedFieldType

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

the class SpanTermQueryBuilder method doToQuery.

@Override
protected SpanQuery doToQuery(QueryShardContext context) throws IOException {
    MappedFieldType mapper = context.fieldMapper(fieldName);
    Term term;
    if (mapper == null) {
        term = new Term(fieldName, BytesRefs.toBytesRef(value));
    } else {
        Query termQuery = mapper.termQuery(value, context);
        term = MappedFieldType.extractTerm(termQuery);
    }
    return new SpanTermQuery(term);
}
Also used : Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 57 with MappedFieldType

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

the class IndicesService method getFieldStats.

/**
     * Fetch {@linkplain FieldStats} for a field. These stats are cached until the shard changes.
     * @param shard the shard to use with the cache key
     * @param searcher searcher to use to lookup the field stats
     * @param field the actual field
     * @param useCache should this request use the cache?
     */
public FieldStats<?> getFieldStats(IndexShard shard, Engine.Searcher searcher, String field, boolean useCache) throws Exception {
    MappedFieldType fieldType = shard.mapperService().fullName(field);
    if (fieldType == null) {
        return null;
    }
    if (useCache == false) {
        return fieldType.stats(searcher.reader());
    }
    BytesReference cacheKey = new BytesArray("fieldstats:" + field);
    BytesReference statsRef = cacheShardLevelResult(shard, searcher.getDirectoryReader(), cacheKey, out -> {
        try {
            out.writeOptionalWriteable(fieldType.stats(searcher.reader()));
        } catch (IOException e) {
            throw new IllegalStateException("Failed to write field stats output", e);
        }
    });
    try (StreamInput in = statsRef.streamInput()) {
        return in.readOptionalWriteable(FieldStats::readFrom);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) IOException(java.io.IOException) FieldStats(org.elasticsearch.action.fieldstats.FieldStats)

Example 58 with MappedFieldType

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

the class LuceneOrderedDocCollector method nextPageQuery.

@Nullable
@VisibleForTesting
static Query nextPageQuery(FieldDoc lastCollected, OrderBy orderBy, Object[] missingValues, FieldTypeLookup fieldTypeLookup) {
    BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
    for (int i = 0; i < orderBy.orderBySymbols().size(); i++) {
        Symbol order = orderBy.orderBySymbols().get(i);
        Object value = lastCollected.fields[i];
        if (order instanceof Reference) {
            boolean nullsFirst = orderBy.nullsFirst()[i] == null ? false : orderBy.nullsFirst()[i];
            value = value == null || value.equals(missingValues[i]) ? null : value;
            if (nullsFirst && value == null) {
                // no filter needed
                continue;
            }
            String columnName = ((Reference) order).ident().columnIdent().fqn();
            MappedFieldType fieldType = requireNonNull(fieldTypeLookup.get(columnName), "Column must exist: " + columnName);
            Query orderQuery;
            // nulls already gone, so they should be excluded
            if (nullsFirst) {
                BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
                booleanQuery.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
                if (orderBy.reverseFlags()[i]) {
                    booleanQuery.add(fieldType.rangeQuery(null, value, false, true), BooleanClause.Occur.MUST_NOT);
                } else {
                    booleanQuery.add(fieldType.rangeQuery(value, null, true, false), BooleanClause.Occur.MUST_NOT);
                }
                orderQuery = booleanQuery.build();
            } else {
                if (orderBy.reverseFlags()[i]) {
                    orderQuery = fieldType.rangeQuery(value, null, false, false);
                } else {
                    orderQuery = fieldType.rangeQuery(null, value, false, false);
                }
            }
            queryBuilder.add(orderQuery, BooleanClause.Occur.MUST);
        }
    }
    BooleanQuery query = queryBuilder.build();
    if (query.clauses().size() > 0) {
        return query;
    } else {
        return null;
    }
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) Reference(io.crate.metadata.Reference) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable)

Example 59 with MappedFieldType

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

the class MatchQueryBuilder method singleQuery.

private Query singleQuery(MatchQuery.Type type, String fieldName, BytesRef queryString) {
    String field;
    MappedFieldType fieldType = mapperService.smartNameFieldType(fieldName);
    if (fieldType == null) {
        field = fieldName;
    } else {
        field = fieldType.names().indexName();
    }
    if (fieldType != null && fieldType.useTermQueryWithQueryString() && !forceAnalyzeQueryString()) {
        try {
            return fieldType.termQuery(queryString, null);
        } catch (RuntimeException e) {
            return null;
        }
    }
    Analyzer analyzer = getAnalyzer(fieldType);
    InnerQueryBuilder builder = new InnerQueryBuilder(analyzer, fieldType);
    Query query;
    switch(type) {
        case BOOLEAN:
            if (options.commonTermsCutoff() == null) {
                query = builder.createBooleanQuery(field, BytesRefs.toString(queryString), options.operator());
            } else {
                query = builder.createCommonTermsQuery(field, BytesRefs.toString(queryString), options.operator(), options.operator(), options.commonTermsCutoff(), fieldType);
            }
            break;
        case PHRASE:
            query = builder.createPhraseQuery(field, BytesRefs.toString(queryString), options.phraseSlop());
            break;
        case PHRASE_PREFIX:
            query = builder.createPhrasePrefixQuery(field, BytesRefs.toString(queryString), options.phraseSlop(), options.maxExpansions());
            break;
        default:
            throw new IllegalArgumentException("invalid type: " + type.toString());
    }
    if (query == null) {
        return zeroTermsQuery();
    } else {
        return query;
    }
}
Also used : MatchQuery(org.elasticsearch.index.search.MatchQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 60 with MappedFieldType

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

the class BytesRefColumnReferenceTest method testFieldCacheExpression.

@Test
public void testFieldCacheExpression() throws Exception {
    MappedFieldType fieldType = StringFieldMapper.Defaults.FIELD_TYPE.clone();
    fieldType.setNames(new MappedFieldType.Names(column));
    BytesRefColumnReference bytesRefColumn = new BytesRefColumnReference(column, fieldType);
    bytesRefColumn.startCollect(ctx);
    bytesRefColumn.setNextReader(readerContext);
    IndexSearcher searcher = new IndexSearcher(readerContext.reader());
    TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), 20);
    int i = 0;
    StringBuilder builder = new StringBuilder();
    for (ScoreDoc doc : topDocs.scoreDocs) {
        builder.append(i);
        bytesRefColumn.setNextDocId(doc.doc);
        assertThat(bytesRefColumn.value().utf8ToString(), is(builder.toString()));
        i++;
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) BytesRefColumnReference(io.crate.operation.reference.doc.lucene.BytesRefColumnReference) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) Test(org.junit.Test)

Aggregations

MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)122 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)19 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)17 Term (org.apache.lucene.index.Term)17 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)12 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)11 IndexableField (org.apache.lucene.index.IndexableField)9 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)9 ArrayList (java.util.ArrayList)8 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)8 TermQuery (org.apache.lucene.search.TermQuery)8 IndexNumericFieldData (org.elasticsearch.index.fielddata.IndexNumericFieldData)8 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)8 Analyzer (org.apache.lucene.analysis.Analyzer)7