【问题标题】:Fields are empty when doing GET in elastic4s在 elastic4s 中执行 GET 时字段为空
【发布时间】:2014-05-28 11:27:08
【问题描述】:

我正在尝试在我的 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 时会遗漏查询信息?

谢谢!

【问题讨论】:

  • 添加文档时要索引哪些字段?字段和来源是有区别的,我想你可能对区别不熟悉。
  • 我编写了一个使用 JsonSource 的索引服务,它是 DocumentSource 的扩展。你可能对我的不熟悉是正确的,因为我是一个有弹性的菜鸟=)。也许这里的一个很好的回应是 elastic4s 行为正确,解释原因并指向正确的文档?如果你有时间当然 =D 我已经将服务添加到问题中。

标签: scala playframework elasticsearch playframework-2.2 elastic4s


【解决方案1】:

您在问题 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。

【讨论】:

    【解决方案2】:

    这些字段是空的,因为 elasticsearch 不返回它。 如果您需要字段,您必须在查询中指明您需要什么字段:

    这是您搜索不带字段的查询:

    search in "test"->"venues" query s"id:${companyId}"
    

    在这个查询中,我们指出我们想要哪个字段,在这种情况下是“名称”和“描述”:

    search in "test"->"venues" fields ("name","description") query s"id:${companyId}"
    

    现在您可以检索字段了:

    for(x <- response.getHits.hits())
          {
            println(x.getFields.get("name").getValue)
    

    您在 get 请求中找到了 getSourceAsString,因为参数 _source 为默认“on”,而 fields 为默认“off”。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      相关资源
      最近更新 更多