Configure elasticsearch mapping with java api

后端 未结 4 1393
说谎
说谎 2020-12-10 13:56

I have a few elasticsearch fields that I don\'t want to analyze before indexing. I have read that the right way to do this is by altering the index mapping. Right now my map

4条回答
  •  囚心锁ツ
    2020-12-10 14:34

    Adding this for future readers. Please note you need to perform mapping prior to create the actual index or you will get an exception. See following code.

    client.admin().indices().create(new CreateIndexRequest("indexname")).actionGet();
    
    PutMappingResponse putMappingResponse = client.admin().indices()
        .preparePutMapping("indexname")
        .setType("indextype")
        .setSource(jsonBuilder().prettyPrint()
                    .startObject()
                        .startObject("indextype")
                            .startObject("properties")
                                .startObject("country").field("type", "string").field("index", "not_analyzed").endObject()
                            .endObject()
                        .endObject()
                    .endObject())
        .execute().actionGet();
    
    IndexResponse response1 = client.prepareIndex("indexname", "indextype")
        .setSource(buildIndex())
        .execute()
        .actionGet();   
    
    // Now "Sri Lanka" considered to be a single country :) 
    SearchResponse response = client.prepareSearch("indexname"
        ).addAggregation(AggregationBuilders.terms("countryfacet").field("country")).setSize(30).execute().actionGet(); 
    

提交回复
热议问题