2017-02-15 116 views
0

Hello Elasticsearchfriends。Elasticsearch Java Api設置索引設置

我正在努力尋找鍵入分析器。我有一個NodeFactory並在那裏創建一個節點。在我的IndexService類中,我索引了一個完整的json文件,它有一些數據。我不是Elasticsearch專業人士,這就是爲什麼我想知道,我如何將設置添加到我的索引或如果我必須將設置添加到節點。

NodeFactory.java

@Component("es") 
public class ElasticSearchNodeFactory implements FactoryBean<Node> { 
private Node node; 

public ElasticSearchNodeFactory() { 
    System.out.println("hallo"); 
} 

@Override 
public Node getObject() throws Exception { 
    return getNode(); 
} 

@Override 
public Class getObjectType() { 
    return Node.class; 
} 

@Override 
public boolean isSingleton() { 
    return true; 
} 

private Node getNode() throws Exception { 

    ImmutableSettings.Builder meineSettings = ImmutableSettings.settingsBuilder(); 
    meineSettings.put("node.name", "orange11-node"); 
    meineSettings.put("path.data", "/Users/lucaarchidiacono/IdeaProjects/moap2/MP3_MoapSampleBuild/data/index"); 
    meineSettings.put("index.store.type", "memory"); 
    meineSettings.put("http.enabled", false); 
    Settings setting = meineSettings.build(); 
    node = NodeBuilder.nodeBuilder().local(true).data(true).clusterName("orange11-cluster").settings(setting).node(); 
    return node; 
} 
} 

IndexService類:

@Service 
public class IndexService { 
private Node node; 
private Client client; 

@Autowired 
public IndexService(Node node) throws Exception { 
    this.node = node; 

    client = this.node.client(); 


    List<Map<String, Object>> data = jsonToMap(); 
    for (int i = 0; i < data.size(); ++i) { 
     Map<String, Object> object = data.get(i); 

     IndexRequest indexRequest = Requests.indexRequest("orange11").type("profile").id(Integer.toString(i)).source(object); 
     IndexResponse indexResponse = client.index(indexRequest).actionGet(); 

     if (indexResponse != null && indexResponse.isCreated()) { 
      System.out.println("Index has been created !"); 
      // read report from response 
      System.out.println("------------------------------"); 
      System.out.println("Index name: " + indexResponse.getIndex()); 
      System.out.println("Type name: " + indexResponse.getType()); 
      System.out.println("ID: " + indexResponse.getId()); 
      System.out.println("Version: " + indexResponse.getVersion()); 
      System.out.println("------------------------------"); 
     } else { 
      System.err.println("Index creation failed."); 
     } 
    } 

} 

public List<Map<String, Object>> jsonToMap() throws IOException { 

    ObjectMapper mapper = new ObjectMapper(); 

    List<Map<String, Object>> listOfMaps = new ArrayList<Map<String, Object>>(); 

    Map<String, Object>[] jsonDocument = mapper.readValue(new File("/Users/lucaarchidiacono/IdeaProjects/moap2/MP3_MoapSampleBuild/data/index/accounts.json"), new TypeReference<HashMap<String, Object>[]>() {}); 

    for (int i = 0; i < jsonDocument.length; i++) { 

     listOfMaps.add(i, jsonDocument[i]); 
    } 

    return listOfMaps; 
} 


} 
+1

什麼是elasticsearch版本? –

+0

這是Elasticsearchversion 1.7.0 –

+1

首先,你應該看看更新版本的elasticsearch。我認爲elasticsearch 1.x已經到了生命的盡頭。您應該嘗試使用至少2.x版本。 –

回答

1

Here是如何添加設置嵌入式註釋。

Node node = 
    nodeBuilder() 
     .settings(ImmutableSettings.settingsBuilder().put("http.enabled", false)) 
     .client(true) 
    .node(); 

您可以獲得該嵌入式節點的客戶端,如下所示。 (如果你有參考節點對象)

Client client = node.client(); 

如果沒有,你可以簡單地創建新的傳輸客戶端進行查詢。

Client client = new TransportClient() 
     .addTransportAddress(new InetSocketTransportAddress("host1", 9300)) 
     .addTransportAddress(new InetSocketTransportAddress("host2", 9300)); 

然後你可以在創建時給你的索引設置如下。

IndexResponse response = client.prepareIndex("twitter", "tweet", "1") 
     .setSource(jsonBuilder() 
        .startObject() 
         .field("user", "kimchy") 
         .field("postDate", new Date()) 
         .field("message", "trying out Elasticsearch") 
        .endObject() 
       ) 
     .setSettings(ImmutableSettings.settingsBuilder().put("**whatever_setting_key**", **whatever_setting_value**)) 
     .execute() 
     .actionGet();. 

編輯:

您可以使用以下。

String json = "{" + 
     "\"user\":\"kimchy\"," + 
     "\"postDate\":\"2013-01-30\"," + 
     "\"message\":\"trying out Elasticsearch\"" + 
    "}"; 

// add your setting json here 
String setting_json = "{" + 
     "\"user\":\"kimchy\"," + 
     "\"postDate\":\"2013-01-30\"," + 
     "\"message\":\"trying out Elasticsearch\"" + 
    "}"; 

IndexResponse response = client.admin().indices().prepareCreate("twitter") 
     .setSource(json) 
     .setSetting(setting_json) 
     .execute() 
     .actionGet(); 
+0

我無法在client.prepareIndex()。setSource()中找到setSettings() ) –