3

我正在尝试在我的 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
}

如您所见,获取请求会省略信息:

"_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. 为什么 getFields 在做get时是空的?
  2. 为什么在获取请求 中执行 getSourceAsString 时会遗漏查询信息?

谢谢!

4

2 回答 2

3

这些字段是空的,因为 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”。

我希望这能帮到您

于 2014-06-04T11:27:53.067 回答
3

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

于 2014-06-02T08:59:31.537 回答