我正在尝试在我的 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}")
}
  • 为什么在执行 get 时 getFields 为空?
  • 为什么在 get 请求中执行 getSourceAsString 时会遗漏查询信息?

  • 谢谢!

    最佳答案

    您在问题 1 中遇到的是您没有指定要返回的字段。默认情况下,ES 将返回源而不是字段(类型和 _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/

    10-12 06:21