我编写了以下用于在ElasticSearch中建立文档索引的类:
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)
这是我收到的错误消息:
怎么了
最佳答案
在Elasticsearch Connector API中,TransportClient类没有公共(public)构造函数,但是有一个声明的私有(private)构造函数。因此,您不能直接“新建” 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))
关于scala - 无法访问类TransportClient中的构造方法TransportClient,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38327177/