简体   繁体   English

在 elasticsearch 中检索单个字段

[英]retrieving individual fields in elasticsearch

i'm currently learning a bit about elasticsearch and right now im trying to obtain specific fields from a searchResponse, im using this code:我目前正在学习一些有关 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();

so what i would like to ask is how i get specific fields from all that data, my database consist of the shakespeare.Json aviable in the ElasticSearch documentation and is formated as such所以我想问的是我如何从所有这些数据中获取特定字段,我的数据库由ElasticSearch 文档中的莎士比亚.Json aviable 组成,并且格式如下

{
"line_id": INT,
"play_name": "String",
"speech_number": INT,
"line_number": "String",
"speaker": "String",
"text_entry": "String",
}

those are the parameters i'm using in case somebody is interested这些是我正在使用的参数,以防有人感兴趣

{"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\"}"},

and that is how the response is seen in the browser any answer or tips or anything is well apreciated这就是在浏览器中看到响应的方式任何答案或提示或任何东西都很好


edit编辑

i used the setFetchSource(include,exclude) as suggested but still quite dont get how to exctract the 2 specific fields "text_entry" and "speaker" from all the _Source what i want to do is return a string that contains only those 2 fields, something like:我按照建议使用了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,

edit 2编辑 2

i tried returning response reponse.getHits() and response.getHits().getHits() to no avail.我尝试返回response reponse.getHits()response.getHits().getHits()无济于事。 so what am i missing to extract those fields?那么我缺少什么来提取这些字段?

You can use addFields methods of SearchRequestBuilder class to specify one or more fields.您可以使用SearchRequestBuilder类的addFields方法来指定一个或多个字段。

Here 's the javadoc for it and this is what it says: 这里的它的Javadoc,这就是它说:

Adds the fields to load and return as part of the search request.添加字段以作为搜索请求的一部分加载和返回。 If none are specified, the source of the document will be returned.如果没有指定,将返回文档的来源。

Eg例如

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更新

For elasticsearch 5.2, you need to use setFetchSource method to include and exclude the fields ( here is javadoc).对于setFetchSource 5.2,您需要使用setFetchSource方法来包含和排除字段( 这里是javadoc)。

Eg例如

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();

最后我使用了一个正则表达式来提取我正在寻找的子字符串

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

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