Search in sources :

Example 1 with FetchSourceFilter

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));
        }
    }
}
Also used : FetchSourceFilter(org.springframework.data.elasticsearch.core.query.FetchSourceFilter) SourceFilter(org.springframework.data.elasticsearch.core.query.SourceFilter) FetchSourceFilter(org.springframework.data.elasticsearch.core.query.FetchSourceFilter)

Example 2 with FetchSourceFilter

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();
}
Also used : BookIndex(com.destiny.wolf.entity.BookIndex) SortBuilder(org.elasticsearch.search.sort.SortBuilder) PageRequest(org.springframework.data.domain.PageRequest) FetchSourceFilter(org.springframework.data.elasticsearch.core.query.FetchSourceFilter) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) Test(org.junit.Test)

Example 3 with FetchSourceFilter

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) FetchSourceFilter(org.springframework.data.elasticsearch.core.query.FetchSourceFilter) SearchDto(com.icthh.xm.ms.entity.service.dto.SearchDto) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) GetMapping(org.springframework.web.bind.annotation.GetMapping) Timed(com.codahale.metrics.annotation.Timed) PrivilegeDescription(com.icthh.xm.commons.permission.annotation.PrivilegeDescription) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with FetchSourceFilter

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;
}
Also used : FetchSourceFilter(org.springframework.data.elasticsearch.core.query.FetchSourceFilter) Iterables(com.google.common.collect.Iterables) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) BranchMetadataHelper(org.snomed.snowstorm.core.data.services.BranchMetadataHelper) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) Long2ObjectArrayMap(it.unimi.dsi.fastutil.longs.Long2ObjectArrayMap) IdentifierService(org.snomed.snowstorm.core.data.services.identifier.IdentifierService) Service(org.springframework.stereotype.Service) Map(java.util.Map) CREATE_CODE_SYSTEM_VERSION(org.snomed.snowstorm.core.data.services.traceability.Activity.ActivityType.CREATE_CODE_SYSTEM_VERSION) QueryBuilders.termsQuery(org.elasticsearch.index.query.QueryBuilders.termsQuery) Commit(io.kaicode.elasticvc.domain.Commit) VersionControlHelper(io.kaicode.elasticvc.api.VersionControlHelper) Logger(org.slf4j.Logger) Config(org.snomed.snowstorm.config.Config) PersistedComponents(org.snomed.snowstorm.core.data.services.pojo.PersistedComponents) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SecurityUtil(org.ihtsdo.sso.integration.SecurityUtil) Collectors(java.util.stream.Collectors) CLAUSE_LIMIT(io.kaicode.elasticvc.api.ComponentService.CLAUSE_LIMIT) List(java.util.List) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) Jackson2ObjectMapperBuilder(org.springframework.http.converter.json.Jackson2ObjectMapperBuilder) CommitListener(io.kaicode.elasticvc.api.CommitListener) ServiceUtil(org.snomed.snowstorm.core.data.services.ServiceUtil) BranchCriteria(io.kaicode.elasticvc.api.BranchCriteria) ElasticsearchOperations(org.springframework.data.elasticsearch.core.ElasticsearchOperations) SearchHitsIterator(org.springframework.data.elasticsearch.core.SearchHitsIterator) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) Lazy(org.springframework.context.annotation.Lazy) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) org.snomed.snowstorm.core.data.domain(org.snomed.snowstorm.core.data.domain) CONTENT(io.kaicode.elasticvc.domain.Commit.CommitType.CONTENT) Long.parseLong(java.lang.Long.parseLong) LARGE_PAGE(io.kaicode.elasticvc.api.VersionControlHelper.LARGE_PAGE) BranchCriteria(io.kaicode.elasticvc.api.BranchCriteria) FetchSourceFilter(org.springframework.data.elasticsearch.core.query.FetchSourceFilter) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) ArrayList(java.util.ArrayList) Long2ObjectArrayMap(it.unimi.dsi.fastutil.longs.Long2ObjectArrayMap) Long.parseLong(java.lang.Long.parseLong) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)

Aggregations

FetchSourceFilter (org.springframework.data.elasticsearch.core.query.FetchSourceFilter)4 Timed (com.codahale.metrics.annotation.Timed)1 BookIndex (com.destiny.wolf.entity.BookIndex)1 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Iterables (com.google.common.collect.Iterables)1 PrivilegeDescription (com.icthh.xm.commons.permission.annotation.PrivilegeDescription)1 XmEntity (com.icthh.xm.ms.entity.domain.XmEntity)1 SearchDto (com.icthh.xm.ms.entity.service.dto.SearchDto)1 BranchCriteria (io.kaicode.elasticvc.api.BranchCriteria)1 CommitListener (io.kaicode.elasticvc.api.CommitListener)1 CLAUSE_LIMIT (io.kaicode.elasticvc.api.ComponentService.CLAUSE_LIMIT)1 VersionControlHelper (io.kaicode.elasticvc.api.VersionControlHelper)1 LARGE_PAGE (io.kaicode.elasticvc.api.VersionControlHelper.LARGE_PAGE)1 Commit (io.kaicode.elasticvc.domain.Commit)1 CONTENT (io.kaicode.elasticvc.domain.Commit.CommitType.CONTENT)1 Long2ObjectArrayMap (it.unimi.dsi.fastutil.longs.Long2ObjectArrayMap)1 LongArrayList (it.unimi.dsi.fastutil.longs.LongArrayList)1 LongOpenHashSet (it.unimi.dsi.fastutil.longs.LongOpenHashSet)1