Search in sources :

Example 71 with MappedFieldType

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

the class TermQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query query = null;
    MappedFieldType mapper = context.fieldMapper(this.fieldName);
    if (mapper != null) {
        query = mapper.termQuery(this.value, context);
    }
    if (query == null) {
        query = new TermQuery(new Term(this.fieldName, BytesRefs.toBytesRef(this.value)));
    }
    return query;
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 72 with MappedFieldType

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

the class WildcardQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    Term term;
    if (fieldType == null) {
        term = new Term(fieldName, BytesRefs.toBytesRef(value));
    } else {
        Query termQuery = fieldType.termQuery(value, context);
        term = MappedFieldType.extractTerm(termQuery);
    }
    WildcardQuery query = new WildcardQuery(term);
    MultiTermQuery.RewriteMethod rewriteMethod = QueryParsers.parseRewriteMethod(rewrite, null);
    QueryParsers.setRewriteMethod(query, rewriteMethod);
    return query;
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(org.apache.lucene.search.Query) WildcardQuery(org.apache.lucene.search.WildcardQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 73 with MappedFieldType

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

the class MatchQuery method parse.

public Query parse(Type type, String fieldName, Object value) throws IOException {
    final String field;
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType != null) {
        field = fieldType.name();
    } else {
        field = fieldName;
    }
    /*
         * If the user forced an analyzer we really don't care if they are
         * searching a type that wants term queries to be used with query string
         * because the QueryBuilder will take care of it. If they haven't forced
         * an analyzer then types like NumberFieldType that want terms with
         * query string will blow up because their analyzer isn't capable of
         * passing through QueryBuilder.
         */
    boolean noForcedAnalyzer = this.analyzer == null;
    if (fieldType != null && fieldType.tokenized() == false && noForcedAnalyzer) {
        return blendTermQuery(new Term(fieldName, value.toString()), fieldType);
    }
    Analyzer analyzer = getAnalyzer(fieldType);
    assert analyzer != null;
    MatchQueryBuilder builder = new MatchQueryBuilder(analyzer, fieldType);
    builder.setEnablePositionIncrements(this.enablePositionIncrements);
    Query query = null;
    switch(type) {
        case BOOLEAN:
            if (commonTermsCutoff == null) {
                query = builder.createBooleanQuery(field, value.toString(), occur);
            } else {
                query = builder.createCommonTermsQuery(field, value.toString(), occur, occur, commonTermsCutoff, fieldType);
            }
            break;
        case PHRASE:
            query = builder.createPhraseQuery(field, value.toString(), phraseSlop);
            break;
        case PHRASE_PREFIX:
            query = builder.createPhrasePrefixQuery(field, value.toString(), phraseSlop, maxExpansions);
            break;
        default:
            throw new IllegalStateException("No type found for [" + type + "]");
    }
    if (query == null) {
        return zeroTermsQuery();
    } else {
        return query;
    }
}
Also used : Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 74 with MappedFieldType

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

the class IndexShard method prepareIndex.

static Engine.Index prepareIndex(DocumentMapperForType docMapper, SourceToParse source, long seqNo, long primaryTerm, long version, VersionType versionType, Engine.Operation.Origin origin, long autoGeneratedIdTimestamp, boolean isRetry) {
    long startTime = System.nanoTime();
    ParsedDocument doc = docMapper.getDocumentMapper().parse(source);
    if (docMapper.getMapping() != null) {
        doc.addDynamicMappingsUpdate(docMapper.getMapping());
    }
    MappedFieldType uidFieldType = docMapper.getDocumentMapper().uidMapper().fieldType();
    Query uidQuery = uidFieldType.termQuery(doc.uid(), null);
    Term uid = MappedFieldType.extractTerm(uidQuery);
    return new Engine.Index(uid, doc, seqNo, primaryTerm, version, versionType, origin, startTime, autoGeneratedIdTimestamp, isRetry);
}
Also used : ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Query(org.apache.lucene.search.Query) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) CheckIndex(org.apache.lucene.index.CheckIndex) Index(org.elasticsearch.index.Index) Term(org.apache.lucene.index.Term)

Example 75 with MappedFieldType

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

the class RangeQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query query = null;
    MappedFieldType mapper = context.fieldMapper(this.fieldName);
    if (mapper != null) {
        if (mapper instanceof DateFieldMapper.DateFieldType) {
            query = ((DateFieldMapper.DateFieldType) mapper).rangeQuery(from, to, includeLower, includeUpper, timeZone, getForceDateParser(), context);
        } else if (mapper instanceof RangeFieldMapper.RangeFieldType && mapper.typeName() == RangeFieldMapper.RangeType.DATE.name) {
            DateMathParser forcedDateParser = null;
            if (this.format != null) {
                forcedDateParser = new DateMathParser(this.format);
            }
            query = ((RangeFieldMapper.RangeFieldType) mapper).rangeQuery(from, to, includeLower, includeUpper, relation, timeZone, forcedDateParser, context);
        } else {
            if (timeZone != null) {
                throw new QueryShardException(context, "[range] time_zone can not be applied to non date field [" + fieldName + "]");
            }
            //LUCENE 4 UPGRADE Mapper#rangeQuery should use bytesref as well?
            query = mapper.rangeQuery(from, to, includeLower, includeUpper, context);
        }
    } else {
        if (timeZone != null) {
            throw new QueryShardException(context, "[range] time_zone can not be applied to non unmapped field [" + fieldName + "]");
        }
    }
    if (query == null) {
        query = new TermRangeQuery(this.fieldName, BytesRefs.toBytesRef(from), BytesRefs.toBytesRef(to), includeLower, includeUpper);
    }
    return query;
}
Also used : DateFieldMapper(org.elasticsearch.index.mapper.DateFieldMapper) Query(org.apache.lucene.search.Query) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) DateMathParser(org.elasticsearch.common.joda.DateMathParser)

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