Query parseGroup(Type type, String field, Float boostValue, Object value, String minimumShouldMatch) throws IOException { if (context.fieldMapper(field) == null) { return null; // indicates to the caller that this field is unmapped and should be disregarded } return parseAndApply(type, field, value, minimumShouldMatch, boostValue); }
private static Query newLegacyExistsQuery(QueryShardContext context, String field) { MappedFieldType fieldType = context.fieldMapper(field); String fieldName = fieldType != null ? fieldType.name() : field; return new TermQuery(new Term(FieldNamesFieldMapper.NAME, fieldName)); }
private String queryFieldName(QueryShardContext context, String fieldName) { MappedFieldType fieldType = context.fieldMapper(fieldName); return fieldType != null ? fieldType.name() : fieldName; }
@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; }
/** * Visible only for testing purposes. */ List<Query> createTermQueries(QueryShardContext context) { final MappedFieldType fieldType = context.fieldMapper(fieldName); final List<Query> queries = new ArrayList<>(values.size()); for (Object value : values) { if (fieldType != null) { queries.add(fieldType.termQuery(value, context)); } else { queries.add(new TermQuery(new Term(fieldName, BytesRefs.toBytesRef(value)))); } } return queries; }
@Override protected SpanQuery doToQuery(QueryShardContext context) throws IOException { String fieldInQuery = fieldName; MappedFieldType fieldType = context.fieldMapper(fieldName); if (fieldType != null) { fieldInQuery = fieldType.name(); } Query innerQuery = queryBuilder.toQuery(context); assert innerQuery instanceof SpanQuery; return new FieldMaskingSpanQuery((SpanQuery)innerQuery, fieldInQuery); }
@Override protected Query doToQuery(QueryShardContext context) throws IOException { MappedFieldType fieldType = context.fieldMapper(fieldName); if (fieldType == null) { return new MatchNoDocsQuery("unknown field [" + fieldName + "]"); } MultiTermQuery.RewriteMethod method = QueryParsers.parseRewriteMethod( rewrite, null, LoggingDeprecationHandler.INSTANCE); return fieldType.wildcardQuery(value, method, context); }
@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); }
@Override protected Query doToQuery(QueryShardContext context) throws IOException { MultiTermQuery.RewriteMethod method = QueryParsers.parseRewriteMethod(rewrite, null, LoggingDeprecationHandler.INSTANCE); Query query = null; MappedFieldType fieldType = context.fieldMapper(fieldName); if (fieldType != null) { query = fieldType.prefixQuery(value, method, context); } if (query == null) { PrefixQuery prefixQuery = new PrefixQuery(new Term(fieldName, BytesRefs.toBytesRef(value))); if (method != null) { prefixQuery.setRewriteMethod(method); } query = prefixQuery; } return query; }
private static void handleExclude(BooleanQuery.Builder boolQuery, Item[] likeItems, QueryShardContext context) { MappedFieldType uidField = context.fieldMapper(UidFieldMapper.NAME); if (uidField == null) { // no mappings, nothing to exclude return; } // artificial docs get assigned a random id and should be disregarded List<BytesRef> uids = new ArrayList<>(); for (Item item : likeItems) { if (item.doc() != null) { continue; } uids.add(createUidAsBytes(item.type(), item.id())); } if (!uids.isEmpty()) { Query query = uidField.termsQuery(uids, context); boolQuery.add(query, BooleanClause.Occur.MUST_NOT); } }
@Override protected Query doToQuery(QueryShardContext context) throws IOException { Query query; MappedFieldType uidField = context.fieldMapper(UidFieldMapper.NAME); if (uidField == null) { return new MatchNoDocsQuery("No mappings"); } if (this.ids.isEmpty()) { query = Queries.newMatchNoDocsQuery("Missing ids in \"" + this.getName() + "\" query."); } else { Collection<String> typesForQuery; if (types.length == 0) { typesForQuery = context.queryTypes(); } else if (types.length == 1 && MetaData.ALL.equals(types[0])) { typesForQuery = context.getMapperService().types(); } else { typesForQuery = new HashSet<>(); Collections.addAll(typesForQuery, types); } query = uidField.termsQuery(Arrays.asList(Uid.createUidsForTypesAndIds(typesForQuery, ids)), context); } return query; }
private LongValuesSource createValuesSource(QueryShardContext context) { LongValuesSource longValuesSource; if (minimumShouldMatchField != null) { MappedFieldType msmFieldType = context.fieldMapper(minimumShouldMatchField); if (msmFieldType == null) { throw new QueryShardException(context, "failed to find minimum_should_match field [" + minimumShouldMatchField + "]"); } IndexNumericFieldData fieldData = context.getForField(msmFieldType); longValuesSource = new FieldValuesSource(fieldData); } else if (minimumShouldMatchScript != null) { TermsSetQueryScript.Factory factory = context.getScriptService().compile(minimumShouldMatchScript, TermsSetQueryScript.CONTEXT); Map<String, Object> params = new HashMap<>(); params.putAll(minimumShouldMatchScript.getParams()); params.put("num_terms", values.size()); longValuesSource = new ScriptLongValueSource(minimumShouldMatchScript, factory.newFactory(params, context.lookup())); } else { throw new IllegalStateException("No minimum should match has been specified"); } return longValuesSource; }
@Override protected Query newTermQuery(Term term) { MappedFieldType ft = context.fieldMapper(term.field()); if (ft == null) { return newUnmappedFieldQuery(term.field()); } return ft.termQuery(term.bytes(), context); }
@Override protected Query doToQuery(QueryShardContext context) throws IOException { Query query = null; String rewrite = this.rewrite; if (rewrite == null && context.isFilter()) { rewrite = QueryParsers.CONSTANT_SCORE.getPreferredName(); } MappedFieldType fieldType = context.fieldMapper(fieldName); if (fieldType != null) { query = fieldType.fuzzyQuery(value, fuzziness, prefixLength, maxExpansions, transpositions); } if (query == null) { int maxEdits = fuzziness.asDistance(BytesRefs.toString(value)); query = new FuzzyQuery(new Term(fieldName, BytesRefs.toBytesRef(value)), maxEdits, prefixLength, maxExpansions, transpositions); } if (query instanceof MultiTermQuery) { MultiTermQuery.RewriteMethod rewriteMethod = QueryParsers.parseRewriteMethod(rewrite, null, LoggingDeprecationHandler.INSTANCE); QueryParsers.setRewriteMethod((MultiTermQuery) query, rewriteMethod); } return query; }
private Query getRangeQuerySingle(String field, String part1, String part2, boolean startInclusive, boolean endInclusive, QueryShardContext context) { currentFieldType = context.fieldMapper(field); if (currentFieldType == null) { return newUnmappedFieldQuery(field); } try { Analyzer normalizer = forceAnalyzer == null ? queryBuilder.context.getSearchAnalyzer(currentFieldType) : forceAnalyzer; BytesRef part1Binary = part1 == null ? null : normalizer.normalize(field, part1); BytesRef part2Binary = part2 == null ? null : normalizer.normalize(field, part2); Query rangeQuery = currentFieldType.rangeQuery(part1Binary, part2Binary, startInclusive, endInclusive, null, timeZone, null, context); return rangeQuery; } catch (RuntimeException e) { if (lenient) { return newLenientFieldQuery(field, e); } throw e; } }
private Query getWildcardQuerySingle(String field, String termStr) throws ParseException { if ("*".equals(termStr)) { // effectively, we check if a field exists or not return existsQuery(field); } String indexedNameField = field; currentFieldType = null; Analyzer oldAnalyzer = getAnalyzer(); try { currentFieldType = queryBuilder.context.fieldMapper(field); if (currentFieldType != null) { setAnalyzer(forceAnalyzer == null ? queryBuilder.context.getSearchAnalyzer(currentFieldType) : forceAnalyzer); indexedNameField = currentFieldType.name(); } return super.getWildcardQuery(indexedNameField, termStr); } catch (RuntimeException e) { if (lenient) { return newLenientFieldQuery(field, e); } throw e; } finally { setAnalyzer(oldAnalyzer); } }
private Query getFuzzyQuerySingle(String field, String termStr, float minSimilarity) throws ParseException { currentFieldType = context.fieldMapper(field); if (currentFieldType == null) { return newUnmappedFieldQuery(field); } try { Analyzer normalizer = forceAnalyzer == null ? queryBuilder.context.getSearchAnalyzer(currentFieldType) : forceAnalyzer; BytesRef term = termStr == null ? null : normalizer.normalize(field, termStr); return currentFieldType.fuzzyQuery(term, Fuzziness.fromEdits((int) minSimilarity), getFuzzyPrefixLength(), fuzzyMaxExpansions, fuzzyTranspositions); } catch (RuntimeException e) { if (lenient) { return newLenientFieldQuery(field, e); } throw e; } }
private Query getRegexpQuerySingle(String field, String termStr) throws ParseException { currentFieldType = null; Analyzer oldAnalyzer = getAnalyzer(); try { currentFieldType = queryBuilder.context.fieldMapper(field); if (currentFieldType == null) { return newUnmappedFieldQuery(field); } setAnalyzer(forceAnalyzer == null ? queryBuilder.context.getSearchAnalyzer(currentFieldType) : forceAnalyzer); Query query = super.getRegexpQuery(field, termStr); return query; } catch (RuntimeException e) { if (lenient) { return newLenientFieldQuery(field, e); } throw e; } finally { setAnalyzer(oldAnalyzer); } }
private AbstractDistanceScoreFunction parseVariable(String fieldName, XContentParser parser, QueryShardContext context, MultiValueMode mode) throws IOException { //the field must exist, else we cannot read the value for the doc later MappedFieldType fieldType = context.fieldMapper(fieldName); if (fieldType == null) { throw new ParsingException(parser.getTokenLocation(), "unknown field [{}]", fieldName); } // dates and time and geo need special handling parser.nextToken(); if (fieldType instanceof DateFieldMapper.DateFieldType) { return parseDateVariable(parser, context, fieldType, mode); } else if (fieldType instanceof GeoPointFieldType) { return parseGeoVariable(parser, context, fieldType, mode); } else if (fieldType instanceof NumberFieldMapper.NumberFieldType) { return parseNumberVariable(parser, context, fieldType, mode); } else { throw new ParsingException(parser.getTokenLocation(), "field [{}] is of type [{}], but only numeric types are supported.", fieldName, fieldType); } }
private Query getPrefixQuerySingle(String field, String termStr) throws ParseException { Analyzer oldAnalyzer = getAnalyzer(); try { currentFieldType = context.fieldMapper(field); if (currentFieldType == null) { return newUnmappedFieldQuery(field); } setAnalyzer(forceAnalyzer == null ? queryBuilder.context.getSearchAnalyzer(currentFieldType) : forceAnalyzer); Query query = null; if (currentFieldType instanceof StringFieldType == false) { query = currentFieldType.prefixQuery(termStr, getMultiTermRewriteMethod(), context); } else { query = getPossiblyAnalyzedPrefixQuery(currentFieldType.name(), termStr); } return query; } catch (RuntimeException e) { if (lenient) { return newLenientFieldQuery(field, e); } throw e; } finally { setAnalyzer(oldAnalyzer); } }