简体   繁体   English

Elasticsearch Java API不返回命中字段

[英]Elasticsearch Java API do not return fields of hits

I have a problem with the java api for elasticsearch. 我对弹性搜索的java api有问题。

When I do a search like this: 当我像这样搜索时:

MatchQueryBuilder query = QueryBuilders.matchQuery("_type", "booking");

SearchResponse searchResponse = client.prepareSearch().setQuery(query).execute().actionGet();

for (SearchHit hit : searchResponse.getHits()){
    Map<String, SearchHitField> fields = hit.getFields();
    System.out.println(fields.size());
}

my response never has fields, can somebody help me ? 我的回答从来没有字段,有人可以帮助我吗? 在此输入图像描述

I'am using: 我在用:

elasticsearch java api 1.4.0 elasticsearch 1.4.0 elasticsearch java api 1.4.0 elasticsearch 1.4.0

and my data looks like 我的数据看起来像

{
  "_index": "bookings",
  "_type": "booking",
  "_id": "50245171",
  "_score": 1,
  "_source": {
    "field1": "value1",
    "field2": "value2",
    "field3": "value3",
    ...
  }
}

Have you tried adding .addFields() to your query? 您是否尝试过将.addFields()添加到查询中?

SearchResponse searchResponse = client.prepareSearch().setQuery(query).addFields("field1", "field2",...).execute().actionGet();

I'm not sure about all details, but I believe elasticsearch tries to send you as few data as possible. 我不确定所有细节,但我相信elasticsearch会尽可能少地向您发送数据。 Which makes sense, since it should be fast and light. 这是有道理的,因为它应该快速而轻盈。

In any case, are you by any chance indexing Booking objects? 无论如何,您是否有机会索引预订对象? Because if you need the whole object again, you could also just fetch the source and transform that back into its original Booking object. 因为如果您再次需要整个对象,您还可以获取源并将其转换回原始的Booking对象。 For example: 例如:

ObjectMapper mapper = new ObjectMapper();
Booking booking = mapper.readValue(hit.getSourceAsString(), Booking.class);

ObjectMapper is from com.fasterxml.jackson.databind.ObjectMapper (should be included with elasticsearch I believe). ObjectMapper来自com.fasterxml.jackson.databind.ObjectMapper(我认为应该包含在elasticsearch中)。

the fields are not responding as fields.... 字段没有作为字段响应....

with hit.getSource() i got my information hit.getSource()我得到了我的信息

The fields section of a hit includes the fields you explicitly ask for, by default that is none. 命中的字段部分包括您明确要求的字段,默认情况下为none。 You can use addField() or addFields() to add one or more fields to the request. 您可以使用addField()或addFields()将一个或多个字段添加到请求中。 This should populate the fields in the response for you. 这应该为您填充响应中的字段。

The source in each hit contains the entire original object, so may well have data you do not need. 每个匹配中的源包含整个原始对象,因此可能包含您不需要的数据。 Returning the source in this manner is often turned off to prevent sending this excess data around. 以这种方式返回源通常是关闭的,以防止发送这些多余的数据。

QueryBuilders.matchQuery()

seems to work with fields only 似乎只适用于字段

"_type" is a build-in properties of the index... “_type”是索引的内置属性...

Try something like that : 尝试类似的东西:

    client.prepareSearch("bookings")
      .setTypes("booking")
      .setQuery(query)
      .execute()
      .actionGet();

Your query must be based on your document fields 您的查询必须基于您的文档字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM