private static void assertMappingsHaveField( GetMappingsResponse mappings, String index, String type, String field) throws IOException { ImmutableOpenMap<String, MappingMetaData> indexMappings = mappings.getMappings().get("index"); assertNotNull(indexMappings); MappingMetaData typeMappings = indexMappings.get(type); assertNotNull(typeMappings); Map<String, Object> typeMappingsMap = typeMappings.getSourceAsMap(); Map<String, Object> properties = (Map<String, Object>) typeMappingsMap.get("properties"); assertTrue( "Could not find [" + field + "] in " + typeMappingsMap.toString(), properties.containsKey(field)); }
/** * Until we can verify that all instances have moved over to our new mapping, we need to handcheck * all index mappings to make sure they have doc.associations.assoc_index as a doc value * * @param communityIdStrs * @return */ @SuppressWarnings("unchecked") private static boolean validateAssociationMapping(String[] communityIdStrs) { // get all index mappings associated with these commids String[] mappings = new String[communityIdStrs.length]; StringBuilder sb = new StringBuilder(", "); for (int i = 0; i < communityIdStrs.length; i++) { String s = communityIdStrs[i]; mappings[i] = "doc_" + s + "*"; sb.append("doc_").append(s).append("*, "); } ElasticSearchManager esm = ElasticSearchManager.getIndex(sb.substring(2, sb.length())); GetMappingsResponse response = esm.getRawClient().admin().indices().prepareGetMappings(mappings).get(); for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> mapping : response.getMappings()) { ImmutableOpenMap<String, MappingMetaData> mappingVal = mapping.value; MappingMetaData mapping_meta = mappingVal.get("document_index"); try { Map<String, Object> map = mapping_meta.getSourceAsMap(); Map<String, Object> props = (Map<String, Object>) map.get("properties"); Map<String, Object> assocs = (Map<String, Object>) props.get(DocumentPojo.associations_); Map<String, Object> assocs_props = (Map<String, Object>) assocs.get("properties"); Map<String, Object> assoc_index = (Map<String, Object>) assocs_props.get(AssociationPojo.assoc_index_); if (!assoc_index.containsKey("doc_values") || !((Boolean) assoc_index.get("doc_values"))) { // doc values doesn't exist in mapping or was false return false; } } catch (Exception ex) { // failed somehow return false; } } // if we fell through, all the checked indexes had the doc_value field set return true; }
@SuppressWarnings("unchecked") @Test public void updateIncludeExclude() throws Exception { assertAcked( prepareCreate("test") .addMapping( "type", jsonBuilder() .startObject() .startObject("type") .startObject("properties") .startObject("normal") .field("type", "long") .endObject() .startObject("exclude") .field("type", "long") .endObject() .startObject("include") .field("type", "long") .endObject() .endObject() .endObject() .endObject())); ensureGreen(); // make sure that replicas are initialized so the refresh command will work them // too logger.info("Index doc"); index( "test", "type", "1", JsonXContent.contentBuilder() .startObject() .field("normal", 1) .field("exclude", 1) .field("include", 1) .endObject()); refresh(); // commit it for later testing. logger.info("Adding exclude settings"); PutMappingResponse putResponse = client() .admin() .indices() .preparePutMapping("test") .setType("type") .setSource( JsonXContent.contentBuilder() .startObject() .startObject("type") .startObject("_source") .startArray("excludes") .value("exclude") .endArray() .endObject() .endObject()) .get(); assertTrue(putResponse.isAcknowledged()); // changed mapping doesn't affect indexed documents (checking backward compatibility) GetResponse getResponse = client().prepareGet("test", "type", "1").setRealtime(false).get(); assertThat(getResponse.getSource(), hasKey("normal")); assertThat(getResponse.getSource(), hasKey("exclude")); assertThat(getResponse.getSource(), hasKey("include")); logger.info("Index doc again"); index( "test", "type", "1", JsonXContent.contentBuilder() .startObject() .field("normal", 2) .field("exclude", 1) .field("include", 2) .endObject()); // but do affect newly indexed docs getResponse = get("test", "type", "1"); assertThat(getResponse.getSource(), hasKey("normal")); assertThat(getResponse.getSource(), not(hasKey("exclude"))); assertThat(getResponse.getSource(), hasKey("include")); logger.info("Changing mapping to includes"); putResponse = client() .admin() .indices() .preparePutMapping("test") .setType("type") .setSource( JsonXContent.contentBuilder() .startObject() .startObject("type") .startObject("_source") .startArray("excludes") .endArray() .startArray("includes") .value("include") .endArray() .endObject() .endObject()) .get(); assertTrue(putResponse.isAcknowledged()); GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").get(); MappingMetaData typeMapping = getMappingsResponse.getMappings().get("test").get("type"); assertThat( (Map<String, Object>) typeMapping.getSourceAsMap().get("_source"), hasKey("includes")); ArrayList<String> includes = (ArrayList<String>) ((Map<String, Object>) typeMapping.getSourceAsMap().get("_source")).get("includes"); assertThat(includes, contains("include")); assertThat( (Map<String, Object>) typeMapping.getSourceAsMap().get("_source"), hasKey("excludes")); assertThat( (ArrayList<String>) ((Map<String, Object>) typeMapping.getSourceAsMap().get("_source")).get("excludes"), emptyIterable()); logger.info("Indexing doc yet again"); index( "test", "type", "1", JsonXContent.contentBuilder() .startObject() .field("normal", 3) .field("exclude", 3) .field("include", 3) .endObject()); getResponse = get("test", "type", "1"); assertThat(getResponse.getSource(), not(hasKey("normal"))); assertThat(getResponse.getSource(), not(hasKey("exclude"))); assertThat(getResponse.getSource(), hasKey("include")); logger.info("Adding excludes, but keep includes"); putResponse = client() .admin() .indices() .preparePutMapping("test") .setType("type") .setSource( JsonXContent.contentBuilder() .startObject() .startObject("type") .startObject("_source") .startArray("excludes") .value("*.excludes") .endArray() .endObject() .endObject()) .get(); assertTrue(putResponse.isAcknowledged()); getMappingsResponse = client().admin().indices().prepareGetMappings("test").get(); typeMapping = getMappingsResponse.getMappings().get("test").get("type"); assertThat( (Map<String, Object>) typeMapping.getSourceAsMap().get("_source"), hasKey("includes")); includes = (ArrayList<String>) ((Map<String, Object>) typeMapping.getSourceAsMap().get("_source")).get("includes"); assertThat(includes, contains("include")); assertThat( (Map<String, Object>) typeMapping.getSourceAsMap().get("_source"), hasKey("excludes")); ArrayList<String> excludes = (ArrayList<String>) ((Map<String, Object>) typeMapping.getSourceAsMap().get("_source")).get("excludes"); assertThat(excludes, contains("*.excludes")); }