Java Code Examples for org.elasticsearch.index.query.QueryStringQueryBuilder

The following examples show how to use org.elasticsearch.index.query.QueryStringQueryBuilder. These examples are extracted from open source projects. 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 Project: presto   Source File: ElasticsearchQueryBuilder.java    License: Apache License 2.0 6 votes vote down vote up
public static QueryBuilder buildSearchQuery(ConnectorSession session, TupleDomain<ElasticsearchColumnHandle> constraint, Optional<String> query)
{
    BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
    if (constraint.getDomains().isPresent()) {
        for (Map.Entry<ElasticsearchColumnHandle, Domain> entry : constraint.getDomains().get().entrySet()) {
            ElasticsearchColumnHandle column = entry.getKey();
            Domain domain = entry.getValue();

            checkArgument(!domain.isNone(), "Unexpected NONE domain for %s", column.getName());
            if (!domain.isAll()) {
                queryBuilder.filter(new BoolQueryBuilder().must(buildPredicate(session, column.getName(), domain, column.getType())));
            }
        }
    }
    query.map(QueryStringQueryBuilder::new)
            .ifPresent(queryBuilder::must);

    if (queryBuilder.hasClauses()) {
        return queryBuilder;
    }
    return new MatchAllQueryBuilder();
}
 
Example 2
Source Project: youkefu   Source File: EkmKnowledgeMasterRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Page<EkmKnowledgeMaster> findBySearchKnowledge(boolean datastatus,
		String q,String knowbaseid, String knowledgetype, String orgi, Pageable pageable) {
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
	if(!StringUtils.isBlank(q)){
		q = q.replaceAll("(OR|AND|NOT|:|\\(|\\))", "") ;
		boolQueryBuilder1.must(QueryBuilders.boolQuery().must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND))) ;
	}
	if (!StringUtils.isBlank(knowledgetype)) {
		boolQueryBuilder.must(termQuery("knowledgetype" , knowledgetype)) ;
	}
	if (!StringUtils.isBlank(knowbaseid)) {
		boolQueryBuilder.must(termQuery("knowbaseid" , knowbaseid)) ;
	}
	boolQueryBuilder.must(termQuery("datastatus" , datastatus)) ;
	boolQueryBuilder.must(termQuery("orgi" ,orgi)) ;
	boolQueryBuilder.must(boolQueryBuilder1) ;
	//boolQueryBuilder.must(termQuery("pubstatus" , UKDataContext.PubStatusEnum.PASS.toString())) ;//审核通过
	return processQuery(boolQueryBuilder , pageable);
}
 
Example 3
Source Project: youkefu   Source File: TopicRepositoryImpl.java    License: 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 4
Source Project: youkefu   Source File: TopicRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Page<Topic> getTopicByCateAndUser(String cate  , String q , String user ,final int p , final int ps) {

	Page<Topic> 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(Topic.class)){
		pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, new XiaoEUKResultMapper());
    }
    return pages ; 
}
 
Example 5
Source Project: youkefu   Source File: TopicRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<Topic> getTopicByOrgi(String orgi , String type, String q) {
	
	List<Topic> list  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
	
	if(!StringUtils.isBlank(type)){
		boolQueryBuilder.must(termQuery("cate" , type)) ;
	}
	
    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("updatetime").unmappedType("date").order(SortOrder.DESC));
	SearchQuery searchQuery = searchQueryBuilder.build();
	if(elasticsearchTemplate.indexExists(Topic.class)){
		list = elasticsearchTemplate.queryForList(searchQuery, Topic.class);
    }
    return list ; 
}
 
Example 6
Source Project: youkefu   Source File: KbsTopicCommentRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Page<KbsTopicComment> findByCon(NativeSearchQueryBuilder searchQueryBuilder , String field , String aggname,  String q , final int p , final int ps) {
	Page<KbsTopicComment> pages  = null ;
	if(!StringUtils.isBlank(q)){
	   	searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
	}
   	SearchQuery searchQuery = searchQueryBuilder.build();
    if(elasticsearchTemplate.indexExists(KbsTopicComment.class)){
    	if(!StringUtils.isBlank(q)){
    		pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class  , new UKResultMapper());
    	}else{
    		pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class , new UKAggTopResultExtractor(field , aggname));
    	}
    }
    return pages ; 
}
 
Example 7
Source Project: youkefu   Source File: QuickReplyRepositoryImpl.java    License: 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 8
Source Project: youkefu   Source File: QuickReplyRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Page<QuickReply> getByOrgiAndType(String orgi ,String type, String q , Pageable page) {
	
	Page<QuickReply> list  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
	if(!StringUtils.isBlank(type)) {
		boolQueryBuilder.must(termQuery("type" , type)) ;
	}
	
    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));
	SearchQuery searchQuery = searchQueryBuilder.build().setPageable(page);
	if(elasticsearchTemplate.indexExists(QuickReply.class)){
		list = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class);
    }
    return list ; 
}
 
Example 9
Source Project: youkefu   Source File: QuickReplyRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<QuickReply> getQuickReplyByOrgi(String orgi , String cate,String type, String q) {
	
	List<QuickReply> list  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
	
	if(!StringUtils.isBlank(cate)){
		boolQueryBuilder.must(termQuery("cate" , cate)) ;
	}
	if(!StringUtils.isBlank(type)){
		boolQueryBuilder.must(termQuery("type" , type)) ;
	}
    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("updatetime").unmappedType("date").order(SortOrder.DESC));
	SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(0, 10000));
	if(elasticsearchTemplate.indexExists(QuickReply.class)){
		list = elasticsearchTemplate.queryForList(searchQuery, QuickReply.class);
    }
    return list ; 
}
 
Example 10
Source Project: youkefu   Source File: KbsTopicRepositoryImpl.java    License: 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 11
Source Project: youkefu   Source File: KbsTopicRepositoryImpl.java    License: 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 12
Source Project: youkefu   Source File: KbsTopicRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<KbsTopic> getTopicByOrgi(String orgi , String type, String q) {
	
	List<KbsTopic> list  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
	
	if(!StringUtils.isBlank(type)){
		boolQueryBuilder.must(termQuery("cate" , type)) ;
	}
	
    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("updatetime").unmappedType("date").order(SortOrder.DESC));
	SearchQuery searchQuery = searchQueryBuilder.build();
	if(elasticsearchTemplate.indexExists(KbsTopic.class)){
		list = elasticsearchTemplate.queryForList(searchQuery, KbsTopic.class);
    }
    return list ; 
}
 
Example 13
Source Project: youkefu   Source File: ContactsRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Page<Contacts> findByCreaterAndSharesAndOrgi(String creater, String shares ,String orgi, boolean includeDeleteData ,String q , Pageable page) {
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
	boolQueryBuilder1.should(termQuery("creater" , creater)) ;
	boolQueryBuilder1.should(termQuery("shares" , creater)) ;
	boolQueryBuilder1.should(termQuery("shares" , "all")) ;
	boolQueryBuilder.must(boolQueryBuilder1) ;
	boolQueryBuilder.must(termQuery("orgi" ,orgi)) ;
	if(includeDeleteData){
		boolQueryBuilder.must(termQuery("datastatus" , true)) ;
	}else{
		boolQueryBuilder.must(termQuery("datastatus" , false)) ;
	}
	if(!StringUtils.isBlank(q)){
    	boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
    }
	return processQuery(boolQueryBuilder , page);
}
 
Example 14
Source Project: youkefu   Source File: EntCustomerRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Page<EntCustomer> findByCreaterAndSharesAndOrgi(String creater, String shares ,String orgi, boolean includeDeleteData ,String q , Pageable page) {
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
	boolQueryBuilder1.should(termQuery("creater" , creater)) ;
	boolQueryBuilder1.should(termQuery("shares" , creater)) ;
	boolQueryBuilder1.should(termQuery("shares" , "all")) ;
	boolQueryBuilder.must(boolQueryBuilder1) ;
	boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
	if(includeDeleteData){
		boolQueryBuilder.must(termQuery("datastatus" , true)) ;
	}else{
		boolQueryBuilder.must(termQuery("datastatus" , false)) ;
	}
	if(!StringUtils.isBlank(q)){
    	boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
    }
	return processQuery(boolQueryBuilder , page);
}
 
Example 15
Source Project: youkefu   Source File: SearchTools.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param orgi
 * @param agent
 * @param p
 * @param ps
 * @return
 */
public static PageImpl<UKDataBean> namesearch(String orgi , String phonenum){
	BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
	queryBuilder.must(termQuery("orgi", orgi)) ;
	queryBuilder.must(termQuery("validresult", "valid")) ;
	queryBuilder.must(termQuery("status", UKDataContext.NamesDisStatusType.DISAGENT.toString())) ;
	StringBuffer strb = new StringBuffer();
	if(!StringUtils.isBlank(phonenum)) {
		strb.append(phonenum) ;
		if(phonenum.startsWith("0")) {
			strb.append(" ").append(phonenum.substring(1)) ;
		}
	}else {
		strb.append(UKDataContext.UKEFU_SYSTEM_NO_DAT) ;
	}
	queryBuilder.must(new QueryStringQueryBuilder(strb.toString()).defaultOperator(Operator.OR) );
	return search(queryBuilder,0, 1);
}
 
Example 16
@Override
public Maybe<List<PolicyView>> findAll(FindPolicyQuery query) {
    SearchRequest searchRequest = new SearchRequest(INDEX_NAME);

    QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryStringQuery(query.getQueryText())
            .field("number")
            .field("policyHolder");

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(queryStringQueryBuilder).size(100);

    searchRequest.source(searchSourceBuilder);

    return elasticClientAdapter
            .search(searchRequest)
            .map(this::mapSearchResponse);
}
 
Example 17
Source Project: Elasticsearch   Source File: RestActions.java    License: Apache License 2.0 6 votes vote down vote up
public static QuerySourceBuilder parseQuerySource(RestRequest request) {
    String queryString = request.param("q");
    if (queryString == null) {
        return null;
    }
    QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(queryString);
    queryBuilder.defaultField(request.param("df"));
    queryBuilder.analyzer(request.param("analyzer"));
    queryBuilder.analyzeWildcard(request.paramAsBoolean("analyze_wildcard", false));
    queryBuilder.lowercaseExpandedTerms(request.paramAsBoolean("lowercase_expanded_terms", true));
    queryBuilder.lenient(request.paramAsBoolean("lenient", null));
    String defaultOperator = request.param("default_operator");
    if (defaultOperator != null) {
        if ("OR".equals(defaultOperator)) {
            queryBuilder.defaultOperator(QueryStringQueryBuilder.Operator.OR);
        } else if ("AND".equals(defaultOperator)) {
            queryBuilder.defaultOperator(QueryStringQueryBuilder.Operator.AND);
        } else {
            throw new IllegalArgumentException("Unsupported defaultOperator [" + defaultOperator + "], can either be [OR] or [AND]");
        }
    }
    return new QuerySourceBuilder().setQuery(queryBuilder);
}
 
Example 18
Source Project: conductor   Source File: ElasticSearchDAOV5.java    License: Apache License 2.0 6 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 fq = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery);

        final SearchRequestBuilder srb = elasticSearchClient.prepareSearch(logIndexPrefix + "*")
                .setQuery(fq)
                .setTypes(MSG_DOC_TYPE)
                .addSort(SortBuilders.fieldSort("created").order(SortOrder.ASC));

        return mapGetMessagesResponse(srb.execute().actionGet());
    } catch (Exception e) {
        String errorMsg = String.format("Failed to get messages for queue: %s", queue);
        logger.error(errorMsg, e);
        throw new ApplicationException(Code.BACKEND_ERROR, errorMsg, e);
    }
}
 
Example 19
Source Project: conductor   Source File: ElasticSearchDAOV5.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<EventExecution> getEventExecutions(String event) {
    try {
        Expression expression = Expression.fromString("event='" + event + "'");
        QueryBuilder queryBuilder = expression.getFilterBuilder();

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

        final SearchRequestBuilder srb = elasticSearchClient.prepareSearch(logIndexPrefix + "*")
                .setQuery(fq).setTypes(EVENT_DOC_TYPE)
                .addSort(SortBuilders.fieldSort("created")
                        .order(SortOrder.ASC));

        return mapEventExecutionsResponse(srb.execute().actionGet());
    } catch (Exception e) {
        String errorMsg = String.format("Failed to get executions for event: %s", event);
        logger.error(errorMsg, e);
        throw new ApplicationException(Code.BACKEND_ERROR, errorMsg, e);
    }
}
 
Example 20
Source Project: conductor   Source File: ElasticSearchRestDAOV5.java    License: Apache License 2.0 6 votes vote down vote up
private SearchResult<String> searchObjectIdsViaExpression(String structuredQuery, int start, int size, List<String> sortOptions, String freeTextQuery, String docType) {
    try {
        // Build query
        QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        if(StringUtils.isNotEmpty(structuredQuery)) {
            Expression expression = Expression.fromString(structuredQuery);
            queryBuilder = expression.getFilterBuilder();
        }

        BoolQueryBuilder filterQuery = QueryBuilders.boolQuery().must(queryBuilder);
        QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery(freeTextQuery);
        BoolQueryBuilder fq = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery);

        return searchObjectIds(indexName, fq, start, size, sortOptions, docType);
    } catch (Exception e) {
        throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, e.getMessage(), e);
    }
}
 
Example 21
Source Project: conductor   Source File: TestElasticSearchDAOV5.java    License: Apache License 2.0 6 votes vote down vote up
private SearchResponse search(String indexName, String structuredQuery, int start,
    int size, String freeTextQuery, String docType) throws ParserException {
    QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
    if (StringUtils.isNotEmpty(structuredQuery)) {
        Expression expression = Expression.fromString(structuredQuery);
        queryBuilder = expression.getFilterBuilder();
    }

    BoolQueryBuilder filterQuery = QueryBuilders.boolQuery().must(queryBuilder);
    QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery(freeTextQuery);
    BoolQueryBuilder fq = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery);
    final SearchRequestBuilder srb = elasticSearchClient.prepareSearch(indexName)
        .setQuery(fq)
        .setTypes(docType)
        .storedFields("_id")
        .setFrom(start)
        .setSize(size);

    return srb.get();
}
 
Example 22
Source Project: conductor   Source File: TestElasticSearchRestDAOV5.java    License: Apache License 2.0 6 votes vote down vote up
private SearchResponse searchObjectIdsViaExpression(String indexName, String structuredQuery, int start, int size,
                                                    List<String> sortOptions, String freeTextQuery, String docType) throws ParserException, IOException {

    // Build query
    QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
    if(StringUtils.isNotEmpty(structuredQuery)) {
        Expression expression = Expression.fromString(structuredQuery);
        queryBuilder = expression.getFilterBuilder();
    }

    BoolQueryBuilder filterQuery = QueryBuilders.boolQuery().must(queryBuilder);
    QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery(freeTextQuery);
    BoolQueryBuilder fq = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery);

    return searchObjectIds(indexName, fq, start, size, sortOptions, docType);
}
 
Example 23
Source Project: es-service-parent   Source File: EsQueryBuilder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * query_string-分词搜索
 * 
 * @param boolQ
 * @param conditions
 * @param conditionType
 */
private void doQueryString(BoolQueryBuilder boolQ, List<Condition> conditions,
        ConditionType conditionType) {
    // 构造查询语法串
    StringBuilder build = new StringBuilder();
    String filed;
    List<String> words;
    for (Condition condition : conditions) {
        filed = condition.getFiled();
        filed = StringUtils.isBlank(filed) ? "_all" : filed;
        words = KeyWordUtil.processKeyWord(condition.getValue());
        for (String word : words) {
            build.append("(").append(filed).append(":").append(word).append(") ")
                    .append(conditionType.name());
        }
    }
    if (build.length() < 1) {
        return;
    }
    String querystring = build.substring(0, build.lastIndexOf(conditionType.name()));
    QueryStringQueryBuilder queryString = QueryBuilders.queryString(querystring)
            .defaultOperator(default_operator);
    mergeBuilder(boolQ, queryString, conditionType);
}
 
Example 24
Source Project: metron   Source File: ElasticsearchMetaAlertSearchDao.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public SearchResponse search(SearchRequest searchRequest) throws InvalidSearchException {
  // Wrap the query to also get any meta-alerts.
  QueryBuilder qb = constantScoreQuery(boolQuery()
      .must(boolQuery()
          .should(new QueryStringQueryBuilder(searchRequest.getQuery()))
          .should(nestedQuery(
              MetaAlertConstants.ALERT_FIELD,
              new QueryStringQueryBuilder(searchRequest.getQuery()),
              ScoreMode.None
              )
          )
      )
      // Ensures that it's a meta alert with active status or that it's an alert (signified by
      // having no status field)
      .must(boolQuery()
          .should(termQuery(MetaAlertConstants.STATUS_FIELD,
              MetaAlertStatus.ACTIVE.getStatusString()))
          .should(boolQuery().mustNot(existsQuery(MetaAlertConstants.STATUS_FIELD)))
      )
      .mustNot(existsQuery(MetaAlertConstants.METAALERT_FIELD))
  );
  return elasticsearchDao.search(searchRequest, qb);
}
 
Example 25
public void testNestedCommonPhraseQuery() throws Exception {
    List<IndexRequestBuilder> reqs = new ArrayList<>();
    reqs.add(client().prepareIndex("test", "_doc", "1").setSource("text", "deutsche Spielbankgesellschaft"));
    indexRandom(true, false, reqs);

    QueryStringQueryBuilder queryStringQueryBuilder =
            QueryBuilders.queryStringQuery("text:\"deutsche spielbankgesellschaft\"");
    ExactPhraseQueryBuilder exactPhraseQueryBuilder = new ExactPhraseQueryBuilder(queryStringQueryBuilder);
    SearchResponse resp = client().prepareSearch("test").setQuery(exactPhraseQueryBuilder).get();
    ElasticsearchAssertions.assertHitCount(resp, 1L);
    assertHits(resp.getHits(), "1");

    QueryStringQueryBuilder queryStringQueryBuilder2 =
            QueryBuilders.queryStringQuery("text:\"deutsche bank\"");
    ExactPhraseQueryBuilder exactPhraseQueryBuilder2 = new ExactPhraseQueryBuilder(queryStringQueryBuilder2);
    SearchResponse resp2 = client().prepareSearch("test").setQuery(exactPhraseQueryBuilder2).get();
    ElasticsearchAssertions.assertHitCount(resp2, 0L);

    QueryStringQueryBuilder queryStringQueryBuilder3 =
            QueryBuilders.queryStringQuery("text:\"deutsche spielbankgesellschaft\" AND NOT text:\"deutsche bank\"");
    ExactPhraseQueryBuilder exactPhraseQueryBuilder3 = new ExactPhraseQueryBuilder(queryStringQueryBuilder3);
    SearchResponse resp3 = client().prepareSearch("test").setQuery(exactPhraseQueryBuilder3).get();
    ElasticsearchAssertions.assertHitCount(resp3, 1L);
    assertHits(resp3.getHits(), "1");
}
 
Example 26
Source Project: youkefu   Source File: KbsTopicCommentRepositoryImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Page<KbsTopicComment> findByCon(
		NativeSearchQueryBuilder searchQueryBuilder, String q, int p, int ps) {
	searchQueryBuilder.withPageable(new PageRequest(p, ps)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC)) ;
	searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("content").fragmentSize(200)) ;
	if(!StringUtils.isBlank(q)){
	   	searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
	}
    return elasticsearchTemplate.queryForPage(searchQueryBuilder.build(), KbsTopicComment.class  , new UKResultMapper()) ;
}
 
Example 27
Source Project: youkefu   Source File: KbsTopicCommentRepositoryImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Page<KbsTopicComment> countByCon(
		NativeSearchQueryBuilder searchQueryBuilder, String q, int p, int ps) {
	Page<KbsTopicComment> pages  = null ;
	if(!StringUtils.isBlank(q)){
	   	searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
	}
   	SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps));
    if(elasticsearchTemplate.indexExists(Topic.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class  , new UKAggResultExtractor("creater") );
    }
    return pages ; 
}
 
Example 28
Source Project: youkefu   Source File: QuickReplyRepositoryImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<QuickReply> findByOrgiAndCreater(String orgi ,String creater , String q) {

	List<QuickReply> pages  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
	
	BoolQueryBuilder quickQueryBuilder = QueryBuilders.boolQuery();
	
	quickQueryBuilder.should(termQuery("type" , UKDataContext.QuickTypeEnum.PUB.toString())) ;
	
	BoolQueryBuilder priQueryBuilder = QueryBuilders.boolQuery();
	
	priQueryBuilder.must(termQuery("type" , UKDataContext.QuickTypeEnum.PRI.toString())) ;
	priQueryBuilder.must(termQuery("creater" , creater)) ;
	
	quickQueryBuilder.should(priQueryBuilder) ;
	
	boolQueryBuilder.must(quickQueryBuilder) ;
	
    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(0, 10000)) ;
    if(elasticsearchTemplate.indexExists(QuickReply.class)){
    	pages = elasticsearchTemplate.queryForList(searchQuery, QuickReply.class);
    }
    return pages ; 
}
 
Example 29
Source Project: youkefu   Source File: QuickReplyRepositoryImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Page<QuickReply> getByCateAndUser(String cate  , String q , String user ,final int p , final int ps) {

	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).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(QuickReply.class)){
		pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class, new UKResultMapper());
    }
    return pages ; 
}
 
Example 30
Source Project: youkefu   Source File: ContactsRepositoryImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Page<Contacts> findByCreaterAndSharesAndOrgi(String creater,
		String shares,String orgi, Date begin, Date end, boolean includeDeleteData,
		BoolQueryBuilder boolQueryBuilder , String q, Pageable page) {
	BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
	boolQueryBuilder1.should(termQuery("creater" , creater)) ;
	boolQueryBuilder1.should(termQuery("shares" , creater)) ;
	boolQueryBuilder1.should(termQuery("shares" , "all")) ;
	boolQueryBuilder.must(boolQueryBuilder1) ;
	boolQueryBuilder.must(termQuery("orgi" ,orgi)) ;
	if(includeDeleteData){
		boolQueryBuilder.must(termQuery("datastatus" , true)) ;
	}else{
		boolQueryBuilder.must(termQuery("datastatus" , false)) ;
	}
	RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("createtime") ;
	if(begin!=null){
		rangeQuery.from(begin.getTime()) ;
	}
	if(end!=null){
		rangeQuery.to(end.getTime()) ;
	}else{
		rangeQuery.to(new Date().getTime()) ;
	}
	if(begin!=null || end!=null){
		boolQueryBuilder.must(rangeQuery) ;
	}
	if(!StringUtils.isBlank(q)){
    	boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
    }
	return processQuery(boolQueryBuilder , page);
}