Search in sources :

Example 36 with MappingMetadata

use of org.elasticsearch.cluster.metadata.MappingMetadata 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 37 with MappingMetadata

use of org.elasticsearch.cluster.metadata.MappingMetadata 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 38 with MappingMetadata

use of org.elasticsearch.cluster.metadata.MappingMetadata in project elasticsearch by elastic.

the class ESIntegTestCase method assertMappingOnMaster.

/**
     * Waits for the given mapping type to exists on the master node.
     */
public void assertMappingOnMaster(final String index, final String type, final String... fieldNames) throws Exception {
    GetMappingsResponse response = client().admin().indices().prepareGetMappings(index).setTypes(type).get();
    ImmutableOpenMap<String, MappingMetaData> mappings = response.getMappings().get(index);
    assertThat(mappings, notNullValue());
    MappingMetaData mappingMetaData = mappings.get(type);
    assertThat(mappingMetaData, notNullValue());
    Map<String, Object> mappingSource = mappingMetaData.getSourceAsMap();
    assertFalse(mappingSource.isEmpty());
    assertTrue(mappingSource.containsKey("properties"));
    for (String fieldName : fieldNames) {
        Map<String, Object> mappingProperties = (Map<String, Object>) mappingSource.get("properties");
        if (fieldName.indexOf('.') != -1) {
            fieldName = fieldName.replace(".", ".properties.");
        }
        assertThat("field " + fieldName + " doesn't exists in mapping " + mappingMetaData.source().string(), XContentMapValues.extractValue(fieldName, mappingProperties), notNullValue());
    }
}
Also used : MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) Map(java.util.Map) XContentTestUtils.convertToMap(org.elasticsearch.test.XContentTestUtils.convertToMap) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 39 with MappingMetadata

use of org.elasticsearch.cluster.metadata.MappingMetadata in project crate by crate.

the class ColumnPolicyIntegrationTest method testStrictPartitionedTableUpdate.

@Test
public void testStrictPartitionedTableUpdate() throws Exception {
    execute("create table numbers (" + "  num int, " + "  odd boolean," + "  prime boolean" + ") partitioned by (odd) with (column_policy='strict', number_of_replicas=0)");
    ensureYellow();
    GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates(PartitionName.templateName(null, "numbers")).execute().actionGet();
    assertThat(response.getIndexTemplates().size(), is(1));
    IndexTemplateMetaData template = response.getIndexTemplates().get(0);
    CompressedXContent mappingStr = template.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(mappingStr, is(notNullValue()));
    Tuple<XContentType, Map<String, Object>> typeAndMap = XContentHelper.convertToMap(mappingStr.compressedReference(), false);
    @SuppressWarnings("unchecked") Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(mapping.get("dynamic")), is(ColumnPolicy.STRICT.value()));
    execute("insert into numbers (num, odd, prime) values (?, ?, ?)", new Object[] { 6, true, false });
    execute("refresh table numbers");
    MappingMetaData partitionMetaData = clusterService().state().metaData().indices().get(new PartitionName("numbers", Arrays.asList(new BytesRef("true"))).asIndexName()).getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(partitionMetaData.getSourceAsMap().get("dynamic")), is(ColumnPolicy.STRICT.value()));
    expectedException.expect(SQLActionException.class);
    expectedException.expectMessage("Column perfect unknown");
    execute("update numbers set num=?, perfect=? where num=6", new Object[] { 28, true });
}
Also used : MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) PartitionName(io.crate.metadata.PartitionName) XContentType(org.elasticsearch.common.xcontent.XContentType) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) HashMap(java.util.HashMap) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 40 with MappingMetadata

use of org.elasticsearch.cluster.metadata.MappingMetadata in project crate by crate.

the class ColumnPolicyIntegrationTest method testDynamicPartitionedTable.

@Test
public void testDynamicPartitionedTable() throws Exception {
    execute("create table numbers (" + "  num int, " + "  odd boolean," + "  prime boolean" + ") partitioned by (odd) with (column_policy='dynamic', number_of_replicas=0)");
    ensureYellow();
    GetIndexTemplatesResponse templateResponse = client().admin().indices().prepareGetTemplates(PartitionName.templateName(null, "numbers")).execute().actionGet();
    assertThat(templateResponse.getIndexTemplates().size(), is(1));
    IndexTemplateMetaData template = templateResponse.getIndexTemplates().get(0);
    CompressedXContent mappingStr = template.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(mappingStr, is(notNullValue()));
    Tuple<XContentType, Map<String, Object>> typeAndMap = XContentHelper.convertToMap(mappingStr.compressedReference(), false);
    @SuppressWarnings("unchecked") Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(mapping.get("dynamic")), is("true"));
    execute("insert into numbers (num, odd, prime) values (?, ?, ?)", new Object[] { 6, true, false });
    execute("refresh table numbers");
    MappingMetaData partitionMetaData = clusterService().state().metaData().indices().get(new PartitionName("numbers", Collections.singletonList(new BytesRef("true"))).asIndexName()).getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(partitionMetaData.getSourceAsMap().get("dynamic")), is("true"));
    execute("insert into numbers (num, odd, prime, perfect) values (?, ?, ?, ?)", new Object[] { 28, true, false, true });
    ensureYellow();
    execute("refresh table numbers");
    waitForMappingUpdateOnAll("numbers", "perfect");
    execute("select * from numbers order by num");
    assertThat(response.rowCount(), is(2L));
    assertThat(response.cols(), arrayContaining("num", "odd", "perfect", "prime"));
    assertThat(TestingHelpers.printedTable(response.rows()), is("6| true| NULL| false\n" + "28| true| true| false\n"));
    execute("update numbers set prime=true, changed='2014-10-23T10:20', author='troll' where num=28");
    assertThat(response.rowCount(), is(1L));
    waitForMappingUpdateOnAll("numbers", "changed");
}
Also used : MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) PartitionName(io.crate.metadata.PartitionName) XContentType(org.elasticsearch.common.xcontent.XContentType) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) HashMap(java.util.HashMap) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Aggregations

MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)46 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)16 Map (java.util.Map)15 GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)11 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)10 Settings (org.elasticsearch.common.settings.Settings)9 Test (org.junit.Test)9 IOException (java.io.IOException)8 HashMap (java.util.HashMap)7 IndexTemplateMetaData (org.elasticsearch.cluster.metadata.IndexTemplateMetaData)7 PartitionName (io.crate.metadata.PartitionName)6 BytesRef (org.apache.lucene.util.BytesRef)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 Set (java.util.Set)4 ClusterState (org.elasticsearch.cluster.ClusterState)4 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)4 ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)3