2

私はElasticsearchを学んでいて、新しいプロジェクトを開始しました。ここで、マッピングなどを作成するための初期コードをどこに追加する必要があるのでしょうか。さまざまなcURLコマンドを保持する外部スクリプトを作成して実行しますか、それとも、構成コードがあるJavaプロジェクトに独自のパッケージを作成しますか。必要なときに実行しますか?どのアプローチが最も適切で、その理由は何ですか?

XContentBuilderで試したいマッピング

{
    "tweet" : {
        "properties" : {
            "message" : {
                "type" : "string",
                "store" : "yes",
                "index" : "analyzed",
                "null_value" : "na"
            }
        }
    }
}
4

1 に答える 1

1

私はJavaでそれを持っているのが好きです:

public void putMappingFromString(String index, String type, String mapping) {

    IndicesAdminClient iac = getClient().admin().indices();
    PutMappingRequestBuilder pmrb = new PutMappingRequestBuilder(iac);
    pmrb.setIndices(index);
    pmrb.setType(type);
    pmrb.setSource(mapping);
    ListenableActionFuture<PutMappingResponse> laf = pmrb.execute();
    PutMappingResponse pmr = laf.actionGet();
    pmr.getAcknowledged();

}

クラスタの状態から(間接的に)インデックスのマッピングを取得することもできます。

public String getMapping(String index, String type) throws EsuException {
    ClusterState cs = getClient().admin().cluster().prepareState().setFilterIndices(index).execute().actionGet().getState();
    IndexMetaData imd = cs.getMetaData().index(index);

    if (imd == null) {
        throw new EsuIndexDoesNotExistException(index);
    }

    MappingMetaData mmd = imd.mapping(type);

    if (mmd == null) {
        throw new EsuTypeDoesNotExistException(index, type);
    }

    String mapping = "";
    try {
        mapping = mmd.source().string();
    } catch (IOException e) {
        mapping = "{ \"" + e.toString() + "\"}";
    }
    return mapping;
}

これにより、マッピングをクラスパスのリソースとして保存する場合に、ソースコードとともにマッピングをバージョン管理できます。

于 2013-03-26T23:54:21.807 に答える