Search in sources :

Example 16 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project elasticsearch by elastic.

the class MetaDataWriteDataNodesIT method testMetaWrittenWhenIndexIsClosedAndMetaUpdated.

public void testMetaWrittenWhenIndexIsClosedAndMetaUpdated() throws Exception {
    String masterNode = internalCluster().startMasterOnlyNode(Settings.EMPTY);
    final String dataNode = internalCluster().startDataOnlyNode(Settings.EMPTY);
    final String index = "index";
    assertAcked(prepareCreate(index).setSettings(Settings.builder().put("index.number_of_replicas", 0)));
    logger.info("--> wait for green index");
    ensureGreen();
    logger.info("--> wait for meta state written for index");
    assertIndexInMetaState(dataNode, index);
    assertIndexInMetaState(masterNode, index);
    logger.info("--> close index");
    client().admin().indices().prepareClose(index).get();
    // close the index
    ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().get();
    assertThat(clusterStateResponse.getState().getMetaData().index(index).getState().name(), equalTo(IndexMetaData.State.CLOSE.name()));
    // update the mapping. this should cause the new meta data to be written although index is closed
    client().admin().indices().preparePutMapping(index).setType("doc").setSource(jsonBuilder().startObject().startObject("properties").startObject("integer_field").field("type", "integer").endObject().endObject().endObject()).get();
    GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings(index).addTypes("doc").get();
    assertNotNull(((LinkedHashMap) (getMappingsResponse.getMappings().get(index).get("doc").getSourceAsMap().get("properties"))).get("integer_field"));
    // make sure it was also written on red node although index is closed
    ImmutableOpenMap<String, IndexMetaData> indicesMetaData = getIndicesMetaDataOnNode(dataNode);
    assertNotNull(((LinkedHashMap) (indicesMetaData.get(index).getMappings().get("doc").getSourceAsMap().get("properties"))).get("integer_field"));
    assertThat(indicesMetaData.get(index).getState(), equalTo(IndexMetaData.State.CLOSE));
    /* Try the same and see if this also works if node was just restarted.
         * Each node holds an array of indices it knows of and checks if it should
         * write new meta data by looking up in this array. We need it because if an
         * index is closed it will not appear in the shard routing and we therefore
         * need to keep track of what we wrote before. However, when the node is
         * restarted this array is empty and we have to fill it before we decide
         * what we write. This is why we explicitly test for it.
         */
    internalCluster().restartNode(dataNode, new RestartCallback());
    client().admin().indices().preparePutMapping(index).setType("doc").setSource(jsonBuilder().startObject().startObject("properties").startObject("float_field").field("type", "float").endObject().endObject().endObject()).get();
    getMappingsResponse = client().admin().indices().prepareGetMappings(index).addTypes("doc").get();
    assertNotNull(((LinkedHashMap) (getMappingsResponse.getMappings().get(index).get("doc").getSourceAsMap().get("properties"))).get("float_field"));
    // make sure it was also written on red node although index is closed
    indicesMetaData = getIndicesMetaDataOnNode(dataNode);
    assertNotNull(((LinkedHashMap) (indicesMetaData.get(index).getMappings().get("doc").getSourceAsMap().get("properties"))).get("float_field"));
    assertThat(indicesMetaData.get(index).getState(), equalTo(IndexMetaData.State.CLOSE));
    // finally check that meta data is also written of index opened again
    assertAcked(client().admin().indices().prepareOpen(index).get());
    // make sure index is fully initialized and nothing is changed anymore
    ensureGreen();
    indicesMetaData = getIndicesMetaDataOnNode(dataNode);
    assertThat(indicesMetaData.get(index).getState(), equalTo(IndexMetaData.State.OPEN));
}
Also used : ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) RestartCallback(org.elasticsearch.test.InternalTestCluster.RestartCallback) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 17 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project elasticsearch by elastic.

the class UpdateMappingOnClusterIT method testConflict.

protected void testConflict(String mapping, String mappingUpdate, Version idxVersion, String... errorMessages) throws InterruptedException {
    assertAcked(prepareCreate(INDEX).setSource(mapping, XContentType.JSON).setSettings("index.version.created", idxVersion.id));
    ensureGreen(INDEX);
    GetMappingsResponse mappingsBeforeUpdateResponse = client().admin().indices().prepareGetMappings(INDEX).addTypes(TYPE).get();
    try {
        client().admin().indices().preparePutMapping(INDEX).setType(TYPE).setSource(mappingUpdate, XContentType.JSON).get();
        fail();
    } catch (IllegalArgumentException e) {
        for (String errorMessage : errorMessages) {
            assertThat(e.getMessage(), containsString(errorMessage));
        }
    }
    compareMappingOnNodes(mappingsBeforeUpdateResponse);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 18 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project elasticsearch by elastic.

the class MultiFieldsIntegrationIT method testGeoPointMultiField.

public void testGeoPointMultiField() throws Exception {
    assertAcked(client().admin().indices().prepareCreate("my-index").addMapping("my-type", createMappingSource("geo_point")));
    GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get();
    MappingMetaData mappingMetaData = getMappingsResponse.mappings().get("my-index").get("my-type");
    assertThat(mappingMetaData, not(nullValue()));
    Map<String, Object> mappingSource = mappingMetaData.sourceAsMap();
    Map aField = ((Map) XContentMapValues.extractValue("properties.a", mappingSource));
    logger.info("Keys: {}", aField.keySet());
    assertThat(aField.size(), equalTo(2));
    assertThat(aField.get("type").toString(), equalTo("geo_point"));
    assertThat(aField.get("fields"), notNullValue());
    Map bField = ((Map) XContentMapValues.extractValue("properties.a.fields.b", mappingSource));
    assertThat(bField.size(), equalTo(1));
    assertThat(bField.get("type").toString(), equalTo("keyword"));
    GeoPoint point = new GeoPoint(51, 19);
    client().prepareIndex("my-index", "my-type", "1").setSource("a", point.toString()).setRefreshPolicy(IMMEDIATE).get();
    SearchResponse countResponse = client().prepareSearch("my-index").setSize(0).setQuery(constantScoreQuery(geoDistanceQuery("a").point(51, 19).distance(50, DistanceUnit.KILOMETERS))).get();
    assertThat(countResponse.getHits().getTotalHits(), equalTo(1L));
    countResponse = client().prepareSearch("my-index").setSize(0).setQuery(matchQuery("a.b", point.geohash())).get();
    assertThat(countResponse.getHits().getTotalHits(), equalTo(1L));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) Map(java.util.Map) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 19 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project elasticsearch by elastic.

the class MultiFieldsIntegrationIT method testIpMultiField.

public void testIpMultiField() throws Exception {
    assertAcked(client().admin().indices().prepareCreate("my-index").addMapping("my-type", createMappingSource("ip")));
    GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get();
    MappingMetaData mappingMetaData = getMappingsResponse.mappings().get("my-index").get("my-type");
    assertThat(mappingMetaData, not(nullValue()));
    Map<String, Object> mappingSource = mappingMetaData.sourceAsMap();
    Map aField = ((Map) XContentMapValues.extractValue("properties.a", mappingSource));
    assertThat(aField.size(), equalTo(2));
    assertThat(aField.get("type").toString(), equalTo("ip"));
    assertThat(aField.get("fields"), notNullValue());
    Map bField = ((Map) XContentMapValues.extractValue("properties.a.fields.b", mappingSource));
    assertThat(bField.size(), equalTo(1));
    assertThat(bField.get("type").toString(), equalTo("keyword"));
    client().prepareIndex("my-index", "my-type", "1").setSource("a", "127.0.0.1").setRefreshPolicy(IMMEDIATE).get();
    SearchResponse countResponse = client().prepareSearch("my-index").setSize(0).setQuery(matchQuery("a.b", "127.0.0.1")).get();
    assertThat(countResponse.getHits().getTotalHits(), equalTo(1L));
}
Also used : MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) Map(java.util.Map) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 20 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project elasticsearch by elastic.

the class MultiFieldsIntegrationIT method testMultiFields.

public void testMultiFields() throws Exception {
    assertAcked(client().admin().indices().prepareCreate("my-index").addMapping("my-type", createTypeSource()));
    GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get();
    MappingMetaData mappingMetaData = getMappingsResponse.mappings().get("my-index").get("my-type");
    assertThat(mappingMetaData, not(nullValue()));
    Map<String, Object> mappingSource = mappingMetaData.sourceAsMap();
    Map titleFields = ((Map) XContentMapValues.extractValue("properties.title.fields", mappingSource));
    assertThat(titleFields.size(), equalTo(1));
    assertThat(titleFields.get("not_analyzed"), notNullValue());
    assertThat(((Map) titleFields.get("not_analyzed")).get("type").toString(), equalTo("keyword"));
    client().prepareIndex("my-index", "my-type", "1").setSource("title", "Multi fields").setRefreshPolicy(IMMEDIATE).get();
    SearchResponse searchResponse = client().prepareSearch("my-index").setQuery(matchQuery("title", "multi")).get();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch("my-index").setQuery(matchQuery("title.not_analyzed", "Multi fields")).get();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    assertAcked(client().admin().indices().preparePutMapping("my-index").setType("my-type").setSource(createPutMappingSource()));
    getMappingsResponse = client().admin().indices().prepareGetMappings("my-index").get();
    mappingMetaData = getMappingsResponse.mappings().get("my-index").get("my-type");
    assertThat(mappingMetaData, not(nullValue()));
    mappingSource = mappingMetaData.sourceAsMap();
    assertThat(((Map) XContentMapValues.extractValue("properties.title", mappingSource)).size(), equalTo(2));
    titleFields = ((Map) XContentMapValues.extractValue("properties.title.fields", mappingSource));
    assertThat(titleFields.size(), equalTo(2));
    assertThat(titleFields.get("not_analyzed"), notNullValue());
    assertThat(((Map) titleFields.get("not_analyzed")).get("type").toString(), equalTo("keyword"));
    assertThat(titleFields.get("uncased"), notNullValue());
    assertThat(((Map) titleFields.get("uncased")).get("analyzer").toString(), equalTo("whitespace"));
    client().prepareIndex("my-index", "my-type", "1").setSource("title", "Multi fields").setRefreshPolicy(IMMEDIATE).get();
    searchResponse = client().prepareSearch("my-index").setQuery(matchQuery("title.uncased", "Multi")).get();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
}
Also used : MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) Map(java.util.Map) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)27 MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)11 Map (java.util.Map)8 PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 IOException (java.io.IOException)4 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Client (org.elasticsearch.client.Client)2 IndicesAdminClient (org.elasticsearch.client.IndicesAdminClient)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 TypeMissingException (org.elasticsearch.indices.TypeMissingException)2 ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)1 ReadContext (com.jayway.jsonpath.ReadContext)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1