2014-05-08 11 views
10

Ho alcuni campi elasticsearch che non desidero analizzare prima dell'indicizzazione. Ho letto che il modo giusto per farlo è modificando la mappatura dell'indice. In questo momento il mio mappatura assomiglia a questo:Configurare il mapping elasticsearch con java api

{ 
    "test" : { 
    "general" : { 
     "properties" : { 
     "message" : { 
      "type" : "string" 
     }, 
     "source" : { 
      "type" : "string" 
     } 
     } 
    } 
    } 
} 

E vorrei a questo aspetto:

{ 
    "test" : { 
    "general" : { 
     "properties" : { 
     "message" : { 
      "type" : "string", 
      "index" : "not_analyzed" 
     }, 
     "source" : { 
      "type" : "string" 
     } 
     } 
    } 
    } 
} 

ho cercato di modificare le impostazioni tramite

client.admin().indices().prepareCreate("test") 
     .setSettings(getGrantSettings()); 

Dove getGrantSettings() è simile a:

static Settings getGrantSettings(){ 
    JSONObject settingSource = new JSONObject(); 
    try{ 
     settingSource.put("mapping", new JSONObject() 
     .put("message", new JSONObject() 
      .put("type", "string") 
      .put("index", "not_analyzed") 
     )); 
    } catch (JSONException e){ 
     e.printStackTrace(); 
    } 


    Settings set = ImmutableSettings.settingsBuilder() 
      .loadFromSource(settingSource.toString()).build(); 
    return set; 
} 
+1

Non vedo una domanda qui. Stai avendo problemi? Se sì, di che tipo? –

risposta

19

ho applicato con successo mappature ad un indice elasticsearch utilizzando l'API Java come la seguente:

XContentBuilder mapping = jsonBuilder() 
           .startObject() 
            .startObject("general") 
             .startObject("properties") 
              .startObject("message") 
               .field("type", "string") 
               .field("index", "not_analyzed") 
              .endObject() 
              .startObject("source") 
               .field("type","string") 
              .endObject() 
             .endObject() 
            .endObject() 
           .endObject(); 

    PutMappingResponse putMappingResponse = client.admin().indices() 
       .preparePutMapping("test") 
       .setType("general") 
       .setSource(mapping) 
       .execute().actionGet(); 

Spero che questo aiuti.

+3

Giusto per chiarire: jsonBuilder() è un metodo statico in org.elasticsearch.common.xcontent.XContentFactory – padilo

+1

aggiungere questa riga se l'indice non esiste prima di mappare client.admin(). Indices(). Create (new CreateIndexRequest (" indexname ")) actionGet().; prima di applicare la mappatura altrimenti indexMissingException sarà lanciata – swaheed

9

Aggiunta per futuri lettori. Si noti che è necessario eseguire la mappatura prima di creare l'indice effettivo o si otterrà un'eccezione. Vedi il seguente codice.

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(); 
+0

grazie mille ricevevo l'indexmissingexception dopo aver seguito il codice fornito nella risposta accettata in questa riga client.admin(). indices(). create (new CreateIndexRequest ("indexname")). actionGet(); risolto il mio problema – swaheed

0

Basta leggere il Definitive Guide con attenzione:

Anche se è possibile aggiungere un mapping esistente, non è possibile modificare le mappature dei campi esistenti. Se esiste già una mappatura per un campo, è probabile che i dati di quel campo siano stati indicizzati. Se si dovesse modificare la mappatura dei campi, i dati indicizzati sarebbero errati e non sarebbero ricercabili correttamente.

Fonte: https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping-intro.html#updating-a-mapping

quindi bisogna solo farlo quando si crea il campo. Ecco perché il codice da this answer funziona senza problemi.