【发布时间】:2017-07-01 23:29:18
【问题描述】:
我目前正在学习一些关于弹性搜索的知识,现在我正在尝试从 searchResponse 中获取特定字段,我正在使用以下代码:
QueryBuilder qb = matchAllQuery();
SearchResponse response = client.prepareSearch(ENTITY_INDEX_NAME)
.setTypes(ENTITY_TYPE_NAME)
.setSearchType(SearchType.QUERY_AND_FETCH)
.setQuery(qb)
.setFrom(0)
.addSort("line_id", SortOrder.ASC)
.setSize(MAX_SIZE_OF_ENTITIES_TO_RETURN)
.execute().actionGet();
client.close();
return response.getHits();
所以我想问的是如何从所有这些数据中获取特定字段,我的数据库包含 shakespeare.Json 在ElasticSearch documentation 中可用,并且格式如下
{
"line_id": INT,
"play_name": "String",
"speech_number": INT,
"line_number": "String",
"speaker": "String",
"text_entry": "String",
}
这些是我正在使用的参数,以防有人感兴趣
{"hits":[{"score":"NaN","id":"2","type":"line","nestedIdentity":null,"version":-1,"source":{"play_name":"Henry IV","speech_number":"","line_number":"","text_entry":"Enter KING HENRY, LORD JOHN OF LANCASTER, the EARL of WESTMORELAND, SIR WALTER BLUNT, and others","speaker":"","line_id":3},"fields":{},"highlightFields":{},"sortValues":[3],"matchedQueries":[],"explanation":null,"shard":{"nodeId":"rxHxu9p_QSSc7K77NFUWQQ","index":"shakespeare","shardId":{"index":{"name":"shakespeare","uuid":"6C3R_1mIQlCVRZfn0XRogw"},"id":2,"indexName":"shakespeare"}},"innerHits":null,"index":"shakespeare","sourceRef":{"childResources":[]},"sourceAsString":"{\"line_id\":3,\"play_name\":\"Henry IV\",\"speech_number\":\"\",\"line_number\":\"\",\"speaker\":\"\",\"text_entry\":\"Enter KING HENRY, LORD JOHN OF LANCASTER, the EARL of WESTMORELAND, SIR WALTER BLUNT, and others\"}"},
这就是在浏览器中看到响应的方式 任何答案或提示或任何东西都非常感谢
编辑
我按照建议使用了setFetchSource(include,exclude),但仍然不知道如何从所有 _Source 中提取 2 个特定字段“text_entry”和“speaker”
我想要做的是返回一个仅包含这两个字段的字符串,例如:
KING HENRY IV, Did lately meet in the intestine shock
KING HENRY IV, The edge of war, like an ill-sheathed knife,
KING HENRY IV, Whose soldier now, under whose blessed cross
KING HENRY IV, Forthwith a power of English shall we levy;
KING HENRY IV, Whose arms were moulded in their mothers womb
KING HENRY IV, To chase these pagans in those holy fields
KING HENRY IV, For our advantage on the bitter cross.
WESTMORELAND, Whose worst was, that the noble Mortimer,
编辑 2
我尝试返回 response reponse.getHits() 和 response.getHits().getHits() 无济于事。那么我在提取这些字段时缺少什么?
【问题讨论】:
标签: java rest elasticsearch intellij-idea spring-data