【问题标题】:SearchPhaseExecutionException with Basic Embedded Elasticsearch带有基本嵌入式 Elasticsearch 的 SearchPhaseExecutionException
【发布时间】:2013-06-12 02:49:56
【问题描述】:

我正在使用 Java API 尝试使用基本 Scala 程序的 Elasticsearch:

object TestES {
  var node:Node = NodeBuilder.nodeBuilder.node
  var client:Client = node.client

  def insertDoc(id:String, doc:String) = {
    client.prepareIndex("myindex", "test", id).
           setSource(doc).execute.actionGet
  }

  def countHits(qry:String) = {
    client.prepareSearch("myindex").setTypes("test").
           setQuery(queryString(qry)).execute.actionGet.
           getHits.getTotalHits
  }

  def waitForGreen = client.admin.cluster.prepareHealth().
                     setWaitForGreenStatus.execute.actionGet

  def main(args: Array[String]): Unit = {
    insertDoc("1", """{"foo":"bar"}""")
    //waitForGreen
    println(countHits("bar"))

    node.close
  }
}

这行得通,插入 + 查询在一秒钟内运行。如果我注释掉插入,我会得到以下异常:

Exception in thread "main" org.elasticsearch.action.search.SearchPhaseExecutionException:
Failed to execute phase [query], total failure;
shardFailures {[_na_][myindex][0]: No active shards}

如果我启用waitForGreen 行,它会再次运行,但运行两条行需要半分钟以上。

这似乎很奇怪。在运行查询之前插入文档是必须的,还是有更好的方法?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    运行查询不需要插入文档,但需要创建索引。 当你插入一个文档并且索引不存在时,它会自动创建。

    如果你想避免创建文档,你可以只使用 API 创建索引:

    client.admin.indices.prepareCreate("myindex").execute.actionGet
    

    【讨论】:

    • 我的索引确实存在。它是我第一次运行时创建的(使用insertDoc)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 2022-07-28
    相关资源
    最近更新 更多