Search in sources :

Example 76 with MappedFieldType

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

the class RegexpQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws QueryShardException, IOException {
    MultiTermQuery.RewriteMethod method = QueryParsers.parseRewriteMethod(rewrite, null);
    Query query = null;
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType != null) {
        query = fieldType.regexpQuery(value, flagsValue, maxDeterminizedStates, method, context);
    }
    if (query == null) {
        RegexpQuery regexpQuery = new RegexpQuery(new Term(fieldName, BytesRefs.toBytesRef(value)), flagsValue, maxDeterminizedStates);
        if (method != null) {
            regexpQuery.setRewriteMethod(method);
        }
        query = regexpQuery;
    }
    return query;
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) Query(org.apache.lucene.search.Query) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term) RegexpQuery(org.apache.lucene.search.RegexpQuery)

Example 77 with MappedFieldType

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

the class SimpleQueryParser method newPhraseQuery.

@Override
public Query newPhraseQuery(String text, int slop) {
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.setDisableCoord(true);
    for (Map.Entry<String, Float> entry : weights.entrySet()) {
        try {
            String field = entry.getKey();
            if (settings.quoteFieldSuffix() != null) {
                String quoteField = field + settings.quoteFieldSuffix();
                MappedFieldType quotedFieldType = context.fieldMapper(quoteField);
                if (quotedFieldType != null) {
                    field = quoteField;
                }
            }
            Float boost = entry.getValue();
            Query q = createPhraseQuery(field, text, slop);
            if (q != null) {
                bq.add(wrapWithBoost(q, boost), BooleanClause.Occur.SHOULD);
            }
        } catch (RuntimeException e) {
            rethrowUnlessLenient(e);
        }
    }
    return super.simplify(bq.build());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Map(java.util.Map)

Example 78 with MappedFieldType

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

the class RandomScoreFunctionBuilder method doToFunction.

@Override
protected ScoreFunction doToFunction(QueryShardContext context) {
    final MappedFieldType fieldType = context.getMapperService().fullName("_uid");
    if (fieldType == null) {
        // mapper could be null if we are on a shard with no docs yet, so this won't actually be used
        return new RandomScoreFunction();
    }
    final int salt = (context.index().getName().hashCode() << 10) | context.getShardId();
    final IndexFieldData<?> uidFieldData = context.getForField(fieldType);
    return new RandomScoreFunction(this.seed == null ? hash(context.nowInMillis()) : seed, salt, uidFieldData);
}
Also used : MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) RandomScoreFunction(org.elasticsearch.common.lucene.search.function.RandomScoreFunction)

Example 79 with MappedFieldType

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

the class GeoShapeQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) {
    if (shape == null) {
        throw new UnsupportedOperationException("query must be rewritten first");
    }
    final ShapeBuilder shapeToQuery = shape;
    final MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "failed to find geo_shape field [" + fieldName + "]");
        }
    }
    // TODO: This isn't the nicest way to check this
    if (!(fieldType instanceof GeoShapeFieldMapper.GeoShapeFieldType)) {
        throw new QueryShardException(context, "Field [" + fieldName + "] is not a geo_shape");
    }
    final GeoShapeFieldMapper.GeoShapeFieldType shapeFieldType = (GeoShapeFieldMapper.GeoShapeFieldType) fieldType;
    PrefixTreeStrategy strategy = shapeFieldType.defaultStrategy();
    if (this.strategy != null) {
        strategy = shapeFieldType.resolveStrategy(this.strategy);
    }
    Query query;
    if (strategy instanceof RecursivePrefixTreeStrategy && relation == ShapeRelation.DISJOINT) {
        // this strategy doesn't support disjoint anymore: but it did
        // before, including creating lucene fieldcache (!)
        // in this case, execute disjoint as exists && !intersects
        BooleanQuery.Builder bool = new BooleanQuery.Builder();
        Query exists = ExistsQueryBuilder.newFilter(context, fieldName);
        Query intersects = strategy.makeQuery(getArgs(shapeToQuery, ShapeRelation.INTERSECTS));
        bool.add(exists, BooleanClause.Occur.MUST);
        bool.add(intersects, BooleanClause.Occur.MUST_NOT);
        query = new ConstantScoreQuery(bool.build());
    } else {
        query = new ConstantScoreQuery(strategy.makeQuery(getArgs(shapeToQuery, relation)));
    }
    return query;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) GeoShapeFieldMapper(org.elasticsearch.index.mapper.GeoShapeFieldMapper) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) PrefixTreeStrategy(org.apache.lucene.spatial.prefix.PrefixTreeStrategy) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)

Example 80 with MappedFieldType

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

the class CompletionSuggestionBuilder method build.

@Override
public SuggestionContext build(QueryShardContext context) throws IOException {
    CompletionSuggestionContext suggestionContext = new CompletionSuggestionContext(context);
    // copy over common settings to each suggestion builder
    final MapperService mapperService = context.getMapperService();
    populateCommonFields(mapperService, suggestionContext);
    suggestionContext.setFuzzyOptions(fuzzyOptions);
    suggestionContext.setRegexOptions(regexOptions);
    MappedFieldType mappedFieldType = mapperService.fullName(suggestionContext.getField());
    if (mappedFieldType == null || mappedFieldType instanceof CompletionFieldMapper.CompletionFieldType == false) {
        throw new IllegalArgumentException("Field [" + suggestionContext.getField() + "] is not a completion suggest field");
    }
    if (mappedFieldType instanceof CompletionFieldMapper.CompletionFieldType) {
        CompletionFieldMapper.CompletionFieldType type = (CompletionFieldMapper.CompletionFieldType) mappedFieldType;
        suggestionContext.setFieldType(type);
        if (type.hasContextMappings() && contextBytes != null) {
            try (XContentParser contextParser = XContentFactory.xContent(contextBytes).createParser(context.getXContentRegistry(), contextBytes)) {
                if (type.hasContextMappings() && contextParser != null) {
                    ContextMappings contextMappings = type.getContextMappings();
                    contextParser.nextToken();
                    Map<String, List<ContextMapping.InternalQueryContext>> queryContexts = new HashMap<>(contextMappings.size());
                    assert contextParser.currentToken() == XContentParser.Token.START_OBJECT;
                    XContentParser.Token currentToken;
                    String currentFieldName;
                    while ((currentToken = contextParser.nextToken()) != XContentParser.Token.END_OBJECT) {
                        if (currentToken == XContentParser.Token.FIELD_NAME) {
                            currentFieldName = contextParser.currentName();
                            final ContextMapping mapping = contextMappings.get(currentFieldName);
                            queryContexts.put(currentFieldName, mapping.parseQueryContext(context.newParseContext(contextParser)));
                        }
                    }
                    suggestionContext.setQueryContexts(queryContexts);
                }
            }
        } else if (contextBytes != null) {
            throw new IllegalArgumentException("suggester [" + type.name() + "] doesn't expect any context");
        }
    }
    assert suggestionContext.getFieldType() != null : "no completion field type set";
    return suggestionContext;
}
Also used : HashMap(java.util.HashMap) CompletionFieldMapper(org.elasticsearch.index.mapper.CompletionFieldMapper) ContextMappings(org.elasticsearch.search.suggest.completion.context.ContextMappings) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) List(java.util.List) MapperService(org.elasticsearch.index.mapper.MapperService) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

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