Search in sources :

Example 21 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)

Example 22 with GetMappingsResponse

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

the class UpdateMappingIntegrationIT method testUpdateMappingWithoutTypeMultiObjects.

public void testUpdateMappingWithoutTypeMultiObjects() throws Exception {
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)).execute().actionGet();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
    PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setType("doc").setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}", XContentType.JSON).execute().actionGet();
    assertThat(putMappingResponse.isAcknowledged(), equalTo(true));
    GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").execute().actionGet();
    assertThat(getMappingsResponse.mappings().get("test").get("doc").source().toString(), equalTo("{\"doc\":{\"properties\":{\"date\":{\"type\":\"integer\"}}}}"));
}
Also used : PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 23 with GetMappingsResponse

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

the class SimpleGetMappingsIT method testSimpleGetMappings.

public void testSimpleGetMappings() throws Exception {
    client().admin().indices().prepareCreate("indexa").addMapping("typeA", getMappingForType("typeA")).addMapping("typeB", getMappingForType("typeB")).addMapping("Atype", getMappingForType("Atype")).addMapping("Btype", getMappingForType("Btype")).execute().actionGet();
    client().admin().indices().prepareCreate("indexb").addMapping("typeA", getMappingForType("typeA")).addMapping("typeB", getMappingForType("typeB")).addMapping("Atype", getMappingForType("Atype")).addMapping("Btype", getMappingForType("Btype")).execute().actionGet();
    ClusterHealthResponse clusterHealth = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
    assertThat(clusterHealth.isTimedOut(), equalTo(false));
    // Get all mappings
    GetMappingsResponse response = client().admin().indices().prepareGetMappings().execute().actionGet();
    assertThat(response.mappings().size(), equalTo(2));
    assertThat(response.mappings().get("indexa").size(), equalTo(4));
    assertThat(response.mappings().get("indexa").get("typeA"), notNullValue());
    assertThat(response.mappings().get("indexa").get("typeB"), notNullValue());
    assertThat(response.mappings().get("indexa").get("Atype"), notNullValue());
    assertThat(response.mappings().get("indexa").get("Btype"), notNullValue());
    assertThat(response.mappings().get("indexb").size(), equalTo(4));
    assertThat(response.mappings().get("indexb").get("typeA"), notNullValue());
    assertThat(response.mappings().get("indexb").get("typeB"), notNullValue());
    assertThat(response.mappings().get("indexb").get("Atype"), notNullValue());
    assertThat(response.mappings().get("indexb").get("Btype"), notNullValue());
    // Get all mappings, via wildcard support
    response = client().admin().indices().prepareGetMappings("*").setTypes("*").execute().actionGet();
    assertThat(response.mappings().size(), equalTo(2));
    assertThat(response.mappings().get("indexa").size(), equalTo(4));
    assertThat(response.mappings().get("indexa").get("typeA"), notNullValue());
    assertThat(response.mappings().get("indexa").get("typeB"), notNullValue());
    assertThat(response.mappings().get("indexa").get("Atype"), notNullValue());
    assertThat(response.mappings().get("indexa").get("Btype"), notNullValue());
    assertThat(response.mappings().get("indexb").size(), equalTo(4));
    assertThat(response.mappings().get("indexb").get("typeA"), notNullValue());
    assertThat(response.mappings().get("indexb").get("typeB"), notNullValue());
    assertThat(response.mappings().get("indexb").get("Atype"), notNullValue());
    assertThat(response.mappings().get("indexb").get("Btype"), notNullValue());
    // Get all typeA mappings in all indices
    response = client().admin().indices().prepareGetMappings("*").setTypes("typeA").execute().actionGet();
    assertThat(response.mappings().size(), equalTo(2));
    assertThat(response.mappings().get("indexa").size(), equalTo(1));
    assertThat(response.mappings().get("indexa").get("typeA"), notNullValue());
    assertThat(response.mappings().get("indexb").size(), equalTo(1));
    assertThat(response.mappings().get("indexb").get("typeA"), notNullValue());
    // Get all mappings in indexa
    response = client().admin().indices().prepareGetMappings("indexa").execute().actionGet();
    assertThat(response.mappings().size(), equalTo(1));
    assertThat(response.mappings().get("indexa").size(), equalTo(4));
    assertThat(response.mappings().get("indexa").get("typeA"), notNullValue());
    assertThat(response.mappings().get("indexa").get("typeB"), notNullValue());
    assertThat(response.mappings().get("indexa").get("Atype"), notNullValue());
    assertThat(response.mappings().get("indexa").get("Btype"), notNullValue());
    // Get all mappings beginning with A* in indexa
    response = client().admin().indices().prepareGetMappings("indexa").setTypes("A*").execute().actionGet();
    assertThat(response.mappings().size(), equalTo(1));
    assertThat(response.mappings().get("indexa").size(), equalTo(1));
    assertThat(response.mappings().get("indexa").get("Atype"), notNullValue());
    // Get all mappings beginning with B* in all indices
    response = client().admin().indices().prepareGetMappings().setTypes("B*").execute().actionGet();
    assertThat(response.mappings().size(), equalTo(2));
    assertThat(response.mappings().get("indexa").size(), equalTo(1));
    assertThat(response.mappings().get("indexa").get("Btype"), notNullValue());
    assertThat(response.mappings().get("indexb").size(), equalTo(1));
    assertThat(response.mappings().get("indexb").get("Btype"), notNullValue());
    // Get all mappings beginning with B* and A* in all indices
    response = client().admin().indices().prepareGetMappings().setTypes("B*", "A*").execute().actionGet();
    assertThat(response.mappings().size(), equalTo(2));
    assertThat(response.mappings().get("indexa").size(), equalTo(2));
    assertThat(response.mappings().get("indexa").get("Atype"), notNullValue());
    assertThat(response.mappings().get("indexa").get("Btype"), notNullValue());
    assertThat(response.mappings().get("indexb").size(), equalTo(2));
    assertThat(response.mappings().get("indexb").get("Atype"), notNullValue());
    assertThat(response.mappings().get("indexb").get("Btype"), notNullValue());
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 24 with GetMappingsResponse

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

the class SimpleGetMappingsIT method testGetMappingsWhereThereAreNone.

public void testGetMappingsWhereThereAreNone() {
    createIndex("index");
    GetMappingsResponse response = client().admin().indices().prepareGetMappings().execute().actionGet();
    assertThat(response.mappings().containsKey("index"), equalTo(true));
    assertThat(response.mappings().get("index").size(), equalTo(0));
}
Also used : GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 25 with GetMappingsResponse

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

the class ChildQuerySearchIT method testAddingParentToExistingMapping.

public void testAddingParentToExistingMapping() throws IOException {
    createIndex("test");
    ensureGreen();
    PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setType("child").setSource("number", "type=integer").get();
    assertThat(putMappingResponse.isAcknowledged(), equalTo(true));
    GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").get();
    Map<String, Object> mapping = getMappingsResponse.getMappings().get("test").get("child").getSourceAsMap();
    // there are potentially some meta fields configured randomly
    assertThat(mapping.size(), greaterThanOrEqualTo(1));
    assertThat(mapping.get("properties"), notNullValue());
    try {
        // Adding _parent metadata field to existing mapping is prohibited:
        client().admin().indices().preparePutMapping("test").setType("child").setSource(jsonBuilder().startObject().startObject("child").startObject("_parent").field("type", "parent").endObject().endObject().endObject()).get();
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e.toString(), containsString("The _parent field's type option can't be changed: [null]->[parent]"));
    }
}
Also used : PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Aggregations

GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)26 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 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 CyclicBarrier (java.util.concurrent.CyclicBarrier)1