public static Filter newFilter(
      QueryParseContext parseContext, String fieldPattern, String filterName) {
    final FieldMappers fieldNamesMappers =
        parseContext.mapperService().indexName(FieldNamesFieldMapper.NAME);
    final FieldNamesFieldMapper fieldNamesMapper =
        fieldNamesMappers == null ? null : (FieldNamesFieldMapper) fieldNamesMappers.mapper();

    MapperService.SmartNameObjectMapper smartNameObjectMapper =
        parseContext.smartObjectMapper(fieldPattern);
    if (smartNameObjectMapper != null && smartNameObjectMapper.hasMapper()) {
      // automatic make the object mapper pattern
      fieldPattern = fieldPattern + ".*";
    }

    List<String> fields = parseContext.simpleMatchToIndexNames(fieldPattern);
    if (fields.isEmpty()) {
      // no fields exists, so we should not match anything
      return Queries.MATCH_NO_FILTER;
    }
    MapperService.SmartNameFieldMappers nonNullFieldMappers = null;

    XBooleanFilter boolFilter = new XBooleanFilter();
    for (String field : fields) {
      MapperService.SmartNameFieldMappers smartNameFieldMappers =
          parseContext.smartFieldMappers(field);
      if (smartNameFieldMappers != null) {
        nonNullFieldMappers = smartNameFieldMappers;
      }
      Filter filter = null;
      if (fieldNamesMapper != null && fieldNamesMapper.enabled()) {
        final String f;
        if (smartNameFieldMappers != null && smartNameFieldMappers.hasMapper()) {
          f = smartNameFieldMappers.mapper().names().indexName();
        } else {
          f = field;
        }
        filter = fieldNamesMapper.termFilter(f, parseContext);
      }
      // if _field_names are not indexed, we need to go the slow way
      if (filter == null && smartNameFieldMappers != null && smartNameFieldMappers.hasMapper()) {
        filter = smartNameFieldMappers.mapper().rangeFilter(null, null, true, true, parseContext);
      }
      if (filter == null) {
        filter = new TermRangeFilter(field, null, null, true, true);
      }
      boolFilter.add(filter, BooleanClause.Occur.SHOULD);
    }

    // we always cache this one, really does not change... (exists)
    // its ok to cache under the fieldName cacheKey, since its per segment and the mapping applies
    // to this data on this segment...
    Filter filter =
        parseContext.cacheFilter(boolFilter, new CacheKeyFilter.Key("$exists$" + fieldPattern));

    filter = wrapSmartNameFilter(filter, nonNullFieldMappers, parseContext);
    if (filterName != null) {
      parseContext.addNamedFilter(filterName, filter);
    }
    return filter;
  }