1 | package org.springframework.data.elasticsearch.repository.query; |
2 | |
3 | |
4 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations; |
5 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; |
6 | import org.springframework.data.elasticsearch.core.query.CriteriaQuery; |
7 | import org.springframework.data.elasticsearch.repository.query.parser.ElasticsearchQueryCreator; |
8 | import org.springframework.data.mapping.context.MappingContext; |
9 | import org.springframework.data.repository.query.ParametersParameterAccessor; |
10 | import org.springframework.data.repository.query.parser.PartTree; |
11 | |
12 | public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery{ |
13 | |
14 | private final PartTree tree; |
15 | private final MappingContext<?, ElasticsearchPersistentProperty> mappingContext; |
16 | |
17 | |
18 | public ElasticsearchPartQuery(ElasticsearchQueryMethod method, ElasticsearchOperations elasticsearchOperations) { |
19 | super(method, elasticsearchOperations); |
20 | this.tree = new PartTree(method.getName(), method.getEntityInformation().getJavaType()); |
21 | this.mappingContext = elasticsearchOperations.getElasticsearchConverter().getMappingContext(); |
22 | } |
23 | |
24 | @Override |
25 | public Object execute(Object[] parameters) { |
26 | ParametersParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters); |
27 | CriteriaQuery query = createQuery(accessor); |
28 | if(queryMethod.isPageQuery()){ |
29 | return elasticsearchOperations.queryForPage(query, queryMethod.getEntityInformation().getJavaType()); |
30 | } |
31 | return elasticsearchOperations.queryForObject(query, queryMethod.getEntityInformation().getJavaType()); |
32 | } |
33 | |
34 | public CriteriaQuery createQuery(ParametersParameterAccessor accessor) { |
35 | return new ElasticsearchQueryCreator(tree, accessor, mappingContext).createQuery(); |
36 | } |
37 | } |