org.elasticsearch.search.sort.SortOrder Java Examples

The following examples show how to use org.elasticsearch.search.sort.SortOrder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlogRepositoryCustomImpl.java    From Spring-5.0-Projects with MIT License 8 votes vote down vote up
public List<Comment> getCommentsForStatus(String status,int from, int size) {
	
   IncludeExclude includeExclude = new IncludeExclude(status, null);
	
   NestedAggregationBuilder aggregation = AggregationBuilders.nested("aggChild", "comments").
								  subAggregation(AggregationBuilders.terms("aggStatsComment").
								  field("comments.status").
								  includeExclude(includeExclude).
								  subAggregation(AggregationBuilders.topHits("aggSortComment").size(10).sort("comments.createdDate", SortOrder.DESC))
	);

	SearchResponse response = elasticsearchTemplate.getClient().prepareSearch("blog")
	  .setTypes("blog")
	  .addAggregation(aggregation)
	  .execute().actionGet();
	
	List<Aggregation> responseAgg = response.getAggregations().asList();
	
	return getAllCommentsWithStatusFromJson(responseAgg.get(0).toString());
	
}
 
Example #2
Source File: QueryOrderByParser.java    From elasticsearch-sql with MIT License 7 votes vote down vote up
@Override
public void parse(ElasticDslContext dslContext) {
    if(dslContext.getSqlContext().selectOperation()!=null&&dslContext.getSqlContext().selectOperation().groupByClause()==null){
        if(dslContext.getSqlContext().selectOperation().orderClause()!=null){
            ElasticsearchParser.OrderClauseContext orderClauseContext=dslContext.getSqlContext().selectOperation().orderClause();
            for(ElasticsearchParser.OrderContext orderContext:orderClauseContext.order()){
                ElasticsearchParser.NameClauseContext nameContext = orderContext.nameClause();
                if(nameContext instanceof ElasticsearchParser.FieldNameContext){
                    ElasticsearchParser.FieldNameContext fieldNameContext=(ElasticsearchParser.FieldNameContext)nameContext;
                    String field = fieldNameContext.field.getText();
                    if(fieldNameContext.highlighter!=null){
                        dslContext.getParseResult().getHighlighter().add(field);
                    }
                    SortOrder sortOrder;
                    if(orderContext.ASC()!=null) {
                        sortOrder=SortOrder.ASC;
                    }else{
                        sortOrder=SortOrder.DESC;
                    }
                    SortBuilder sortBuilder = SortBuilders.fieldSort(field).sortMode(SortMode.AVG).order(sortOrder);
                    dslContext.getParseResult().getOrderBy().add(sortBuilder);
                }
            }
        }
    }
}
 
Example #3
Source File: SearchService.java    From leyou with Apache License 2.0 6 votes vote down vote up
/**
 * 构建基本查询条件
 *
 * @param queryBuilder
 * @param request
 */
private void searchWithPageAndSort(NativeSearchQueryBuilder queryBuilder, SearchRequest request) {
    // 准备分页参数
    int page = request.getPage();
    int size = request.getSize();

    // 1、分页
    queryBuilder.withPageable(PageRequest.of(page - 1, size));
    // 2、排序
    String sortBy = request.getSortBy();
    Boolean desc = request.getDescending();
    if (StringUtils.isNotBlank(sortBy)) {
        // 如果不为空,则进行排序
        queryBuilder.withSort(SortBuilders.fieldSort(sortBy).order(desc ? SortOrder.DESC : SortOrder.ASC));
    }
}
 
Example #4
Source File: BaseSearchService.java    From albert with MIT License 6 votes vote down vote up
protected SortBuilder addSort(SearchParam param){
		Integer sort = param.getSort();
		
//		if(null==sort){
//			return SortBuilders.fieldSort("lastRefresh").order(SortOrder.DESC);
//		}else if(Constants.Sort.nearest == sort){
//			return SortBuilders.geoDistanceSort("geoPoint").point(param.getLat(), param.getLon()).order(SortOrder.ASC);
//		
//		} else if(Constants.Sort.pageView == sort){
//			return SortBuilders.fieldSort("reviewNum").order(SortOrder.DESC);
//		
//		} else if(Constants.Sort.score == sort){
//			return SortBuilders.fieldSort("avgScore").order(SortOrder.DESC);
//		
//		} else if(Constants.Sort.priceAsc == sort){
//			return SortBuilders.fieldSort("price").order(SortOrder.ASC);
//		
//		} else if(Constants.Sort.priceDesc == sort){
//			return SortBuilders.fieldSort("price").order(SortOrder.DESC);
//		
//		}
		
		return SortBuilders.fieldSort("addTime").order(SortOrder.DESC);
	}
 
Example #5
Source File: EsSort.java    From es-service-parent with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param sort_fileds
 * @return
 */
public static List<SortBuilder> sortAdapter(List<String> sort_fileds) {
    List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
    for (String sort : sort_fileds) {
        String[] items = StringUtils.split(sort, " ");
        if (items.length > 2 || items.length < 2) {
            throw new RuntimeException("排序参数格式不正确,必须为:filed desc|asc,多个filed以逗号分隔!");
        }
        String[] fileds = items[0].split(",");
        for (String filed : fileds) {
            SortBuilder sortb = null;
            if (items[0].equalsIgnoreCase("desc")) {
                sortb = SortBuilders.fieldSort(filed).order(SortOrder.DESC);
            } else {
                sortb = SortBuilders.fieldSort(filed).order(SortOrder.ASC);
            }
            sortBuilders.add(sortb);
        }

    }
    return sortBuilders;
}
 
Example #6
Source File: CustomSearchRepositoryImpl.java    From klask-io with GNU General Public License v3.0 6 votes vote down vote up
/**
 * add the sort order to the request searchRequestBuilder
 * if the frontend send sort with "path : desc". It should be converted to "path.raw" : {"order" : "desc" }
 * https://www.elastic.co/guide/en/elasticsearch/guide/current/multi-fields.html#multi-fields
 *
 * @param pageable
 * @param searchRequestBuilder
 */
private void addPagingAndSortingToSearchRequest(Pageable pageable, SearchRequestBuilder searchRequestBuilder) {
    //par défaut, renvoi la première page trié sur le _score ou le _doc, si rien n'est spécifié
    //effectue le tri
    if (pageable != null) {

        searchRequestBuilder
            .setFrom(pageable.getOffset())
            .setSize(pageable.getPageSize());

        if (pageable.getSort() != null) {
            pageable.getSort().forEach(
                order -> searchRequestBuilder.addSort(
                    Constants.ORDER_FIELD_MAPPING.get(order.getProperty()),
                    SortOrder.valueOf(order.getDirection().name()))
            );
        }
    }
}
 
Example #7
Source File: ProfileTaskLogEsDAO.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public List<ProfileTaskLog> getTaskLogList() throws IOException {
    final SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();

    final BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    sourceBuilder.query(boolQueryBuilder);

    sourceBuilder.sort(ProfileTaskLogRecord.OPERATION_TIME, SortOrder.DESC);
    sourceBuilder.size(queryMaxSize);

    final SearchResponse response = getClient().search(ProfileTaskLogRecord.INDEX_NAME, sourceBuilder);

    final LinkedList<ProfileTaskLog> tasks = new LinkedList<>();
    for (SearchHit searchHit : response.getHits().getHits()) {
        tasks.add(parseTaskLog(searchHit));
    }

    return tasks;
}
 
Example #8
Source File: EkmKnowledgeMasterRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private Page<EkmKnowledgeMaster> processQueryItem(BoolQueryBuilder boolQueryBuilder, Pageable page){
	
	//过滤掉已过期的知识
	//QueryBuilder beginFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("begintime")).should(QueryBuilders.rangeQuery("begintime").to(new Date().getTime())) ;
	//QueryBuilder endFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("endtime")).should(QueryBuilders.rangeQuery("endtime").from(new Date().getTime())) ;
	
	NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
	searchQueryBuilder.withHighlightFields(new Field("title") , new Field("content")) ;
	
	searchQueryBuilder.withPageable(page) ;
	
	Page<EkmKnowledgeMaster> knowledgeList = null ;
	if(elasticsearchTemplate.indexExists(EkmKnowledgeMaster.class)){
		knowledgeList = elasticsearchTemplate.queryForPage(searchQueryBuilder.build() , EkmKnowledgeMaster.class , new EKMResultMapper()) ;
	}
	
	return knowledgeList;
}
 
Example #9
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public List<Message> getMessages(String queue) {
    try {
        BoolQueryBuilder fq = boolQueryBuilder("queue='" + queue + "'", "*");

        String docType = StringUtils.isBlank(docTypeOverride) ? MSG_DOC_TYPE : docTypeOverride;
        final SearchRequestBuilder srb = elasticSearchClient.prepareSearch(messageIndexPrefix + "*")
                .setQuery(fq)
                .setTypes(docType)
                .addSort(SortBuilders.fieldSort("created").order(SortOrder.ASC));

        return mapGetMessagesResponse(srb.execute().actionGet());
    } catch (Exception e) {
        LOGGER.error("Failed to get messages for queue: {}", queue, e);
    }
    return null;
}
 
Example #10
Source File: DefaultMigrationClient.java    From elasticsearch-migration with Apache License 2.0 6 votes vote down vote up
private List<MigrationEntry> getAllMigrations() {
    try {
        final QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.termQuery(MigrationEntryMeta.IDENTIFIER_FIELD, identifier));
        final SearchRequest searchRequest = new SearchRequest()
                .indices(MigrationEntryMeta.INDEX)
                .searchType(SearchType.DEFAULT)
                .source(SearchSourceBuilder.searchSource().query(queryBuilder).fetchSource(true).size(1000).sort(MigrationEntryMeta.VERSION_FIELD, SortOrder.ASC));

        final SearchResponse searchResponse = restHighLevelClient.search(searchRequest);
        if (searchResponse.status() == RestStatus.OK) {
            return transformHitsFromEs(searchResponse.getHits(), MigrationEntry.class);
        } else {
            throw new MigrationFailedException("Could not access '" + MigrationEntryMeta.INDEX + "' index. Failures: " + Arrays.asList(searchResponse.getShardFailures()));
        }
    } catch (IOException e) {
        throw new MigrationFailedException("IO Exception during migration", e);
    }
}
 
Example #11
Source File: TopicRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@Override
public Page<Topic> getTopicByCateAndOrgi(String cate ,String orgi, String q, final int p , final int ps) {

	Page<Topic> pages  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	if(!StringUtils.isBlank(cate)) {
		boolQueryBuilder.must(termQuery("cate" , cate)) ;
	}
	boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
    if(!StringUtils.isBlank(q)){
    	boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
    }
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("top").unmappedType("boolean").order(SortOrder.DESC)).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200)) ;
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)) ;
    if(elasticsearchTemplate.indexExists(Topic.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class , new XiaoEUKResultMapper());
    }
    return pages ; 
}
 
Example #12
Source File: AnalyticsServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 6 votes vote down vote up
@Override
public List<CompletionTime> getTraceCompletions(String tenantId, Criteria criteria) {
    String index = client.getIndex(tenantId);
    if (!refresh(index)) {
        return null;
    }

    BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, CompletionTime.class);
    SearchRequestBuilder request = getTraceCompletionRequest(index, criteria, query, criteria.getMaxResponseSize());
    request.addSort(ElasticsearchUtil.TIMESTAMP_FIELD, SortOrder.DESC);
    SearchResponse response = getSearchResponse(request);
    if (response.isTimedOut()) {
        return null;
    }

    return Arrays.stream(response.getHits().getHits())
            .map(AnalyticsServiceElasticsearch::toCompletionTime)
            .filter(c -> c != null)
            .collect(Collectors.toList());
}
 
Example #13
Source File: BaseClient.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
public Long mostRecentDocument(String index) {
    if (client() == null) {
        return null;
    }
    SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client(), SearchAction.INSTANCE);
    SortBuilder sort = SortBuilders.fieldSort("_timestamp").order(SortOrder.DESC);
    SearchResponse searchResponse = searchRequestBuilder.setIndices(index).addField("_timestamp").setSize(1).addSort(sort).execute().actionGet();
    if (searchResponse.getHits().getHits().length == 1) {
        SearchHit hit = searchResponse.getHits().getHits()[0];
        if (hit.getFields().get("_timestamp") != null) {
            return hit.getFields().get("_timestamp").getValue();
        } else {
            return 0L;
        }
    }
    return null;
}
 
Example #14
Source File: SlowOperQueryHandler.java    From uavstack with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes" })
private SortBuilder[] buildSort(UAVHttpMessage data) {

    SortBuilder[] sorts = null;

    String sort = data.getRequest("sort");

    if (sort != null) {
        String[] sortFieldStrs = sort.split(",");
        List<SortBuilder> ls = new ArrayList<SortBuilder>();
        for (String sortFieldStr : sortFieldStrs) {
            String[] sortExp = sortFieldStr.split("=");
            SortBuilder stimeSort = new FieldSortBuilder(sortExp[0]);
            stimeSort.order(SortOrder.fromString(sortExp[1]));
            ls.add(stimeSort);
        }
        sorts = new SortBuilder[ls.size()];
        sorts = ls.toArray(sorts);
    }
    else {
        return null;
    }
    return sorts;
}
 
Example #15
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 6 votes vote down vote up
protected void applySort(SearchRequestBuilder q) {
    AtomicBoolean sortedById = new AtomicBoolean(false);
    for (SortContainer sortContainer : getParameters().getSortContainers()) {
        if (sortContainer instanceof PropertySortContainer) {
            applySortProperty(q, (PropertySortContainer) sortContainer, sortedById);
        } else if (sortContainer instanceof SortingStrategySortContainer) {
            applySortStrategy(q, (SortingStrategySortContainer) sortContainer);
        } else {
            throw new VertexiumException("Unexpected sorting type: " + sortContainer.getClass().getName());
        }
    }
    q.addSort("_score", SortOrder.DESC);
    if (!sortedById.get()) {
        // If an id sort isn't specified, default is to sort by score and then sort id by ascending order after specified sorts
        q.addSort("_uid", SortOrder.ASC);
    }
}
 
Example #16
Source File: ElasticSearchRestDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public List<TaskExecLog> getTaskExecutionLogs(String taskId) {
    try {
        BoolQueryBuilder query = boolQueryBuilder("taskId='" + taskId + "'", "*");

        // Create the searchObjectIdsViaExpression source
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(query);
        searchSourceBuilder.sort(new FieldSortBuilder("createdTime").order(SortOrder.ASC));
        searchSourceBuilder.size(config.getElasticSearchTasklogLimit());

        // Generate the actual request to send to ES.
        String docType = StringUtils.isBlank(docTypeOverride) ? LOG_DOC_TYPE : docTypeOverride;
        SearchRequest searchRequest = new SearchRequest(logIndexPrefix + "*");
        searchRequest.types(docType);
        searchRequest.source(searchSourceBuilder);

        SearchResponse response = elasticSearchClient.search(searchRequest);

        return mapTaskExecLogsResponse(response);
    } catch (Exception e) {
        logger.error("Failed to get task execution logs for task: {}", taskId, e);
    }
    return null;
}
 
Example #17
Source File: QuickReplyRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@Override
public Page<QuickReply> getByOrgiAndCate(String orgi ,String cate , String q, Pageable page) {

	Page<QuickReply> pages  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("cate" , cate)) ;
	
    if(!StringUtils.isBlank(q)){
    	boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
    }
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200)) ;
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(page) ;
    if(elasticsearchTemplate.indexExists(QuickReply.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class , new UKResultMapper());
    }
    return pages ; 
}
 
Example #18
Source File: RwCrawlerThread.java    From elasticsearch-river-web with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean isContentUpdated(final CrawlerClient client, final UrlQueue<?> urlQueue) {
    final RiverConfigManager riverConfigManager = SingletonLaContainer.getComponent(RiverConfigManager.class);
    final RiverConfig riverConfig = riverConfigManager.get(crawlerContext.getSessionId());
    if (riverConfig.isIncremental()) {
        final EsClient esClient = SingletonLaContainer.getComponent(EsClient.class);
        try {
            final SearchResponse response = esClient.prepareSearch(riverConfig.getIndex()).setTypes(riverConfig.getType())
                    .setQuery(QueryBuilders.termQuery("url", urlQueue.getUrl())).addField("lastModified")
                    .addSort("lastModified", SortOrder.DESC).execute().actionGet();
            final SearchHits hits = response.getHits();
            if (hits.getTotalHits() > 0) {
                final SearchHitField lastModifiedField = hits.getAt(0).getFields().get("lastModified");
                if (lastModifiedField != null) {
                    final Date lastModified = ConversionUtil.convert(lastModifiedField.getValue(), Date.class);
                    if (lastModified != null) {
                        urlQueue.setLastModified(lastModified.getTime());
                    }
                }
            }
        } catch (final Exception e) {
            logger.debug("Failed to retrieve lastModified.", e);
        }
    }
    return super.isContentUpdated(client, urlQueue);
}
 
Example #19
Source File: TestTransportClient.java    From jframe with Apache License 2.0 6 votes vote down vote up
@Test
public void testScrollSearch() {
    QueryBuilder qb = QueryBuilders.termQuery("multi", "test");

    // 100 hits per shard will be returned for each scroll
    SearchResponse scrollResp = client.prepareSearch("index1").addSort(SortParseElement.DOC_FIELD_NAME, SortOrder.ASC)
            .setScroll(new TimeValue(60000)).setQuery(qb).setSize(100).execute().actionGet();
    // Scroll until no hits are returned
    while (true) {

        for (SearchHit hit : scrollResp.getHits().getHits()) {
            // Handle the hit...
        }
        scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
        // Break condition: No hits are returned
        if (scrollResp.getHits().getHits().length == 0) {
            break;
        }
    }
}
 
Example #20
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
public List<String> searchRecentRunningWorkflows(int lastModifiedHoursAgoFrom, int lastModifiedHoursAgoTo) {
    DateTime dateTime = new DateTime();
    QueryBuilder q = QueryBuilders.boolQuery()
            .must(QueryBuilders.rangeQuery("updateTime")
                    .gt(dateTime.minusHours(lastModifiedHoursAgoFrom)))
            .must(QueryBuilders.rangeQuery("updateTime")
                    .lt(dateTime.minusHours(lastModifiedHoursAgoTo)))
            .must(QueryBuilders.termQuery("status", "RUNNING"));

    String docType = StringUtils.isBlank(docTypeOverride) ? WORKFLOW_DOC_TYPE : docTypeOverride;
    SearchRequestBuilder s = elasticSearchClient.prepareSearch(workflowIndexName)
            .setTypes(docType)
            .setQuery(q)
            .setSize(5000)
            .addSort("updateTime", SortOrder.ASC);

    return extractSearchIds(s);
}
 
Example #21
Source File: SpringDataQueryTest.java    From code with Apache License 2.0 6 votes vote down vote up
/**
 * 排序
 */
@Test
public void searchAndSort() {
    // 构建查询条件
    NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
    // 添加基本分词查询
    queryBuilder.withQuery(QueryBuilders.termQuery("category", "手机"));
    // 排序
    queryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.ASC));
    // 搜索,获取结果
    Page<Item> items = itemRepository.search(queryBuilder.build());
    // 总条数
    long total = items.getTotalElements();
    System.out.println("总条数 = " + total);
    items.forEach(System.out::println);
}
 
Example #22
Source File: SearchSourceBuilder.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
public FieldSortBuilder getSort(OrderCondition orderCondition) {
    final String fieldName = orderCondition.getFieldName();
    final SortEnum sort = orderCondition.getOrderCondition();

    FieldSortBuilder order;
    switch (sort) {
        case ASC:
            order = SortBuilders.fieldSort(fieldName).order(SortOrder.ASC);
            break;
        case DESC:
            order = SortBuilders.fieldSort(fieldName).order(SortOrder.DESC);
            break;
        default:
            order = null;
    }
    return order;
}
 
Example #23
Source File: KbsTopicRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@Override
public Page<KbsTopic> getTopicByCate(String cate , String q, final int p , final int ps) {

	Page<KbsTopic> pages  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("cate" , cate)) ;
	
    if(!StringUtils.isBlank(q)){
    	boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
    }
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200)) ;
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)) ;
    if(elasticsearchTemplate.indexExists(KbsTopic.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class , new UKResultMapper());
    }
    return pages ; 
}
 
Example #24
Source File: KbsTopicRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public Page<KbsTopic> getTopicByTop(boolean top , final int p , final int ps) {

	Page<KbsTopic> pages  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("top" , top)) ;
	
	QueryBuilder beginFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("begintime")).should(QueryBuilders.rangeQuery("begintime").from(new Date().getTime())) ;
	QueryBuilder endFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("endtime")).should(QueryBuilders.rangeQuery("endtime").to(new Date().getTime())) ;
	
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withFilter(QueryBuilders.boolQuery().must(beginFilter).must(endFilter)).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    
    searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200)) ;
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)) ;
    if(elasticsearchTemplate.indexExists(KbsTopic.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class , new UKResultMapper());
    }
    return pages ; 
}
 
Example #25
Source File: KbsTopicRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@Override
public Page<KbsTopic> getTopicByCateAndUser(String cate  , String q , String user ,final int p , final int ps) {

	Page<KbsTopic> pages  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("cate" , cate)) ;
	
    if(!StringUtils.isBlank(q)){
    	boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
    }
	
	NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withQuery(termQuery("creater" , user)).withSort(new FieldSortBuilder("top").unmappedType("boolean").order(SortOrder.DESC)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC));
	SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps));
	if(elasticsearchTemplate.indexExists(KbsTopic.class)){
		pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class, new UKResultMapper());
    }
    return pages ; 
}
 
Example #26
Source File: ElasticSearchRestDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public List<Message> getMessages(String queue) {
    try {
        BoolQueryBuilder query = boolQueryBuilder("queue='" + queue + "'", "*");

        // Create the searchObjectIdsViaExpression source
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(query);
        searchSourceBuilder.sort(new FieldSortBuilder("created").order(SortOrder.ASC));

        // Generate the actual request to send to ES.
        String docType = StringUtils.isBlank(docTypeOverride) ? MSG_DOC_TYPE : docTypeOverride;
        SearchRequest searchRequest = new SearchRequest(messageIndexPrefix + "*");
        searchRequest.types(docType);
        searchRequest.source(searchSourceBuilder);

        SearchResponse response = elasticSearchClient.search(searchRequest);
        return mapGetMessagesResponse(response);
    } catch (Exception e) {
        logger.error("Failed to get messages for queue: {}", queue, e);
    }
    return null;
}
 
Example #27
Source File: ModifyIndexFactory.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * <b>重建索引处理</b>
 * <ul>
 * <li>将变更后的数据结构创建一个临时索引</li>
 * <li>将当前索引中的数据迁移到临时索引</li>
 * <li>重建当前索引(删除+重建索引)</li>
 * <li>将临时索引中的数据迁移到当前索引</li>
 * <li>删除临时索引</li>
 * </ul>
 *
 * @param index            索引名称
 * @param type             类型
 * @param updateProperties 更新属性处理
 * @param sortName         排序字段
 * @param order            排序方式
 * @return
 * @throws IOException
 * @throws ExecutionException
 * @throws InterruptedException
 */
public boolean reindex(String index, String type, UpdateProperties updateProperties, String sortName, SortOrder order)
        throws IOException, InterruptedException, ExecutionException {
    String tmp_index = index + "_tmp";
    MappingMetaData metaData = IndexUtils.loadIndexMeta(client, index, type);
    Map<String, Object> data = updateProperties.execute(metaData.getSourceAsMap());
    if (!IndexUtils.createIndex(client, tmp_index, type, data)) {
        throw new IllegalArgumentException("创建临时索引失败");
    }
    //将数据拷贝到临时索引
    copy_data(index, tmp_index, type, sortName, order);
    //删除主索引
    IndexUtils.deleteIndex(client, index);
    //重建主索引
    if (!IndexUtils.createIndex(client, index, type, data)) {
        throw new IllegalArgumentException("重建主索引失败");
    }
    // 从临时索引中拷贝到主索引中
    copy_data(tmp_index, index, type, sortName, order);
    // 删除临时索引
    IndexUtils.deleteIndex(client, tmp_index);
    return true;
}
 
Example #28
Source File: TopicRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public Page<Topic> getTopicByCon(BoolQueryBuilder boolQueryBuilder, final int p , final int ps) {

	Page<Topic> pages  = null ;
	
	QueryBuilder beginFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("begintime")).should(QueryBuilders.rangeQuery("begintime").to(new Date().getTime())) ;
	QueryBuilder endFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("endtime")).should(QueryBuilders.rangeQuery("endtime").from(new Date().getTime())) ;
	
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withFilter(QueryBuilders.boolQuery().must(beginFilter).must(endFilter)).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)) ;
    if(elasticsearchTemplate.indexExists(Topic.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class);
    }
    return pages ; 
}
 
Example #29
Source File: ElasticSearchRestDAOV5.java    From conductor with Apache License 2.0 5 votes vote down vote up
@Override
public List<Message> getMessages(String queue) {
    try {
        Expression expression = Expression.fromString("queue='" + queue + "'");
        QueryBuilder queryBuilder = expression.getFilterBuilder();

        BoolQueryBuilder filterQuery = QueryBuilders.boolQuery().must(queryBuilder);
        QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery("*");
        BoolQueryBuilder query = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery);

        // Create the searchObjectIdsViaExpression source
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(query);
        searchSourceBuilder.sort(new FieldSortBuilder("created").order(SortOrder.ASC));

        // Generate the actual request to send to ES.
        SearchRequest searchRequest = new SearchRequest(logIndexPrefix + "*");
        searchRequest.types(MSG_DOC_TYPE);
        searchRequest.source(searchSourceBuilder);

        SearchResponse response = elasticSearchClient.search(searchRequest);
        return mapGetMessagesResponse(response);
    } catch (Exception e) {
        logger.error("Failed to get messages for queue: {}", queue, e);
        throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, e.getMessage(), e);
    }
}
 
Example #30
Source File: ElasticsearchLengthOfStringSortingStrategy.java    From vertexium with Apache License 2.0 5 votes vote down vote up
@Override
public void updateElasticsearchQuery(
    Graph graph,
    Elasticsearch5SearchIndex searchIndex,
    SearchRequestBuilder q,
    QueryParameters parameters,
    SortDirection direction
) {
    PropertyDefinition propertyDefinition = graph.getPropertyDefinition(getPropertyName());

    SortOrder esOrder = direction == SortDirection.ASCENDING ? SortOrder.ASC : SortOrder.DESC;
    Map<String, Object> scriptParams = new HashMap<>();
    String[] propertyNames = searchIndex.getPropertyNames(graph, getPropertyName(), parameters.getAuthorizations());
    List<String> fieldNames = Arrays.stream(propertyNames)
        .map(propertyName -> {
            String suffix = propertyDefinition.getDataType() == String.class
                ? Elasticsearch5SearchIndex.EXACT_MATCH_PROPERTY_NAME_SUFFIX
                : "";
            return propertyName + suffix;
        })
        .collect(Collectors.toList());
    scriptParams.put("fieldNames", fieldNames);
    scriptParams.put("direction", esOrder.name());
    Script script = new Script(ScriptType.INLINE, "painless", scriptSource, scriptParams);
    ScriptSortBuilder.ScriptSortType sortType = ScriptSortBuilder.ScriptSortType.NUMBER;
    q.addSort(SortBuilders.scriptSort(script, sortType).order(SortOrder.ASC));
}