我目前有以下代码。以下是使用elastic4s库设置本地elasticsearch实例
val essettings = Settings
.settingsBuilder().put("cluster.name", "elasticsearch")
.put("path.home", "/tmp/v").build()
ElasticsearchConnection.setInstance(ElasticClient.local(essettings))
val mockESItem1 = Map("title" -> "Worth It",
"artist" -> "Fifth Harmony",
"countries" -> Seq("AI", "CA", "IT", "AU", "GB", "IO", "IE", "GI", "US", "SH", "MS", "KY"),
"thumbnails" -> Seq(Map("default" -> "http://cache.vevo.com/Content/VevoImages/video/WRONGVEVOPICTURE.jpg")),
"videoId" -> "VEVOID1",
"videoType" -> "type",
"ytVideoId" -> "YTID1",
"features" -> Seq(),
"duration" -> 230)
在这里,我正在创建一个新索引,然后在上面插入 mockES 项。
client.execute( create index "videos" shards 1 replicas 5 mappings(
"video" as (
"artist" typed StringType,
"title" typed StringType,
"countries" typed StringType,
"thumbnails" typed ObjectType,
"videoId" typed StringType,
"videoType" typed StringType,
"ytVideoId" typed StringType,
"features" typed StringType,
"duration" typed IntegerType
)
)
)
client.execute(
bulk(
index into "videos"/"video" id 1 fields mockESItem1
)
).await
但是,如果我随后运行查询以在我的任何测试中找到该项目,类似于以下内容:
es.execute {
search in "videos" / "video" limit 5 query bool {
must(
queryStringQuery("Worth It").field("title"),
queryStringQuery("Fifth Harmony").field("artist").field("features"),
matchQuery("videoType","type"),
matchQuery("countries","US")
)
}
}.await
该程序因以下错误而中断:
org.elasticsearch.action.NoShardAvailableActionException: null
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.start(TransportSearchTypeAction.java:151) [elasticsearch-2.0.1.jar:2.0.1]
at org.elasticsearch.action.search.type.TransportSearchQueryThenFetchAction.doExecute(TransportSearchQueryThenFetchAction.java:64) [elasticsearch-2.0.1.jar:2.0.1]
at org.elasticsearch.action.search.type.TransportSearchQueryThenFetchAction.doExecute(TransportSearchQueryThenFetchAction.java:53) [elasticsearch-2.0.1.jar:2.0.1]
...
org.elasticsearch.action.search.SearchPhaseExecutionException: all shards failed
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:228) ~[elasticsearch-2.0.1.jar:2.0.1]
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.start(TransportSearchTypeAction.java:151) ~[elasticsearch-2.0.1.jar:2.0.1]
at org.elasticsearch.action.search.type.TransportSearchQueryThenFetchAction.doExecute(TransportSearchQueryThenFetchAction.java:64) ~[elasticsearch-2.0.1.jar:2.0.1]
at org.elasticsearch.action.search.type.TransportSearchQueryThenFetchAction.doExecute(TransportSearchQueryThenFetchAction.java:53) ~[elasticsearch-2.0.1.jar:2.0.1]
几天来,我一直试图断断续续地寻找解决此问题的方法,但找不到任何可以帮助我解决此问题的方法,因此现在查看 SE 上是否有人遇到过类似问题并发现一个修复。
更新
我已经重写了我所拥有的,所以这是一个更简单的例子,因为以前我自己的代码中仍然有几个类我正在使用。
import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.mappings.FieldType.{IntegerType, ObjectType, StringType}
import org.elasticsearch.action.delete.DeleteRequest
import org.elasticsearch.common.settings.Settings
import org.scalatest._
class ESTest extends FlatSpec with Matchers {
val essettings = Settings
.settingsBuilder().put("cluster.name", "elasticsearch")
.put("path.home", "/tmp/v").build()
val client = ElasticClient.local(essettings)
val mockESItem1 = Map("title" -> "Worth It",
"artist" -> "Fifth Harmony",
"countries" -> Seq("AI", "CA", "IT", "AU", "GB", "IO", "IE", "GI", "US", "SH", "MS", "KY"),
"thumbnails" -> Seq(Map("default" -> "http://cache.vevo.com/Content/VevoImages/video/WRONGVEVOPICTURE.jpg")),
"videoId" -> "VEVOID1",
"videoType" -> "type",
"ytVideoId" -> "YTID1",
"features" -> Seq(),
"duration" -> 230)
client.execute(create index "videos" shards 1 replicas 5 mappings (
"video" as(
"artist" typed StringType,
"title" typed StringType,
"countries" typed StringType,
"thumbnails" typed ObjectType,
"videoId" typed StringType,
"videoType" typed StringType,
"ytVideoId" typed StringType,
"features" typed StringType,
"duration" typed IntegerType
)
)
).await
val exists = client.execute {
index exists "videos"
}.await.isExists
println(exists)
println(mockESItem1.get("videoId").get)
client.execute(
index into "videos" / "video" id mockESItem1.get("videoId").get fields mockESItem1
).await
println("hi")
val resp = client.execute {
search in "videos" / "video" limit 5 query bool {
must(
matchQuery("videoType", "type"),
matchQuery("countries", "US")
)
}
}.await
println(resp.hits.head.sourceAsString)
client.client.delete(new DeleteRequest("videos"))
client.close
}
目前,如果您运行它,它将无法执行批量插入,然后NoShardAvailableActionException
在“测试”尝试运行搜索时收到。这可能是由specs2引起的问题吗?