scala - 使用elastic4s在搜索中获得零结果

标签 scala elasticsearch elastic4s

这是我用来进行简单搜索的一个小代码:

import com.sksamuel.elastic4s.{ElasticsearchClientUri, ElasticClient}
import com.sksamuel.elastic4s.ElasticDsl._
import org.elasticsearch.common.settings.ImmutableSettings

object Main3 extends App {
  val uri = ElasticsearchClientUri("elasticsearch://localhost:9300")
  val settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch").build()
  val client = ElasticClient.remote(settings, uri)
  if (client.exists("bands").await.isExists()) {
    println("Index already exists!")
    val num = readLine("Want to delete the index? ")
    if (num == "y") {
      client.execute {deleteIndex("bands")}.await
    } else {
      println("Leaving this here ...")
    }
  } else {
    println("Creating the index!")
    client.execute(create index "bands").await
    client.execute(index into "bands/artists" fields "name"->"coldplay").await
    val resp = client.execute(search in "bands/artists" query "coldplay").await
    println(resp)
  }
  client.close()
}

这是我得到的结果:

Connected to the target VM, address: '127.0.0.1:51872', transport: 'socket'
log4j:WARN No appenders could be found for logger (org.elasticsearch.plugins).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Creating the index!
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}
Disconnected from the target VM, address: '127.0.0.1:51872', transport: 'socket'

Process finished with exit code 0

创建索引并将文档添加到该索引运行良好,但简单的搜索查询没有给出任何结果。我什至在 Sense 上检查过这一点。

GET bands/artists/_search
{
  "query": {
    "match": {
      "name": "coldplay"
    }
  }
}

给出

{
   "took": 4,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.30685282,
      "hits": [
         {
            "_index": "bands",
            "_type": "artists",
            "_id": "AU21OYO9w-qZq8hmdTOl",
            "_score": 0.30685282,
            "_source": {
               "name": "coldplay"
            }
         }
      ]
   }
}

如何解决这个问题?

最佳答案

我怀疑发生的情况是您在代码中的索引操作之后直接进行搜索。然而,在elasticsearch中,文档并没有立即可供搜索。请参阅refresh interval setting这里。 (因此,当您使用其余客户端时,您需要等待几秒钟,因为您必须手动在选项卡之间滑动等)。

您可以通过在索引后放置 Thread.sleep(3000) 来快速测试这一点。如果确认它有效,那么您需要考虑如何编写程序。

通常情况下,您只需建立索引,当数据可用时,它就可用。这称为最终一致性。在此期间(几秒钟),用户可能无法进行搜索。这通常不是问题。

如果这是一个问题,那么您将不得不像我们在elastic4s的单元测试中那样采取一些技巧,不断“计数”直到返回正确数量的文档。

最后,您还可以通过调用手动“刷新”索引以加快速度

client.execute {
  refresh index "indexname"
}

但这通常仅在您关闭批量插入的自动刷新时使用。

关于scala - 使用elastic4s在搜索中获得零结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30602459/

相关文章:

scala - 什么是 `path.home` 变量以及如何设置它?

scala - 嵌入式ES实例在运​​行单元测试时失败

scala - Spark RDD - 它们是如何工作的

function - 如何在 Scala 中分析方法?

mongodb - mongoosastic [mapper_parsing_exception] 没有在字段 [type] 上声明的类型 [string] 的处理程序

elasticsearch - 是否可以使用update_by_query API更新到Elasticsearch中具有字段数组的文档?

scala - 是否可以在 scala 匹配器中使用算术?

scala - Akka 2.4.6 - ask(或?)方法在哪里?

elasticsearch - Logstash和Filebeat设置event.dataset值

scala - 使用elastic4s客户端为Elasticsearch构建动态聚合查询