use of org.springframework.data.elasticsearch.core.query.FetchSourceFilter in project spring-data-elasticsearch by spring-projects.
the class MappingElasticsearchConverter method updateFieldsAndSourceFilter.
private void updateFieldsAndSourceFilter(Query query, Class<?> domainClass) {
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(domainClass);
if (persistentEntity != null) {
List<String> fields = query.getFields();
if (!fields.isEmpty()) {
query.setFields(updateFieldNames(fields, persistentEntity));
}
List<String> storedFields = query.getStoredFields();
if (!CollectionUtils.isEmpty(storedFields)) {
query.setStoredFields(updateFieldNames(storedFields, persistentEntity));
}
SourceFilter sourceFilter = query.getSourceFilter();
if (sourceFilter != null) {
String[] includes = null;
String[] excludes = null;
if (sourceFilter.getIncludes() != null) {
includes = updateFieldNames(Arrays.asList(sourceFilter.getIncludes()), persistentEntity).toArray(new String[] {});
}
if (sourceFilter.getExcludes() != null) {
excludes = updateFieldNames(Arrays.asList(sourceFilter.getExcludes()), persistentEntity).toArray(new String[] {});
}
query.addSourceFilter(new FetchSourceFilter(includes, excludes));
}
}
}
use of org.springframework.data.elasticsearch.core.query.FetchSourceFilter in project springboot-templet-start by thedestiny.
the class BookIndexServiceTest method test003.
@Test
public void test003() {
/**
* 排序字段
* {
* "sort": [
* {
* "FIELD": {
* "order": "desc"
* }
* }
* ]
* }
*/
SortBuilder sortBuilder = // 排序字段
SortBuilders.fieldSort("_id").order(SortOrder.ASC);
// 显示和不显示的字段
FetchSourceFilter sourceFilter = new FetchSourceFilter(null, null);
/**
* "math":{
* "FIELD": "TEXT" //字段:值
* }
*/
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("name", "sky")).withPageable(PageRequest.of(1, 1)).withHighlightFields(new HighlightBuilder.Field("sky")).withSourceFilter(sourceFilter).withSort(sortBuilder).build();
log.info("query is {}", searchQuery.toString());
QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
PageRequest page = PageRequest.of(2, 10);
Iterable<BookIndex> search = repository.search(queryBuilder, page);
SearchHits<BookIndex> search1 = template.search(searchQuery, BookIndex.class);
log.info("response {}", search);
log.info("response {}", search1);
// Bool查询现在包括四种子句:must,filter,should,must_not
// filter 比query 快 1 避免算分 2 会缓存结果
// withFilter withQuery
NativeSearchQuery searchQuery1 = new NativeSearchQueryBuilder().withFilter(boolQuery().should(matchQuery("price", 170))).build();
// 关键字查询,不分词
QueryBuilders.termQuery("name", "小明");
// .addAggregation(
AggregationBuilders.terms("all_tags").field("tags");
// 支持 collection
QueryBuilders.termsQuery("name", "小明", "小五");
// text 查询 分词
QueryBuilders.matchQuery("title", "平凡的世界");
// text 查询 分词
QueryBuilders.multiMatchQuery("title", "平凡的世界", "路遥").slop(0);
// 模糊查询 动态将关键词前后增加或者删除一个词进行匹配 模糊查询
QueryBuilders.fuzzyQuery("title", "开发开放").fuzziness(Fuzziness.ONE).prefixLength(// 前缀查询的长度
3).maxExpansions(// max expansion 选项,用来控制模糊查询
10);
// 前缀匹配
QueryBuilders.prefixQuery("title", "开发开放");
// * 是多个 ? 单个字符
QueryBuilders.wildcardQuery("title", "开*放");
QueryBuilders.wildcardQuery("title", "开?放");
// 短语匹配
QueryBuilders.matchPhraseQuery("title", "开放");
// fuzzyQuery、prefixQuery、wildcardQuery 不支持分词查询
// 闭区间查询
QueryBuilders.rangeQuery("fieldName").from("fieldValue1").to("fieldValue2");
// 开区间查询,默认是true,也就是包含
QueryBuilders.rangeQuery("fieldName").from("fieldValue1").to("fieldValue2").includeUpper(false).includeLower(false);
// 大于 gt lt gte lte
QueryBuilders.rangeQuery("fieldName").gt("fieldValue");
// 文档必须完全匹配条件,相当于and
QueryBuilders.boolQuery().must();
// 文档必须不匹配条件,相当于not
QueryBuilders.boolQuery().mustNot();
// 至少满足一个条件,这个文档就符合should,相当于or
QueryBuilders.boolQuery().should();
}
use of org.springframework.data.elasticsearch.core.query.FetchSourceFilter in project xm-ms-entity by xm-online.
the class XmEntityResource method searchXmEntitiesV2.
@GetMapping("/_search/v2/xm-entities")
@Timed
@PreAuthorize("hasPermission({'query': #query}, 'XMENTITY.SEARCH.QUERY')")
@PrivilegeDescription("Privilege to search for the xmEntity corresponding to the query")
public ResponseEntity<List<XmEntity>> searchXmEntitiesV2(@RequestParam String query, @ApiParam Pageable pageable, @ApiParam ElasticFetchSourceFilterDto fetchSourceFilterDto) {
SearchDto searchDto = SearchDto.builder().query(query).pageable(pageable).entityClass(XmEntity.class).fetchSourceFilter(new FetchSourceFilter(fetchSourceFilterDto.getIncludes(), fetchSourceFilterDto.getExcludes())).build();
Page<XmEntity> page = xmEntityService.searchV2(searchDto, null);
HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/xm-entities");
return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
use of org.springframework.data.elasticsearch.core.query.FetchSourceFilter in project snowstorm by IHTSDO.
the class TraceabilityLogService method filterRefsetMembersAndLookupComponentConceptIds.
private Map<Long, List<ReferenceSetMember>> filterRefsetMembersAndLookupComponentConceptIds(Iterable<ReferenceSetMember> persistedReferenceSetMembers, Commit commit, Map<Long, Long> componentToConceptIdMap) {
Map<Long, List<ReferenceSetMember>> conceptToMembersMap = new Long2ObjectArrayMap<>();
List<ReferenceSetMember> membersToLog = new ArrayList<>();
Set<Long> referencedDescriptions = new LongOpenHashSet();
Set<Long> referencedRelationships = new LongOpenHashSet();
for (ReferenceSetMember refsetMember : persistedReferenceSetMembers) {
String conceptId = refsetMember.getConceptId();
if (conceptId != null) {
conceptToMembersMap.computeIfAbsent(parseLong(conceptId), id -> new ArrayList<>()).add(refsetMember);
} else {
final String referencedComponentId = refsetMember.getReferencedComponentId();
if (IdentifierService.isConceptId(referencedComponentId)) {
conceptToMembersMap.computeIfAbsent(Long.parseLong(referencedComponentId), id -> new ArrayList<>()).add(refsetMember);
} else {
membersToLog.add(refsetMember);
if (IdentifierService.isDescriptionId(referencedComponentId)) {
referencedDescriptions.add(parseLong(referencedComponentId));
} else if (IdentifierService.isRelationshipId(referencedComponentId)) {
referencedRelationships.add(parseLong(referencedComponentId));
}
}
}
}
final Set<Long> descriptionIdsToLookup = referencedDescriptions.stream().filter(Predicate.not(componentToConceptIdMap::containsKey)).collect(Collectors.toSet());
final Set<Long> relationshipIdsToLookup = referencedRelationships.stream().filter(Predicate.not(componentToConceptIdMap::containsKey)).collect(Collectors.toSet());
BranchCriteria branchCriteria = null;
if (!descriptionIdsToLookup.isEmpty()) {
branchCriteria = versionControlHelper.getBranchCriteria(commit.getBranch());
for (List<Long> descriptionIdsSegment : Iterables.partition(descriptionIdsToLookup, CLAUSE_LIMIT)) {
try (final SearchHitsIterator<Description> stream = elasticsearchTemplate.searchForStream(new NativeSearchQueryBuilder().withQuery(branchCriteria.getEntityBranchCriteria(Description.class).must(termsQuery(Description.Fields.DESCRIPTION_ID, descriptionIdsSegment))).withFields(Description.Fields.DESCRIPTION_ID, Description.Fields.CONCEPT_ID).withPageable(LARGE_PAGE).build(), Description.class)) {
stream.forEachRemaining(hit -> {
final Description description = hit.getContent();
componentToConceptIdMap.put(parseLong(description.getDescriptionId()), parseLong(description.getConceptId()));
});
}
}
}
if (!relationshipIdsToLookup.isEmpty()) {
if (branchCriteria == null) {
branchCriteria = versionControlHelper.getBranchCriteria(commit.getBranch());
}
for (List<Long> relationshipsIdsSegment : Iterables.partition(relationshipIdsToLookup, CLAUSE_LIMIT)) {
try (final SearchHitsIterator<Relationship> stream = elasticsearchTemplate.searchForStream(new NativeSearchQueryBuilder().withQuery(branchCriteria.getEntityBranchCriteria(Relationship.class).must(termsQuery(Relationship.Fields.RELATIONSHIP_ID, relationshipsIdsSegment))).withSourceFilter(new FetchSourceFilter(new String[] { Relationship.Fields.RELATIONSHIP_ID, Relationship.Fields.SOURCE_ID }, new String[] {})).withPageable(LARGE_PAGE).build(), Relationship.class)) {
stream.forEachRemaining(hit -> {
final Relationship relationship = hit.getContent();
componentToConceptIdMap.put(parseLong(relationship.getRelationshipId()), parseLong(relationship.getSourceId()));
});
}
}
}
membersToLog.forEach(refsetMember -> {
final String referencedComponentId = refsetMember.getReferencedComponentId();
final Long conceptId = componentToConceptIdMap.get(parseLong(referencedComponentId));
if (conceptId != null) {
conceptToMembersMap.computeIfAbsent(conceptId, id -> new ArrayList<>()).add(refsetMember);
} else {
logger.error("Refset member {} with referenced component {} can not be mapped to a concept id for traceability on branch {}", refsetMember.getId(), refsetMember.getReferencedComponentId(), commit.getBranch().getPath());
}
});
return conceptToMembersMap;
}
Aggregations