gpt4 book ai didi

scala - 在 elastic4s 中执行 GET 时字段为空

转载 作者:行者123 更新时间:2023-11-29 02:54:25 28 4
gpt4 key购买 nike

我正在尝试在我的 play2 应用程序中实现一项服务,该服务使用 elastic4s 通过 Id 获取文档。

我在 elasticsearch 中的文档:

curl -XGET 'http://localhost:9200/test/venues/3659653'

{
"_index": "test",
"_type": "venues",
"_id": "3659653",
"_version": 1,
"found": true,
"_source": {
"id": 3659653,
"name": "Salong Anna och Jag",
"description": "",
"telephoneNumber": "0811111",
"postalCode": "16440",
"streetAddress": "Kistagången 12",
"city": "Kista",
"lastReview": null,
"location": {
"lat": 59.4045675,
"lon": 17.9502138
},
"pictures": [],
"employees": [],
"reviews": [],
"strongTags": [
"skönhet ",
"skönhet ",
"skönhetssalong"
],
"weakTags": [
"Frisörsalong",
"Frisörer"
],
"reviewCount": 0,
"averageGrade": 0,
"roundedGrade": 0,
"recoScore": 0
}
}

我的服务:

@Singleton
class VenueSearchService extends ElasticSearchService[IndexableVenue] {

/**
* Elastic search conf
*/
override def path = "test/venues"

def getVenue(companyId: String) = {
val resp = client.execute(
get id companyId from path
).map { response =>
// transform response to IndexableVenue
response
}
resp
}

如果我在响应对象上使用 getFields(),我会得到一个空对象。但是,如果我调用 response.getSourceAsString,我会得到 json 格式的文档:

{

"id": 3659653,
"name": "Salong Anna och Jag ",
"description": "",
"telephoneNumber": "0811111",
"postalCode": "16440",
"streetAddress": "Kistagången 12",
"city": "Kista",
"lastReview": null,
"location": {
"lat": 59.4045675,
"lon": 17.9502138
},
"pictures": [],
"employees": [],
"reviews": [],
"strongTags": [
"skönhet ",
"skönhet ",
"skönhetssalong"
],
"weakTags": [
"Frisörsalong",
"Frisörer"
],
"reviewCount": 0,
"averageGrade": 0,
"roundedGrade": 0,
"recoScore": 0
}

如您所见,get 请求省略了信息:

"_index": "test",
"_type": "venues",
"_id": "3659653",
"_version": 1,
"found": true,
"_source": {}

如果我尝试进行常规搜索:

def getVenue(companyId: String) = {
val resp = client.execute(
search in "test"->"venues" query s"id:${companyId}"
//get id companyId from path
).map { response =>
Logger.info("response: "+response.toString)
}
resp
}

我得到:

{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "venues",
"_id": "3659653",
"_score": 1,
"_source": {
"id": 3659653,
"name": "Salong Anna och Jag ",
"description": "",
"telephoneNumber": "0811111",
"postalCode": "16440",
"streetAddress": "Kistagången 12",
"city": "Kista",
"lastReview": null,
"location": {
"lat": 59.4045675,
"lon": 17.9502138
},
"pictures": [],
"employees": [],
"reviews": [],
"strongTags": [
"skönhet ",
"skönhet ",
"skönhetssalong"
],
"weakTags": [
"Frisörsalong",
"Frisörer"
],
"reviewCount": 0,
"averageGrade": 0,
"roundedGrade": 0,
"recoScore": 0
}
}
]
}
}

我的索引服务:

trait ElasticIndexService [T <: ElasticDocument] {

val clientProvider: ElasticClientProvider
def path: String

def indexInto[T](document: T, id: String)(implicit writes: Writes[T]) : Future[IndexResponse] = {
Logger.debug(s"indexing into $path document: $document")
clientProvider.getClient.execute {
index into path doc JsonSource(document) id id
}
}


}

case class JsonSource[T](document: T)(implicit writes: Writes[T]) extends DocumentSource {
def json: String = {
val js = Json.toJson(document)
Json.stringify(js)
}
}

和索引:

@Singleton
class VenueIndexService @Inject()(
stuff...) extends ElasticIndexService[IndexableVenue] {

def indexVenue(indexableVenue: IndexableVenue) = {

indexInto(indexableVenue, s"${indexableVenue.id.get}")
}
  1. 为什么在执行get时getFields 为空?
  2. 为什么在 get 请求中执行 getSourceAsString 时会遗漏查询信息?

谢谢!

最佳答案

您在问题 1 中遇到的问题是您没有指定要返回的字段。默认情况下,ES 将返回源而不是字段(type 和 _id 除外)。参见 http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-fields.html

我已经向 elastic4s 添加了一个测试来展示如何检索字段,请参阅: https://github.com/sksamuel/elastic4s/blob/master/src%2Ftest%2Fscala%2Fcom%2Fsksamuel%2Felastic4s%2FSearchTest.scala

我不确定问题 2。

关于scala - 在 elastic4s 中执行 GET 时字段为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23910378/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com