public void testExplicitEnabled() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("_field_names")
            .field("enabled", true)
            .endObject()
            .endObject()
            .endObject()
            .string();
    DocumentMapper docMapper =
        createIndex("test").mapperService().documentMapperParser().parse(mapping);
    FieldNamesFieldMapper fieldNamesMapper = docMapper.rootMapper(FieldNamesFieldMapper.class);
    assertTrue(fieldNamesMapper.enabled());

    ParsedDocument doc =
        docMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("field", "value")
                .endObject()
                .bytes());

    assertFieldNames(set("field", "_uid", "_type", "_version", "_source", "_all"), doc);
  }
  public void testDisablingBackcompat() throws Exception {
    // before 1.5, disabling happened by setting index:no
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("_field_names")
            .field("index", "no")
            .endObject()
            .endObject()
            .endObject()
            .string();

    Settings indexSettings =
        ImmutableSettings.builder()
            .put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id)
            .build();
    DocumentMapper docMapper =
        createIndex("test", indexSettings).mapperService().documentMapperParser().parse(mapping);
    FieldNamesFieldMapper fieldNamesMapper = docMapper.rootMapper(FieldNamesFieldMapper.class);
    assertFalse(fieldNamesMapper.enabled());

    ParsedDocument doc =
        docMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("field", "value")
                .endObject()
                .bytes());

    assertNull(doc.rootDoc().get("_field_names"));
  }
  public void testDisabled() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("_field_names")
            .field("enabled", false)
            .endObject()
            .endObject()
            .endObject()
            .string();
    DocumentMapper docMapper =
        createIndex("test").mapperService().documentMapperParser().parse(mapping);
    FieldNamesFieldMapper fieldNamesMapper = docMapper.rootMapper(FieldNamesFieldMapper.class);
    assertFalse(fieldNamesMapper.enabled());

    ParsedDocument doc =
        docMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("field", "value")
                .endObject()
                .bytes());

    assertNull(doc.rootDoc().get("_field_names"));
  }
  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;
  }
 private static SortedSet<String> extract(String path) {
   SortedSet<String> set = new TreeSet<>();
   for (String fieldName : FieldNamesFieldMapper.extractFieldNames(path)) {
     set.add(fieldName);
   }
   return set;
 }
  public void testFieldTypeSettingsBackcompat() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("_field_names")
            .field("store", "yes")
            .endObject()
            .endObject()
            .endObject()
            .string();

    Settings indexSettings =
        ImmutableSettings.builder()
            .put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id)
            .build();
    DocumentMapper docMapper =
        createIndex("test", indexSettings).mapperService().documentMapperParser().parse(mapping);
    FieldNamesFieldMapper fieldNamesMapper = docMapper.rootMapper(FieldNamesFieldMapper.class);
    assertTrue(fieldNamesMapper.fieldType().stored());
  }