我目前正在学习一些关于elasticsearch的内容,现在我正试图从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();
所以我想问的是我如何从所有数据中获取特定字段,我的数据库由ElasticSearch documentation中可用的 shakespeare.Json 组成,并且形成为< / p>
{
"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)
,但仍然没有得到如何提取2个特定字段&#34; text_entry&#34;和&#34;发言者&#34;来自所有_Source
我想要做的是返回一个只包含这两个字段的字符串,如:
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,
我尝试退回response
reponse.getHits()
和response.getHits().getHits()
无济于事。那么我缺少什么来提取这些字段?
答案 0 :(得分:1)
您可以使用addFields
类的SearchRequestBuilder
方法指定一个或多个字段。
Here是它的javadoc,这就是它所说的:
添加要加载的字段并作为搜索请求的一部分返回。如果 如果没有指定,将返回文档的来源。
E.g。
SearchResponse response = client.prepareSearch(ENTITY_INDEX_NAME)
.setTypes(ENTITY_TYPE_NAME)
.addFields("field1", "field2") //fields
.setSearchType(SearchType.QUERY_AND_FETCH)
.setQuery(qb)
.setFrom(0)
.addSort("line_id", SortOrder.ASC)
.setSize(MAX_SIZE_OF_ENTITIES_TO_RETURN)
.execute().actionGet();
<强> Updte 强>
对于elasticsearch 5.2,您需要使用setFetchSource
方法来包含和排除字段(here是javadoc)。
E.g。
SearchResponse response = client.prepareSearch(ENTITY_INDEX_NAME)
.setTypes(ENTITY_TYPE_NAME)
.setFetchSource(new String[] {"field1"}, null) //fields
.setSearchType(SearchType.QUERY_AND_FETCH)
.setQuery(qb)
.setFrom(0)
.addSort("line_id", SortOrder.ASC)
.setSize(MAX_SIZE_OF_ENTITIES_TO_RETURN)
.execute().actionGet();
答案 1 :(得分:0)
最后我使用正则表达式来提取我正在寻找的子串