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

The following examples show how to use org.elasticsearch.index.query.BoolQueryBuilder. 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: conductor   Author: Netflix   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 #2
Source Project: anomaly-detection   Author: opendistro-for-elasticsearch   File: IndexAnomalyDetectorActionHandler.java    License: Apache License 2.0 6 votes vote down vote up
private void checkADNameExists(String detectorId) throws IOException {
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    // src/main/resources/mappings/anomaly-detectors.json#L14
    boolQueryBuilder.must(QueryBuilders.termQuery("name.keyword", anomalyDetector.getName()));
    if (StringUtils.isNotBlank(detectorId)) {
        boolQueryBuilder.mustNot(QueryBuilders.termQuery(RestHandlerUtils._ID, detectorId));
    }
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(boolQueryBuilder).timeout(requestTimeout);
    SearchRequest searchRequest = new SearchRequest(ANOMALY_DETECTORS_INDEX).source(searchSourceBuilder);

    client
        .search(
            searchRequest,
            ActionListener
                .wrap(
                    searchResponse -> onSearchADNameResponse(searchResponse, detectorId, anomalyDetector.getName()),
                    exception -> onFailure(exception)
                )
        );
}
 
Example #3
Source Project: Elasticsearch   Author: baidu   File: DfsOnlyRequest.java    License: Apache License 2.0 6 votes vote down vote up
public DfsOnlyRequest(Fields termVectorsFields, String[] indices, String[] types, Set<String> selectedFields) throws IOException {
    super(indices);

    // build a search request with a query of all the terms
    final BoolQueryBuilder boolBuilder = boolQuery();
    for (String fieldName : termVectorsFields) {
        if ((selectedFields != null) && (!selectedFields.contains(fieldName))) {
            continue;
        }
        Terms terms = termVectorsFields.terms(fieldName);
        TermsEnum iterator = terms.iterator();
        while (iterator.next() != null) {
            String text = iterator.term().utf8ToString();
            boolBuilder.should(QueryBuilders.termQuery(fieldName, text));
        }
    }
    // wrap a search request object
    this.searchRequest = new SearchRequest(indices).types(types).source(new SearchSourceBuilder().query(boolBuilder));
}
 
Example #4
private void fetchFeaturesFromStore() {
    SearchRequest srequest = new SearchRequest(store);
    srequest.setParentTask(clusterService.localNode().getId(), task.getId());
    QueryBuilder nameQuery;

    if (featureNamesQuery.endsWith("*")) {
        String parsed = featureNamesQuery.replaceAll("[*]+$", "");
        if (parsed.isEmpty()) {
            nameQuery = QueryBuilders.matchAllQuery();
        } else {
            nameQuery = QueryBuilders.matchQuery("name.prefix", parsed);
        }
    } else {
        nameQuery = QueryBuilders.matchQuery("name", featureNamesQuery);
    }
    BoolQueryBuilder bq = QueryBuilders.boolQuery();
    bq.must(nameQuery);
    bq.must(QueryBuilders.matchQuery("type", StoredFeature.TYPE));
    srequest.types(IndexFeatureStore.ES_TYPE);
    srequest.source().query(bq);
    srequest.source().fetchSource(true);
    srequest.source().size(StoredFeatureSet.MAX_FEATURES);
    searchAction.execute(srequest, wrap(this::onSearchResponse, this::onSearchFailure));
}
 
Example #5
Source Project: conductor   Author: Netflix   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 #6
Source Project: youkefu   Author: zhangyanbo2007   File: EkmKnowledgeMasterRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public EkmKnowledgeMaster findByTitleAndOrgi(String title, String orgi) {
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	BoolQueryBuilder bq = QueryBuilders.boolQuery() ; 
	bq.must(QueryBuilders.termQuery("title", title)) ;
	bq.must(QueryBuilders.termQuery("orgi", orgi)) ;
	boolQueryBuilder.must(bq); 
	
	NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder) ;
	Page<EkmKnowledgeMaster> knowledgeList = null ;
	if(elasticsearchTemplate.indexExists(EkmKnowledgeMaster.class)){
		knowledgeList = elasticsearchTemplate.queryForPage(searchQueryBuilder.build() , EkmKnowledgeMaster.class ) ;
    }
	
	return knowledgeList.getContent().get(0);
}
 
Example #7
Source Project: youkefu   Author: zhangyanbo2007   File: EkmKnowledgeMasterRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Page<EkmKnowledgeMaster> findByCreaterAndDatastatusAndOrgi(String creater, boolean datastatus, String orgi) {
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	BoolQueryBuilder bq = QueryBuilders.boolQuery() ; 
	bq.must(QueryBuilders.termQuery("creater", creater)) ;
	bq.must(QueryBuilders.termQuery("datastatus", datastatus)) ;
	bq.must(QueryBuilders.termQuery("orgi", orgi)) ;
	boolQueryBuilder.must(bq); 
	
	NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder) ;
	Page<EkmKnowledgeMaster> knowledgeList = null ;
	if(elasticsearchTemplate.indexExists(EkmKnowledgeMaster.class)){
		knowledgeList = elasticsearchTemplate.queryForPage(searchQueryBuilder.build() , EkmKnowledgeMaster.class ) ;
    }
	
	return knowledgeList;
}
 
Example #8
Source Project: nexus-public   Author: sonatype   File: PyPiSearchUtils.java    License: Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Adds subqueries to a BoolQueryBuilder based on the contents of a particular search term in the request.
 */
private static void addSubqueries(final XPath xPath, final BoolQueryBuilder query, final String name, final Node item)
    throws XPathExpressionException
{
  checkNotNull(xPath);
  checkNotNull(query);
  checkNotNull(name);
  checkNotNull(item);
  String parameterName = "attributes.pypi." + name;
  NodeList values = (NodeList) xPath.evaluate(PARAMETER_VALUE_EXPRESSION, item, XPathConstants.NODESET);
  for (int index = 0, count = values.getLength(); index < count; index++) {

    Node value = values.item(index);
    String parameterValue = value.getTextContent().toLowerCase(Locale.ENGLISH);

    // Note that under normal circumstances, prefixing a wildcard query with * or ? is contraindicated according
    // to the ES docs. In this case it's the only way to get the same substring search behavior that PyPI seems
    // to produce, and we think the total number of components in a single repo will be small enough that the
    // penalty will be minimal in real-world terms. If search is running too slow, start looking through here.
    if (!parameterValue.contains("*")) {
      parameterValue = "*" + parameterValue + "*";
    }

    query.should(QueryBuilders.wildcardQuery(parameterName, parameterValue));
  }
}
 
Example #9
Source Project: youkefu   Author: zhangyanbo2007   File: TopicRepositoryImpl.java    License: 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 #10
Source Project: uavstack   Author: uavorg   File: ThreadAnalysisQueryHandler.java    License: Apache License 2.0 6 votes vote down vote up
private void queryMultiDumpInfo(UAVHttpMessage data) {

        String ipport = data.getRequest("ipport");
        String timesStr = data.getRequest("times");
        List<String> times = JSONHelper.toObjectArray(timesStr, String.class);
        List<List<Map<String, Object>>> records = new ArrayList<>();
        for (String time : times) {
            long timestamp = DataConvertHelper.toLong(time, -1L);
            // build query builder
            BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
            queryBuilder.must(QueryBuilders.rangeQuery("time").gte(timestamp).lte(timestamp));
            queryBuilder.must(QueryBuilders.termQuery("ipport", ipport));
            SearchResponse sr = query(data, queryBuilder, null, buildSorts(data));
            List<Map<String, Object>> record = getRecords(sr);

            records.add(record);
        }

        ThreadAnalyser ta = (ThreadAnalyser) getConfigManager().getComponent(feature, "ThreadAnalyser");
        List<Map<String, String>> rs = ta.queryMutilDumpInfo(times, records);
        data.putResponse("rs", JSONHelper.toString(rs));
    }
 
Example #11
Source Project: usergrid   Author: apache   File: RunDao.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns a map of all Runs with queried commitId, runNumber and testName.
 * <p>
 * <ul>
 *     <li>Key of the map is Run's id in elastic search</li>
 *     <li>Value of the map is Run itself</li>
 * </ul>
 *
 * @param commitId    commit id of the Run
 * @param runNumber   Run number to filter queried Runs
 * @param testName    Test class name that resulting Run is about
 * @return            Map satisfying given parameters. The map is empty if there are no Runs.
 */
public Map<String, Run> getMap( String commitId, int runNumber, String testName ) {

    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
            .must( termQuery( "commitId", commitId.toLowerCase() ) )
            .must( termQuery( "runNumber", runNumber ) )
            .must( termQuery( "testName", testName.toLowerCase() ) );

    SearchResponse response = getRequest( DAO_INDEX_KEY, DAO_TYPE_KEY )
            .setQuery( queryBuilder )
            .setSize( MAX_RESULT_SIZE )
            .execute()
            .actionGet();

    HashMap<String, Run> runs = new HashMap<String, Run>();

    for ( SearchHit hit : response.getHits().hits() ) {
        runs.put( hit.getId(), toRun( hit ) );
    }

    return runs;
}
 
Example #12
Source Project: conductor   Author: Netflix   File: ElasticSearchDAOV5.java    License: Apache License 2.0 5 votes vote down vote up
private SearchResult<String> search(String indexName, String structuredQuery, int start, int size,
    List<String> sortOptions, String freeTextQuery, String docType) {
    try {
        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);

        if (sortOptions != null) {
            sortOptions.forEach(sortOption -> addSortOptionToSearchRequest(srb, sortOption));
        }

        SearchResponse response = srb.get();

        LinkedList<String> result = StreamSupport.stream(response.getHits().spliterator(), false)
                .map(SearchHit::getId)
                .collect(Collectors.toCollection(LinkedList::new));
        long count = response.getHits().getTotalHits();

        return new SearchResult<>(count, result);
    } catch (ParserException e) {
        String errorMsg = String.format("Error performing search on index:%s with docType:%s", indexName, docType);
        logger.error(errorMsg);
        throw new ApplicationException(Code.BACKEND_ERROR, errorMsg, e);
    }
}
 
Example #13
Source Project: youkefu   Author: zhangyanbo2007   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,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)) ;
	}
	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);
}
 
Example #14
Source Project: skywalking   Author: apache   File: TopNRecordsQueryEsDAO.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<SelectedRecord> readSampledRecords(final TopNCondition condition,
                                               final String valueColumnName,
                                               final Duration duration) throws IOException {
    SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.must().add(QueryBuilders.rangeQuery(TopN.TIME_BUCKET)
                                             .gte(duration.getStartTimeBucketInSec())
                                             .lte(duration.getEndTimeBucketInSec()));

    if (StringUtil.isNotEmpty(condition.getParentService())) {
        final String serviceId = IDManager.ServiceID.buildId(condition.getParentService(), condition.isNormal());
        boolQueryBuilder.must().add(QueryBuilders.termQuery(TopN.SERVICE_ID, serviceId));
    }

    sourceBuilder.query(boolQueryBuilder);
    sourceBuilder.size(condition.getTopN())
                 .sort(valueColumnName, condition.getOrder().equals(Order.DES) ? SortOrder.DESC : SortOrder.ASC);
    SearchResponse response = getClient().search(condition.getName(), sourceBuilder);

    List<SelectedRecord> results = new ArrayList<>(condition.getTopN());

    for (SearchHit searchHit : response.getHits().getHits()) {
        SelectedRecord record = new SelectedRecord();
        final Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
        record.setName((String) sourceAsMap.get(TopN.STATEMENT));
        record.setRefId((String) sourceAsMap.get(TopN.TRACE_ID));
        record.setId(record.getRefId());
        record.setValue(((Number) sourceAsMap.get(valueColumnName)).toString());
        results.add(record);
    }

    return results;
}
 
Example #15
Source Project: youkefu   Author: zhangyanbo2007   File: SearchTools.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param orgi
 * @param agent
 * @param p
 * @param ps
 * @return
 */
public static PageImpl<UKDataBean> ainamesearch(String orgi , String phonenum){
	BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
	queryBuilder.must(termQuery("orgi", orgi)) ;
	queryBuilder.must(termQuery("validresult", "valid")) ;
	queryBuilder.must(termQuery("status", UKDataContext.NamesDisStatusType.DISAI.toString())) ;
	StringBuffer strb = new StringBuffer();
	if(!StringUtils.isBlank(phonenum)) {
		if(phonenum.startsWith("0")) {
			strb.append(phonenum.substring(1)) ;
		}else {
			strb.append(phonenum) ;
		}
	}else {
		strb.append(UKDataContext.UKEFU_SYSTEM_NO_DAT) ;
	}
	queryBuilder.must(new QueryStringQueryBuilder(strb.toString()).defaultOperator(Operator.OR) );
	return search(queryBuilder,0, 1);
}
 
Example #16
Source Project: 21-points   Author: mraible   File: PointsResource.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * SEARCH  /_search/points?query=:query : search for the points corresponding
 * to the query.
 *
 * @param query the query of the points search
 * @param pageable the pagination information
 * @return the result of the search
 */
@GetMapping("/_search/points")
@Timed
public ResponseEntity<List<Points>> searchPoints(@RequestParam String query, Pageable pageable) {
    log.debug("REST request to search for a page of Points for query {}", query);
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(queryStringQuery(query));
    if (SecurityUtils.isAuthenticated() && !SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)) {
        queryBuilder = queryBuilder.filter(matchQuery("user.login",
            SecurityUtils.getCurrentUserLogin().orElse("")));
    }
    Page<Points> page = pointsSearchRepository.search(queryBuilder, pageable);
    HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/points");
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
 
Example #17
Source Project: dk-fitting   Author: dkhadoop   File: ElasticsearchFilter.java    License: Apache License 2.0 5 votes vote down vote up
public QueryBuilder translate() {
    final List<RexNode> orNodes = RelOptUtil.disjunctions(condition);
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    for (RexNode node : orNodes)
        boolQueryBuilder.should(translateAnd(node));
    return boolQueryBuilder;
}
 
Example #18
Source Project: blue-marlin   Author: Futurewei-io   File: TestBaseDaoESImp.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void createQueryForSingleValues() {
    BaseDaoESImp bd = new BaseDaoESImp();
    TargetingChannel tc = new TargetingChannel();
    BoolQueryBuilder res = bd.createQueryForSingleValues(tc);
    String exp = "{\"bool\":{\"must\":[{\"match_all\":{\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}";
    assertEquals(exp, res.toString().replaceAll("\\s+", ""));
}
 
Example #19
Source Project: act-platform   Author: mnemonic-no   File: FactSearchManager.java    License: ISC License 5 votes vote down vote up
private QueryBuilder buildObjectsQuery(FactSearchCriteria criteria) {
  BoolQueryBuilder rootQuery = boolQuery();

  // Apply all simple filter queries on Objects. It's not necessary to wrap them inside a nested query because the
  // query is executed inside a nested aggregation which has direct access to the nested documents.
  if (!CollectionUtils.isEmpty(criteria.getObjectID())) {
    rootQuery.filter(termsQuery("objects.id", toString(criteria.getObjectID())));
  }

  if (!CollectionUtils.isEmpty(criteria.getObjectTypeID())) {
    rootQuery.filter(termsQuery("objects.typeID", toString(criteria.getObjectTypeID())));
  }

  if (!CollectionUtils.isEmpty(criteria.getObjectValue())) {
    rootQuery.filter(termsQuery("objects.value", criteria.getObjectValue()));
  }

  // Apply keyword search on Object values if necessary.
  Set<FactSearchCriteria.KeywordFieldStrategy> objectFieldStrategy = onlyObjectFieldStrategy(criteria);
  if (!StringUtils.isBlank(criteria.getKeywords()) && !CollectionUtils.isEmpty(objectFieldStrategy)) {
    // Values are indexed differently. Avoid errors by setting 'lenient' to true.
    applyFieldStrategy(rootQuery, field -> simpleQueryStringQuery(criteria.getKeywords()).field(field).lenient(true),
            objectFieldStrategy, criteria.getKeywordMatchStrategy());
  }

  return rootQuery;
}
 
Example #20
Source Project: frostmourne   Author: AutohomeCorp   File: EsRestClientContainer.java    License: MIT License 5 votes vote down vote up
public long totalCount(BoolQueryBuilder boolQueryBuilder, String[] indices) throws IOException {
    CountRequest countRequest = new CountRequest(indices);
    SearchSourceBuilder countSourceBuilder = new SearchSourceBuilder();
    countSourceBuilder.query(boolQueryBuilder);
    countRequest.source(countSourceBuilder);

    CountResponse countResponse = this.fetchHighLevelClient().count(countRequest, RequestOptions.DEFAULT);
    return countResponse.getCount();
}
 
Example #21
Source Project: youkefu   Author: zhangyanbo2007   File: EkmKnowledgeCollectRepositoryImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<EkmKnowledgeCollect> findByCreaterAndKnowledgeidAndOrgi(String creater, String knowledgeid,
		String orgi) {
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	BoolQueryBuilder bq = QueryBuilders.boolQuery() ; 
	bq.must(QueryBuilders.termQuery("creater", creater)) ;
	bq.must(QueryBuilders.termQuery("knowledgeid", knowledgeid)) ;
	bq.must(QueryBuilders.termQuery("orgi", orgi)) ;
	boolQueryBuilder.must(bq); 
	
	return this.proccessQuery(boolQueryBuilder);
}
 
Example #22
Source Project: youkefu   Author: zhangyanbo2007   File: SearchTools.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param orgi
 * @param agent
 * @param p
 * @param ps
 * @return
 */
public static PageImpl<UKDataBean> aisearch(String orgi , int p, int ps){
	BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
	queryBuilder.must(termQuery("orgi", orgi)) ;
	queryBuilder.must(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.NOTCALL.toString())) ;
	
	queryBuilder.must(termQuery("validresult", "valid")) ;
	queryBuilder.must(termQuery("status", UKDataContext.NamesDisStatusType.DISAI.toString())) ;
	
	return search(queryBuilder, p, ps);
}
 
Example #23
Source Project: blue-marlin   Author: Futurewei-io   File: TBRDaoESImp.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * This method returns the aggregation value of tbr documents.
 *
 * @param boolQueryBuilder
 * @return
 * @throws IOException
 * @throws JSONException
 */
private double getSumForQuery(BoolQueryBuilder boolQueryBuilder) throws IOException
{
    SearchRequest searchRequest;
    ESResponse esResponse;
    double ratio = 0;

    SumAggregationBuilder aggregationBuilders = new SumAggregationBuilder("sum").script(
            new Script(genericScriptReader(Paths.get(TBR_KEYS_TOTAL))));

    searchRequest = new SearchRequest(tbrIndex).source(
            new SearchSourceBuilder().query(boolQueryBuilder).size(0).
                    aggregation(aggregationBuilders));

    esResponse = esclient.search(searchRequest);
    Map<String, Object> map = esResponse.getSourceMap();

    if (map.containsKey("value"))
    {
        ratio = Double.parseDouble(map.get("value").toString());
    }
    else
    {
        LOGGER.warn("Result of getSumForQuery() does not contain key \"value!\"");
    }
    return ratio;
}
 
Example #24
Source Project: blue-marlin   Author: Futurewei-io   File: TestBaseDaoESImp.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void createAndQueryForMultiValuePlusAgeGender() {
    BaseDaoESImp bd = new BaseDaoESImp();
    TargetingChannel tc = new TargetingChannel();
    List<TargetingChannel> list = new ArrayList();
    list.add(tc);
    TargetingChannel tc2 = new TargetingChannel();
    tc2.setG(Arrays.asList("G"));
    list.add(tc2);
    BoolQueryBuilder res = bd.createAndQueryForMultiValuePlusAgeGender(list);
    String exp = "{\"bool\":{\"must\":[{\"bool\":{\"must\":[{\"bool\":{\"should\":[{\"match_phrase\":{\"data.g\":{\"query\":\"G\",\"slop\":0,\"zero_terms_query\":\"NONE\",\"boost\":1.0}}}],"
            + "\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}],\"adjust_pure_negative\":true,\"boost\":1.0}}";
    assertEquals(exp, res.toString().replaceAll("\\s+", ""));
}
 
Example #25
Source Project: 21-points   Author: mraible   File: PreferencesResource.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * SEARCH  /_search/preferences?query=:query : search for the preferences corresponding
 * to the query.
 *
 * @param query the query of the preferences search
 * @return the result of the search
 */
@GetMapping("/_search/preferences")
@Timed
public List<Preferences> searchPreferences(@RequestParam String query) {
    log.debug("REST request to search Preferences for query {}", query);
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(queryStringQuery(query));
    if (SecurityUtils.isAuthenticated() && !SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)) {
        queryBuilder = queryBuilder.filter(matchQuery("user.login",
            SecurityUtils.getCurrentUserLogin().orElse("")));
    }
    return StreamSupport
        .stream(preferencesSearchRepository.search(queryBuilder).spliterator(), false)
        .collect(Collectors.toList());
}
 
Example #26
Source Project: albert   Author: pospospos2007   File: ChnlThemeSearchService.java    License: MIT License 5 votes vote down vote up
private BoolQueryBuilder createFilter(ThemeSearchParam param){
		BoolQueryBuilder builder = QueryBuilders.boolQuery();
	
		this.addThemeQuery(param.getTheme(), builder);
		//默认查已发布的数据
//		addDraftFlagFilter(param.getDraftFlag()==null?Constants.PostStatus.Approved:param.getDraftFlag(), builder);
		//      addDelFlagFilter(param.getDelFlag()==null?Constants.IS_DELFLAG_TYPE.NORMAL:param.getDelFlag(),builder);
//		addApprvlStatusFilter(param.getApprvlStatus()==null?Constants.ApprvlStatus.Approved:param.getApprvlStatus(), builder);
		
		
		return builder;
	}
 
Example #27
Source Project: vind   Author: RBMHTechnology   File: ElasticQueryBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public static QueryBuilder buildFilterQuery(Filter filter, DocumentFactory factory, String context) {
    final BoolQueryBuilder filterQuery = QueryBuilders.boolQuery();
    // Add base doc type filter
    filterQuery.must(QueryBuilders.termQuery(FieldUtil.TYPE, factory.getType()));
    Optional.ofNullable(filter)
            .ifPresent(vindFilter -> {
                filterQuery.must(filterMapper(vindFilter, factory, context));
            });
    return filterQuery;

}
 
Example #28
Source Project: youkefu   Author: zhangyanbo2007   File: EkmKnowledgeCollectRepositoryImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<EkmKnowledgeCollect> findByIdAndOrgi(String id, String orgi) {
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	BoolQueryBuilder bq = QueryBuilders.boolQuery() ; 
	bq.must(QueryBuilders.termQuery("id", id)) ;
	bq.must(QueryBuilders.termQuery("orgi", orgi)) ;
	boolQueryBuilder.must(bq); 
	
	return this.proccessQuery(boolQueryBuilder);
}
 
Example #29
Source Project: jetlinks-community   Author: jetlinks   File: QueryParamTranslator.java    License: Apache License 2.0 5 votes vote down vote up
public static SearchSourceBuilder convertSearchSourceBuilder(QueryParam queryParam, ElasticSearchIndexMetadata metadata) {
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    if (queryParam.isPaging()) {
        sourceBuilder.from(queryParam.getPageIndex() * queryParam.getPageSize());
        sourceBuilder.size(queryParam.getPageSize());
    }
    for (Sort sort : queryParam.getSorts()) {
        if (!StringUtils.isEmpty(sort.getName())) {
            sourceBuilder.sort(sort.getName(), SortOrder.fromString(sort.getOrder()));
        }
    }
    BoolQueryBuilder queryBuilders = QueryBuilders.boolQuery();
    Consumer<Term> paramConverter = doNotingParamConverter;
    if (metadata != null) {
        paramConverter = t -> {
            if (StringUtils.isEmpty(t.getColumn())) {
                return;
            }
            PropertyMetadata property = metadata.getProperty(t.getColumn());
            if (null != property) {
                DataType type = property.getValueType();
                converter.getOrDefault(type.getId(), defaultDataTypeConverter).accept(type, t);
            }
        };
    }
    for (Term term : queryParam.getTerms()) {
        linkTypeParser.process(term, paramConverter, queryBuilders);
    }
    return sourceBuilder.query(queryBuilders);
}
 
Example #30
Source Project: fess   Author: codelibs   File: BsBoostDocumentRuleCQ.java    License: Apache License 2.0 4 votes vote down vote up
public void setUrlExpr_NotEqual(String urlExpr, ConditionOptionCall<BoolQueryBuilder> opLambda) {
    setUrlExpr_NotTerm(urlExpr, opLambda);
}