Previous post: Using Jest to implement multi condition query of ElasticSearch under SpringBoot One of the ways to use Jest to get the data in ElasticSearch is to transform it into JsonObject and then parse it to get the field information you want and put it into the entity object. Although this method is a little cumbersome, it has the advantage that the structure can be specified by yourself without considering the hierarchical relationship. But people are lazy, so how to use Jest to put the results directly into the entity? Here is a brief introduction of the method.
1, Build entity
First, the following is the mapping of demo:
{ "mapping": { "info": { "properties": { "textInfo": { "type": "nested", "properties": { } }, "textId": { "type": "keyword" }, "textTags": { "type": "nested", "properties": { } } } } } }
Among them, textId is keyword, and the remaining two are complex structures, which will not be listed in detail. The corresponding entity classes in the program are:
import java.util.List; public class MInfo { private String textId; private List<textDetail> textInfo; private List<textTag> textTags; public String getTextId() { return textId; } public void setTextId(String textId) { this.textId = textId; } public List<textDetail> getTextInfo() { return textInfo; } public void setTextInfo(List<textDetail> textInfo) { this.textInfo = textInfo; } public List<textTag> getTextTags() { return textTags; } public void setTextTags(List<textTag> textTags) { this.textTags = textTags; } }
In fact, it is the entity at the time of writing. Of course, when reading data, it is unnecessary to read all the data and obtain it on demand.
2, Method construction
public List<MInfo> getSentence(QueryConditions queryConditions, Integer begin){ SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery(); List<MInfo> mInfos; try{ setBoolQueryBuilder(queryBuilder, queryConditions); searchSourceBuilder.query(queryBuilder).from(begin).size(limitSize); Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(indexName).addType(typeName).build(); JestResult jestResult = jestClient.execute(search); mInfos = jestResult.getSourceAsObjectList(MInfo.class); logger.info("Information obtained under conditions"); return mInfos; }catch (IOException e){ logger.error("Get information under specified conditions:" + queryConditions.toString() + "IO Exception:", e); return null; }catch (Exception e){ logger.error("Get information under specified conditions:" + queryConditions.toString() + "Exception:", e); return null; } }
Among them, setBoolQueryBuilder(queryBuilder, queryConditions) is a method to set retrieval conditions for custom general settings.
Because this is to query the data in es, you will get multiple results. You need to call getSourceAsObjectList, and getSourceAsObject is often used to write aggregation results to entity classes.
When reading data, it is recommended to use paging method, especially when the amount of reading is large, the method of getting data several times is appropriate.