2016-07-12 4 views
1

にアクセスすることはできません私はElasticSearchにおけるインデックスドキュメントの以下のクラスを書いた:コンストラクタのtransportClientは

import java.net.InetAddress 
import com.typesafe.config.ConfigFactory 
import org.elasticsearch.client.transport.TransportClient 
import org.elasticsearch.common.settings.Settings 
import org.elasticsearch.common.transport.InetSocketTransportAddress 
import play.api.libs.json.{JsString, JsValue} 

/** 
    * Created by liana on 12/07/16. 
    */ 
class ElasticSearchConnector { 

    private var transportClient: TransportClient = null 
    private val host = "localhost" 
    private val port = 9300 
    private val cluster = "elasticsearch" 
    private val indexName = "tests" 
    private val docType = "test" 

    def configElasticSearch(): Unit = 
    { 
    val settings = Settings.settingsBuilder().put("cluster.name", cluster).build() 
    transportClient = new TransportClient(settings) 
    transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port.toInt)) 
    } 

    def putText(json: String, id: Int): String = 
    { 
    val response = transportClient.prepareIndex(indexName, docType, id) 
            .setSource(json) 
            .get() 
    val responseId = response.getId 
    responseId 
    } 
} 

次のようにその後、私はそれを使用する:

val json = """val jsonString = 
{ 
"title": "Elastic", 
"price": 2000, 
"author":{ 
"first": "Zachary", 
"last": "Tong"; 
} 
}""" 

val ec = new ElasticSearchConnector() 
ec.configElasticSearch() 
val id = ec.putText(json) 
System.out.println(id) 

これは私が得たエラーメッセージです:

Error:(28, 23) constructor TransportClient in class TransportClient cannot be accessed in class ElasticSearchConnector transportClient = new TransportClient(settings)

ここで何が間違っていますか?

答えて

0

Elasticsearch Connector APIには、TransportClientクラスにはパブリックコンストラクタがありませんが、プライベートコンストラクタが宣言されています。したがって、TransportClientのインスタンスを直接「新規」にすることはできません。 APIはBuilder Patternをかなり利用しています。そのため、TransportClientのインスタンスを作成するには、次のような処理を行う必要があります。

val settings = Settings.settingsBuilder().put("cluster.name", cluster).build() 
val transportClient = TransportClient.builder().settings(settings).build() 
transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port.toInt)) 
関連する問題