org.elasticsearch.index.query.FilterBuilders Java Examples

The following examples show how to use org.elasticsearch.index.query.FilterBuilders. 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: AST_Search.java    From elasticsearch-rest-command with The Unlicense 6 votes vote down vote up
private static FilterBuilder fromValueType(String field, String value, int valueType){
	if(value.contains("*") || value.contains("?")){
		if( value.length() > 1 && value.indexOf('*') == (value.length()-1))
			return FilterBuilders.prefixFilter(field, value.substring(0, value.length()-1));
		else
			return FilterBuilders.queryFilter(QueryBuilders.wildcardQuery(field, value));
	}
	
	switch(valueType){
	case AST_TermExpression.TERM:
		return FilterBuilders.termFilter(field, value);
	case AST_TermExpression.PHRASE:
		/*for(byte b: value.getBytes()){
			System.out.printf("0x%02X ", b);		        
		}*/
		return FilterBuilders.queryFilter(QueryBuilders.matchPhraseQuery(field, value));
	}
	return null;
}
 
Example #2
Source File: ShellScope.java    From elasticshell with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new <code>ShellScope</code> given the actual scope object
 * @param scope the actual scope object that depends on the engine in use
 */
ShellScope(Scope scope, ResourceRegistry resourceRegistry) {
    this.scope = scope;
    this.resourceRegistry = resourceRegistry;
    registerJavaClass(Requests.class);
    registerJavaClass(SearchSourceBuilder.class);
    registerJavaClass(QueryBuilders.class);
    registerJavaClass(FilterBuilders.class);
    registerJavaClass(SortBuilders.class);
    registerJavaClass(FacetBuilders.class);
    registerJavaClass(RescoreBuilder.class);
    registerJavaClass(SuggestBuilder.class);
    registerJavaClass(AliasAction.class);
    registerJavaClass(HttpParameters.class);
    registerJavaClass(AllocateAllocationCommand.class);
    registerJavaClass(CancelAllocationCommand.class);
    registerJavaClass(MoveAllocationCommand.class);
    registerJavaClass(ShardId.class);
}
 
Example #3
Source File: ProductQueryServiceImpl.java    From searchanalytics-bigdata with MIT License 6 votes vote down vote up
private FilterBuilder getBaseFilterBuilder(final String facetName,
		final String fieldValue) {
	if (facetName
			.startsWith(SearchFacetName.SEARCH_FACET_TYPE_FACET_PREFIX)) {
		return getTermFilter(
				SearchFacetName.CATEGORIES_FIELD_PREFIX
						+ facetName
						+ "."
						+ SearchDocumentFieldName.FACETFILTER
								.getFieldName(),
				fieldValue.toLowerCase());
	} else if (facetName.startsWith(SearchFacetName.PRODUCT_PRICE_RANGE
			.getCode())) {
		return FilterBuilders
				.rangeFilter(SearchDocumentFieldName.PRICE.getFieldName())
				.includeLower(true).includeUpper(false)
				.from(fieldValue.split("-")[0])
				.to(fieldValue.split("-")[1]);
	} else {
		return FilterBuilders.termFilter(facetName, fieldValue);
	}
	// return null;
}
 
Example #4
Source File: EsInstanceStore.java    From soundwave with Apache License 2.0 6 votes vote down vote up
@Override
public Iterator<EsQueryResult> getRunningInstancesWithFields(String[] includeFields)
    throws Exception {

  QueryBuilder queryBuilder = QueryBuilders.filteredQuery(
      QueryBuilders.termQuery("state", "running"),
      FilterBuilders.termFilter("state", "running"));

  ScrollableResponse<List<EsQueryResult>> response = this.retrieveScrollByQuery(queryBuilder,
      includeFields, BATCHSIZE,
      str -> insertMapper.readValue(str, EsQueryResult.class));

  EsIterator<EsQueryResult>
      iterator =
      new EsIterator<>(response, r -> scrollNext(r.getContinousToken(),
          str -> insertMapper.readValue(str, EsQueryResult.class)));

  return iterator;
}
 
Example #5
Source File: GitHubRiver.java    From elasticsearch-river-github with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the creation data of the single newest entry.
 *
 * @return ISO8601 formatted time of most recent entry, or null on empty or error.
 */
private String getMostRecentEntry() {
    long totalEntries = client.prepareCount(index).setQuery(matchAllQuery()).execute().actionGet().getCount();
    if (totalEntries > 0) {
        FilteredQueryBuilder updatedAtQuery = QueryBuilders
                .filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.existsFilter("created_at"));
        FieldSortBuilder updatedAtSort = SortBuilders.fieldSort("created_at").order(SortOrder.DESC);

        SearchResponse response = client.prepareSearch(index)
                .setQuery(updatedAtQuery)
                .addSort(updatedAtSort)
                .setSize(1)
                .execute()
                .actionGet();

        String createdAt = (String) response.getHits().getAt(0).getSource().get("created_at");
        logger.debug("Most recent event was created at {}", createdAt);
        return createdAt;
    } else {
        // getData will get all data on a null.
        logger.info("No existing entries, assuming first run");
        return null;
    }
}
 
Example #6
Source File: IndexService.java    From disthene-reader with MIT License 6 votes vote down vote up
public String getSearchPathsAsString(String tenant, String regEx, int limit) {
    SearchResponse response = client.prepareSearch(indexConfiguration.getIndex())
            .setScroll(new TimeValue(indexConfiguration.getTimeout()))
            .setSize(limit)
            .setQuery(QueryBuilders.filteredQuery(
                    QueryBuilders.regexpQuery("path", regEx),
                    FilterBuilders.termFilter("tenant", tenant)))
            .addField("path")
            .execute().actionGet();

    List<String> paths = new ArrayList<>();
    for (SearchHit hit : response.getHits()) {
        paths.add((String) hit.field("path").getValue());
    }

    return Joiner.on(",").skipNulls().join(paths);
}
 
Example #7
Source File: ProductQueryServiceImpl.java    From elasticsearch-tutorial with MIT License 6 votes vote down vote up
private FilterBuilder getBaseFilterBuilder(String facetName, String fieldValue)
    {
        if(facetName.startsWith(SearchFacetName.SEARCH_FACET_TYPE_FACET_PREFIX))
        {
            return getTermFilter(SearchFacetName.CATEGORIES_FIELD_PREFIX + facetName + "." + SearchDocumentFieldName.FACETFILTER.getFieldName(), fieldValue.toLowerCase());
        }
        else if(facetName.startsWith(SearchFacetName.PRODUCT_PRICE_RANGE.getCode()))
        {
            return FilterBuilders.rangeFilter(SearchDocumentFieldName.PRICE.getFieldName()).includeLower(true).includeUpper(false).from(fieldValue.split("-")[0]).to(fieldValue.split("-")[1]);
        }
        else
        {
            return FilterBuilders.termFilter(facetName, fieldValue);
        }
//        return null;
    }
 
Example #8
Source File: EsQueryBuilder.java    From es-service-parent with Apache License 2.0 6 votes vote down vote up
/**
 * 获取过滤器
 * 
 * @return
 */
public BoolFilterBuilder makeFilterBuilder(SearchRequestBuilder srb) {
    // 过滤器
    BoolFilterBuilder boolFilterBuilder = FilterBuilders.boolFilter();
    if (request.getSafeFilterScript().size() > 0) {
        ScriptFilterBuilder filter;
        for (EsFilterScript filterScript : request.getSafeFilterScript()) {
            filter = FilterBuilders.scriptFilter(filterScript.getScript());
            if (filterScript.getScriptParams() != null
                    && filterScript.getScriptParams().size() > 0) {
                filter.params(filterScript.getScriptParams());
            }
            boolFilterBuilder.should(filter);
        }
        srb.setPostFilter(boolFilterBuilder);
    }
    return boolFilterBuilder;
}
 
Example #9
Source File: EsStore.java    From soundwave with Apache License 2.0 6 votes vote down vote up
protected <E extends EsDocument> ScrollableResponse<List<E>> retrieveScrollByField(
    String field, Object value, String[] includeFields, int size,
    ThrowingFunction<String, E> createFunc)
    throws Exception {

  Preconditions.checkArgument(size > 0);

  SearchRequestBuilder builder = esClient.prepareSearch()
      .setIndices(getIndexName()).setTypes(getDocTypeName())
      .setScroll(new TimeValue(SCROLLDEFAULTTIMEOUT))
      .setSize(size)
      .setQuery(QueryBuilders
          .filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.termsFilter(field, value)))
      .setFetchSource(includeFields, null).setVersion(true);

  SearchResponse response = builder.execute().actionGet();
  return convertToScrollableResponse(response, createFunc);
}
 
Example #10
Source File: AnalyticsServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 6 votes vote down vote up
private long getTraceCompletionCount(String tenantId, Criteria criteria, boolean onlyFaulty) {
    String index = client.getIndex(tenantId);
    if (!refresh(index)) {
        return 0;
    }

    BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, CompletionTime.class);
    SearchRequestBuilder request = getTraceCompletionRequest(index, criteria, query, 0);

    if (onlyFaulty) {
        FilterBuilder filter = FilterBuilders.queryFilter(QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery(ElasticsearchUtil.PROPERTIES_NAME_FIELD, Constants.PROP_FAULT)));
        request.setPostFilter(FilterBuilders.nestedFilter("properties", filter));
    }

    SearchResponse response = request.execute().actionGet();
    if (response.isTimedOut()) {
        msgLog.warnQueryTimedOut();
        return 0;
    } else {
        return response.getHits().getTotalHits();
    }
}
 
Example #11
Source File: EsStore.java    From soundwave with Apache License 2.0 5 votes vote down vote up
protected SearchResponse retrieveByField(String field, Object value, String[] includeFields)
    throws Exception {
  // The query here is using the exact value match.
  // https://www.elastic.co/guide/en/elasticsearch/guide/1.x/_finding_exact_values.html
  SearchRequestBuilder builder = esClient.prepareSearch()
      .setIndices(getIndexName()).setTypes(getDocTypeName())
      .setQuery(QueryBuilders
          .filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.termsFilter(field, value)))
      .setFetchSource(includeFields, null).setVersion(true);

  return builder.execute().actionGet();
}
 
Example #12
Source File: IndexService.java    From disthene-reader with MIT License 5 votes vote down vote up
public String getPathsAsJsonArray(String tenant, String wildcard) throws TooMuchDataExpectedException {
    String regEx = WildcardUtil.getPathsRegExFromWildcard(wildcard);

    SearchResponse response = client.prepareSearch(indexConfiguration.getIndex())
            .setScroll(new TimeValue(indexConfiguration.getTimeout()))
            .setSize(indexConfiguration.getScroll())
            .setQuery(QueryBuilders.filteredQuery(
                    QueryBuilders.regexpQuery("path", regEx),
                    FilterBuilders.termFilter("tenant", tenant)))
            .execute().actionGet();

    // if total hits exceeds maximum - abort right away returning empty array
    if (response.getHits().totalHits() > indexConfiguration.getMaxPaths()) {
        logger.debug("Total number of paths exceeds the limit: " + response.getHits().totalHits());
        throw new TooMuchDataExpectedException("Total number of paths exceeds the limit: " + response.getHits().totalHits() + " (the limit is " + indexConfiguration.getMaxPaths() + ")");
    }

    List<String> paths = new ArrayList<>();
    while (response.getHits().getHits().length > 0) {
        for (SearchHit hit : response.getHits()) {
            paths.add(hit.getSourceAsString());
        }
        response = client.prepareSearchScroll(response.getScrollId())
                .setScroll(new TimeValue(indexConfiguration.getTimeout()))
                .execute().actionGet();
    }

    return "[" + joiner.join(paths) + "]";
}
 
Example #13
Source File: ElasticsearchUtil.java    From hawkular-apm with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns a filter associated with the supplied criteria.
 *
 * @param criteria The criteria
 * @return The filter, or null if not relevant
 */
public static FilterBuilder buildFilter(Criteria criteria) {
    if (criteria.getTransaction() != null && criteria.getTransaction().trim().isEmpty()) {
        return FilterBuilders.missingFilter(TRANSACTION_FIELD);
    }
    return null;
}
 
Example #14
Source File: AST_Search.java    From elasticsearch-rest-command with The Unlicense 5 votes vote down vote up
private static QueryBuilder fromValueTypeQ(String field, String value, int valueType){
	if(value.contains("*") || value.contains("?")){
		if( value.length() > 1 && value.indexOf('*') == (value.length()-1))
			return QueryBuilders.prefixQuery(field, value.substring(0, value.length()-1));
		else
			return QueryBuilders.wildcardQuery(field, value);
	}else if(value.equalsIgnoreCase("")){
		
		return QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.boolFilter()
				.should(FilterBuilders.scriptFilter("doc['"+field+"'].value.size() == 0"))					
				.should(FilterBuilders.missingFilter(field).nullValue(true).existence(true)));
	}
	//全部使用对短语进行分词后再搜索
	return QueryBuilders.matchPhraseQuery(field, value);
	
	
	/*
	switch(valueType){
	
	case AST_TermExpression.TERM:
		return QueryBuilders.termQuery(field, value);
	case AST_TermExpression.PHRASE:			
		return QueryBuilders.matchPhraseQuery(field, value);
	}
	
	
	return null;
	*/
}
 
Example #15
Source File: SearchRequestBuilderStrategy.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Create our filter builder.  We need to restrict our results on edge search, as well as on types, and any filters
 * that came from the grammar.
 */
private FilterBuilder createFilterBuilder( final SearchEdge searchEdge, final QueryVisitor visitor,
                                           final SearchTypes searchTypes ) {
    String context = createContextName( applicationScope, searchEdge );


    // Add our filter for context to our query for fast execution.
    // Fast because it utilizes bitsets internally. See this post for more detail.
    // http://www.elasticsearch.org/blog/all-about-elasticsearch-filter-bitsets/

    // TODO evaluate performance when it's an all query.
    // Do we need to put the context term first for performance?

    //make sure we have entity in the context
    BoolFilterBuilder boolQueryFilter = FilterBuilders.boolFilter();

    //add our edge search
    boolQueryFilter.must( FilterBuilders.termFilter( IndexingUtils.EDGE_SEARCH_FIELDNAME, context ) );


    /**
     * For the types the user specified, add them to an OR so 1 of them must match
     */
    final String[] sourceTypes = searchTypes.getTypeNames( applicationScope );


    if ( sourceTypes.length > 0 ) {
        final FilterBuilder[] typeTerms = new FilterBuilder[sourceTypes.length];

        for ( int i = 0; i < sourceTypes.length; i++ ) {
            typeTerms[i] = FilterBuilders.termFilter( IndexingUtils.ENTITY_TYPE_FIELDNAME, sourceTypes[i] );
        }

        //add all our types, 1 type must match per query
        boolQueryFilter.must( FilterBuilders.orFilter( typeTerms ) );
    }

    //if we have a filter from our visitor, add it

    Optional<FilterBuilder> queryBuilder = visitor.getFilterBuilder();

    if ( queryBuilder.isPresent() ) {
        boolQueryFilter.must( queryBuilder.get() );
    }

    return boolQueryFilter;
}
 
Example #16
Source File: HistoryEventsIndexingTest.java    From camunda-bpm-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
  public void testIndexingSingleInvoice() throws IOException {
    HashMap<String,ProcessDataContainer> processesById = TestDataGenerator.startInvoiceProcess(processEngineRule.getProcessEngine(), 1);

    String[] pids = processesById.keySet().toArray(new String[0]);

    // elasticsearch //////////////////////////////

    flushAndRefresh();

    FilteredQueryBuilder query = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.termFilter("processInstanceId", pids[0]));

    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM)
        .setQuery(query);

    IoUtil.writeToFile(searchRequestBuilder.toString(), this.getClass().getSimpleName() + ".json", true);

    SearchResponse searchResponse = searchRequestBuilder.get();
    SearchHits hits = searchResponse.getHits();
    assertEquals(1, hits.totalHits());

    SearchHit hit = hits.getAt(0);
    assertEquals(pids[0], hit.getId());
    assertEquals(ES_DEFAULT_TYPE_NAME_CAMUNDA_BPM, hit.getType());

    Map<String,Object> source = hit.getSource();
    assertNotNull(source.get("startTime"));
    assertNotNull(source.get("endTime"));
    ArrayList variables = (ArrayList) source.get("variables");
    assertEquals(5, variables.size());
    ArrayList tasks = (ArrayList) source.get("tasks");
    assertEquals(9, tasks.size());
    ArrayList activities = (ArrayList) source.get("activities");
    assertEquals(19, activities.size());

    logger.info(searchResponse.toString());

//    for (SearchHit searchHit : searchResponse.getHits()) {
//      logger.info(searchHit.sourceAsString());
//    }
  }
 
Example #17
Source File: HistoryEventsMappingTest.java    From camunda-bpm-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
//  @ElasticsearchIndex(
//      indexName = ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM,
//      cleanAfter = true,
//      mappings = @ElasticsearchMapping(typeName = ES_DEFAULT_TYPE_NAME_CAMUNDA_BPM, properties = {
//          @Elasticsearch
//      }))
  public void testIndexingSingleInvoice() throws IOException {
    HashMap<String,ProcessDataContainer> processesById = TestDataGenerator.startInvoiceProcess(processEngineRule.getProcessEngine(), 1);

    String[] pids = processesById.keySet().toArray(new String[0]);

    // elasticsearch //////////////////////////////

    flushAndRefresh();

    showMappings(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM);

    FilteredQueryBuilder query = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.termFilter("processInstanceId", pids[0]));

    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM)
        .setQuery(query);

    IoUtil.writeToFile(searchRequestBuilder.toString(), this.getClass().getSimpleName() + ".json", true);

    SearchResponse searchResponse = searchRequestBuilder.get();
    SearchHits hits = searchResponse.getHits();
    assertEquals(1, hits.totalHits());


    SearchHit hit = hits.getAt(0);
    assertEquals(pids[0], hit.getId());
    assertEquals(ES_DEFAULT_TYPE_NAME_CAMUNDA_BPM, hit.getType());

    Map<String,Object> source = hit.getSource();
    assertNotNull(source.get("startTime"));
    assertNotNull(source.get("endTime"));
    ArrayList variables = (ArrayList) source.get("variables");
    assertEquals(5, variables.size());
    ArrayList tasks = (ArrayList) source.get("tasks");
    assertEquals(9, tasks.size());
    ArrayList activities = (ArrayList) source.get("activities");
    assertEquals(19, activities.size());

    logger.info(searchResponse.toString());

//    for (SearchHit searchHit : searchResponse.getHits()) {
//      logger.info(searchHit.sourceAsString());
//    }

    showMappings(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM);

    assertMappings(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM);
    // TODO: write assertions for mapping
  }
 
Example #18
Source File: SortBuilder.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Create a term filter for our sorts
 */
public static TermFilterBuilder sortPropertyTermFilter( final String propertyName ) {
    return FilterBuilders.termFilter( IndexingUtils.FIELD_NAME, propertyName );
}
 
Example #19
Source File: EsQueryVistor.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
public void visit( Equal op ) throws NoIndexException {
    final String name = op.getProperty().getValue().toLowerCase();
    final Object value = op.getLiteral().getValue();

    //special case so we support our '*' char with wildcard, also should work for uuids
    if ( value instanceof String || value instanceof UUID ) {
        String stringValue = ((value instanceof String) ? (String)value : value.toString()).toLowerCase().trim();

        // or field is just a string that does need a prefix us a query
        if ( stringValue.contains( "*" ) ) {

            //Because of our legacy behavior, where we match CCCC*, we need to use the unanalyzed string to ensure that
            //we start
            final WildcardQueryBuilder wildcardQuery =
                    QueryBuilders.wildcardQuery( IndexingUtils.FIELD_STRING_NESTED_UNANALYZED, stringValue );
            queryBuilders.push( fieldNameTerm( name, wildcardQuery ) );
            filterBuilders.push( NoOpFilterBuilder.INSTANCE );
            return;
        }

        // Usergrid query parser allows single quotes to be escaped in values
        if ( stringValue.contains("\\'")) {
            stringValue = stringValue.replace("\\'", "'");
        }

        //it's an exact match, use a filter
        final TermFilterBuilder termFilter =
                FilterBuilders.termFilter( IndexingUtils.FIELD_STRING_NESTED_UNANALYZED, stringValue );

        queryBuilders.push( NoOpQueryBuilder.INSTANCE );
        filterBuilders.push( fieldNameTerm( name, termFilter ) );

        return;
    }

    // assume all other types need prefix

    final TermFilterBuilder termQuery =
            FilterBuilders.termFilter(getFieldNameForType(value), sanitize(value));

    filterBuilders.push( fieldNameTerm( name, termQuery ) );

    queryBuilders.push( NoOpQueryBuilder.INSTANCE );
}
 
Example #20
Source File: EsQueryVistor.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
public void visit( LessThan op ) throws NoIndexException {
    final String name = op.getProperty().getValue().toLowerCase();
    final Object value = op.getLiteral().getValue();


    final RangeFilterBuilder termQuery =
            FilterBuilders.rangeFilter( getFieldNameForType( value ) ).lt(sanitize(value));


    queryBuilders.push( NoOpQueryBuilder.INSTANCE );

    //we do this by query, push empty

    filterBuilders.push( fieldNameTerm( name, termQuery ) );
}
 
Example #21
Source File: EsQueryVistor.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
public void visit( NotOperand op ) throws IndexException {

    //we need to know if we're the root entry for building our queries correctly
    final boolean rootNode = queryBuilders.empty() && filterBuilders.isEmpty();

    op.getOperation().visit( this );

    //push our not operation into our query

    final QueryBuilder notQueryBuilder = queryBuilders.pop();

    if ( use( notQueryBuilder ) ) {
        final QueryBuilder notQuery = QueryBuilders.boolQuery().mustNot(notQueryBuilder);
        queryBuilders.push( notQuery  );
    }
    else {
        queryBuilders.push( NoOpQueryBuilder.INSTANCE );
    }

    final FilterBuilder notFilterBuilder = filterBuilders.pop();

    //push the filter in
    if ( use( notFilterBuilder ) ) {

        final FilterBuilder notFilter = FilterBuilders.boolFilter().mustNot( notFilterBuilder ) ;

        //just the root node
        if(!rootNode) {
            filterBuilders.push( notFilter );
        }
        //not the root node, we have to select all to subtract from with the NOT statement
        else{
            final FilterBuilder selectAllFilter = FilterBuilders.boolFilter().must( FilterBuilders.matchAllFilter()) .must( notFilter );
            filterBuilders.push( selectAllFilter );
        }

    }
    else {
        filterBuilders.push( NoOpFilterBuilder.INSTANCE );
    }
}
 
Example #22
Source File: EsQueryVistor.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
public void visit( OrOperand op ) throws IndexException {

    op.getLeft().visit( this );
    op.getRight().visit( this );

    final QueryBuilder rightQuery = queryBuilders.pop();
    final FilterBuilder rightFilter = filterBuilders.pop();


    //get all the left
    final QueryBuilder leftQuery = queryBuilders.pop();
    final FilterBuilder leftFilter = filterBuilders.pop();


    final boolean useLeftQuery = use( leftQuery );
    final boolean useRightQuery = use(rightQuery);

    //push our boolean filters
    if ( useLeftQuery && useRightQuery ) {
        //when we issue an OR query in usergrid, 1 or more of the terms should match.  When doing bool query in ES, there is no requirement for more than 1 to match, where as in a filter more than 1 must match
        final BoolQueryBuilder qb = QueryBuilders.boolQuery().should( leftQuery ).should(rightQuery).minimumNumberShouldMatch(
            1);
        queryBuilders.push( qb );
    }
    else if ( useLeftQuery ) {
        queryBuilders.push( leftQuery );
    }
    else if ( useRightQuery ) {
        queryBuilders.push( rightQuery );
    }

    //put in an empty in case we're not the root.  I.E X or Y or Z
    else {
        queryBuilders.push( NoOpQueryBuilder.INSTANCE );
    }


    final boolean useLeftFilter = use( leftFilter );
    final boolean useRightFilter = use(rightFilter);

    //use left and right
    if ( useLeftFilter && useRightFilter ) {
        final BoolFilterBuilder fb = FilterBuilders.boolFilter().should( leftFilter ).should( rightFilter );
        filterBuilders.push( fb );
    }

    //only use left
    else if ( useLeftFilter ) {
        filterBuilders.push( leftFilter );
    }
    //only use right
    else if ( useRightFilter ) {
        filterBuilders.push( rightFilter );
    }
    //put in an empty in case we're not the root.  I.E X or Y or Z
    else {
        filterBuilders.push( NoOpFilterBuilder.INSTANCE );
    }
}
 
Example #23
Source File: EsQueryVistor.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
public void visit( AndOperand op ) throws IndexException {


    op.getLeft().visit( this );
    op.getRight().visit( this );

    //get all the right
    final QueryBuilder rightQuery = queryBuilders.pop();
    final FilterBuilder rightFilter = filterBuilders.pop();


    //get all the left
    final QueryBuilder leftQuery = queryBuilders.pop();
    final FilterBuilder leftFilter = filterBuilders.pop();


    //push our boolean filters


    final boolean useLeftQuery = use( leftQuery );
    final boolean useRightQuery = use( rightQuery );

    /**
     * We use a left and a right, add our boolean query
     */
    if ( useLeftQuery && useRightQuery ) {
        final BoolQueryBuilder qb = QueryBuilders.boolQuery().must(leftQuery).must(rightQuery);
        queryBuilders.push( qb );
    }
    //only use the left
    else if ( useLeftQuery ) {
        queryBuilders.push( leftQuery );
    }
    //only use the right
    else if ( useRightQuery ) {
        queryBuilders.push( rightQuery );
    }
    //put in an empty in case we're not the root.  I.E X and Y and Z
    else {
        queryBuilders.push( NoOpQueryBuilder.INSTANCE );
    }

    //possibly use neither if the is a no-op


    final boolean useLeftFilter = use( leftFilter );
    final boolean useRightFilter = use( rightFilter );

    //use left and right
    if ( useLeftFilter && useRightFilter ) {
        final BoolFilterBuilder fb = FilterBuilders.boolFilter().must(leftFilter).must(rightFilter);
        filterBuilders.push( fb );
    }

    //only use left
    else if ( useLeftFilter ) {
        filterBuilders.push( leftFilter );
    }
    //only use right
    else if ( useRightFilter ) {
        filterBuilders.push( rightFilter );
    }
    //push in a no-op in case we're not the root   I.E X and Y and Z
    else {
        filterBuilders.push( NoOpFilterBuilder.INSTANCE );
    }
}
 
Example #24
Source File: ProductQueryServiceImpl.java    From searchanalytics-bigdata with MIT License 4 votes vote down vote up
private TermFilterBuilder getTermFilter(final String fieldName,
		final String fieldValue) {
	return FilterBuilders.termFilter(fieldName, fieldValue);
}
 
Example #25
Source File: ProductQueryServiceImpl.java    From searchanalytics-bigdata with MIT License 4 votes vote down vote up
private FilterBuilder getSpecificationsFacetFilterBuilder(
		final SearchCriteria searchCriteria) {
	// This is used in case you want to search based on separate
	// resolution/memory facets
	final Map<String, List<String>> multiSelectFilters = searchCriteria
			.getMultiSelectFilters();
	final List<String> resolutionFilters = new ArrayList<String>();
	final List<String> memoryFilters = new ArrayList<String>();
	for (final Entry<String, List<String>> entry : multiSelectFilters
			.entrySet()) {
		if (entry.getKey().equals(
				SearchFacetName.SPECIFICATION_RESOLUTION.getCode())) {
			resolutionFilters.addAll(entry.getValue());
		} else if (entry.getKey().equals(
				SearchFacetName.SPECIFICATION_MEMORY.getCode())) {
			memoryFilters.addAll(entry.getValue());
		}
	}
	if (resolutionFilters.size() == 0 && memoryFilters.size() == 0) {
		return FilterBuilders.queryFilter(QueryBuilders.matchAllQuery());
	}
	final AndFilterBuilder andResolutionAndMemoryFilterBuilder = FilterBuilders
			.andFilter();
	if (resolutionFilters.size() > 0) {
		final OrFilterBuilder OrResolutionFilterBuilder = FilterBuilders
				.orFilter();
		for (final String resolution : resolutionFilters) {
			OrResolutionFilterBuilder.add(FilterBuilders.termFilter(
					SearchDocumentFieldName.RESOLUTION.getFieldName(),
					resolution));
		}
		andResolutionAndMemoryFilterBuilder.add(OrResolutionFilterBuilder);
	}
	if (memoryFilters.size() > 0) {
		final OrFilterBuilder OrMemoryFilterBuilder = FilterBuilders
				.orFilter();
		for (final String memory : memoryFilters) {
			OrMemoryFilterBuilder.add(FilterBuilders.termFilter(
					SearchDocumentFieldName.MEMORY.getFieldName(), memory));
		}
		andResolutionAndMemoryFilterBuilder.add(OrMemoryFilterBuilder);
	}
	// else if(specifications.size() == 1)
	// {
	// Specification specification =
	// searchCriteria.getSpecifications().get(0);
	//
	// FilterBuilder filterBuilder = FilterBuilders.andFilter(FilterBuilders
	// .termFilter(SearchDocumentFieldName.RESOLUTION.getFieldName(),
	// specification.getResolution()),
	// FilterBuilders.termFilter(SearchDocumentFieldName.MEMORY.getFieldName(),
	// specification.getMemory()));
	//
	// specificationFilterBuilder = filterBuilder;
	// }
	// else
	// {
	// specificationFilterBuilder = FilterBuilders.matchAllFilter();
	// }
	return andResolutionAndMemoryFilterBuilder;
}
 
Example #26
Source File: IndexService.java    From disthene-reader with MIT License 4 votes vote down vote up
public String getPathsWithStats(String tenant, String wildcard) throws TooMuchDataExpectedException {
    String regEx = WildcardUtil.getPathsRegExFromWildcard(wildcard);

    SearchResponse response = client.prepareSearch(indexConfiguration.getIndex())
            .setScroll(new TimeValue(indexConfiguration.getTimeout()))
            .setSize(indexConfiguration.getScroll())
            .setQuery(QueryBuilders.filteredQuery(
                    QueryBuilders.regexpQuery("path", regEx),
                    FilterBuilders.termFilter("tenant", tenant)))
            .addField("path")
            .execute().actionGet();

    // if total hits exceeds maximum - abort right away returning empty array
    if (response.getHits().totalHits() > indexConfiguration.getMaxPaths()) {
        logger.debug("Total number of paths exceeds the limit: " + response.getHits().totalHits());
        throw new TooMuchDataExpectedException("Total number of paths exceeds the limit: " + response.getHits().totalHits() + " (the limit is " + indexConfiguration.getMaxPaths() + ")");
    }

    List<String> paths = new ArrayList<>();
    while (response.getHits().getHits().length > 0) {
        for (SearchHit hit : response.getHits()) {
            paths.add(String.valueOf(hit.field("path").getValue()));
        }
        response = client.prepareSearchScroll(response.getScrollId())
                .setScroll(new TimeValue(indexConfiguration.getTimeout()))
                .execute().actionGet();
    }

    Collections.sort(paths);

    // we got the paths. Now let's get the counts
    List<String> result = new ArrayList<>();
    for (String path : paths) {
        CountResponse countResponse = client.prepareCount(indexConfiguration.getIndex())
                .setQuery(QueryBuilders.filteredQuery(
                        QueryBuilders.regexpQuery("path", path + "\\..*"),
                        FilterBuilders.boolFilter()
                            .must(FilterBuilders.termFilter("tenant", tenant))
                            .must(FilterBuilders.termFilter("leaf", true))))
                .execute().actionGet();
        long count = countResponse.getCount();
        result.add("{\"path\": \"" + path + "\",\"count\":" + countResponse.getCount() + "}");
    }


    return "[" + joiner.join(result) + "]";
}
 
Example #27
Source File: AnalyticsServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 4 votes vote down vote up
@Override
public List<TimeseriesStatistics> getTraceCompletionTimeseriesStatistics(String tenantId, Criteria criteria, long interval) {
    String index = client.getIndex(tenantId);
    if (!refresh(index)) {
        return null;
    }

    StatsBuilder statsBuilder = AggregationBuilders
            .stats("stats")
            .field(ElasticsearchUtil.DURATION_FIELD);

    // TODO: HWKAPM-679 (related to HWKAPM-675), faults now recorded as properties. However this
    // current results in the fault count being an actual count of fault properties, where
    // the original intention of the fault count is the number of txns that have been affected
    // by a fault.
    FilterAggregationBuilder faultCountBuilder = AggregationBuilders
            .filter("faults")
            .filter(FilterBuilders.queryFilter(QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchQuery(ElasticsearchUtil.PROPERTIES_NAME_FIELD, Constants.PROP_FAULT))));

    NestedBuilder nestedFaultCountBuilder = AggregationBuilders
            .nested("nested")
            .path(ElasticsearchUtil.PROPERTIES_FIELD)
            .subAggregation(faultCountBuilder);

    DateHistogramBuilder histogramBuilder = AggregationBuilders
            .dateHistogram("histogram")
            .interval(interval)
            .field(ElasticsearchUtil.TIMESTAMP_FIELD)
            .subAggregation(statsBuilder)
            .subAggregation(nestedFaultCountBuilder);

    BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, CompletionTime.class);
    SearchRequestBuilder request = getTraceCompletionRequest(index, criteria, query, 0)
            .addAggregation(histogramBuilder);

    SearchResponse response = getSearchResponse(request);
    DateHistogram histogram = response.getAggregations().get("histogram");

    return histogram.getBuckets().stream()
            .map(AnalyticsServiceElasticsearch::toTimeseriesStatistics)
            .collect(Collectors.toList());
}
 
Example #28
Source File: IndexService.java    From disthene-reader with MIT License 4 votes vote down vote up
public List<String> getPaths(String tenant, List<String> wildcards) throws TooMuchDataExpectedException {
    List<String> regExs = new ArrayList<>();
    List<String> result = new ArrayList<>();

    for(String wildcard : wildcards) {
        if (WildcardUtil.isPlainPath(wildcard)) {
            result.add(wildcard);
        } else {
            regExs.add(WildcardUtil.getPathsRegExFromWildcard(wildcard));
        }
    }

    logger.debug("getPaths plain paths: " + result.size() + ", wildcard paths: " + regExs.size());

    if (regExs.size() > 0) {
        String regEx = Joiner.on("|").skipNulls().join(regExs);

        SearchResponse response = client.prepareSearch(indexConfiguration.getIndex())
                .setScroll(new TimeValue(indexConfiguration.getTimeout()))
                .setSize(indexConfiguration.getScroll())
                .setQuery(QueryBuilders.filteredQuery(QueryBuilders.regexpQuery("path", regEx),
                        FilterBuilders.termFilter("tenant", tenant)))
                .addField("path")
                .execute().actionGet();

        // if total hits exceeds maximum - abort right away returning empty array
        if (response.getHits().totalHits() > indexConfiguration.getMaxPaths()) {
            logger.debug("Total number of paths exceeds the limit: " + response.getHits().totalHits());
            throw new TooMuchDataExpectedException("Total number of paths exceeds the limit: " + response.getHits().totalHits() + " (the limit is " + indexConfiguration.getMaxPaths() + ")");
        }

        while (response.getHits().getHits().length > 0) {
            for (SearchHit hit : response.getHits()) {
                result.add((String) hit.field("path").getValue());
            }
            response = client.prepareSearchScroll(response.getScrollId())
                    .setScroll(new TimeValue(indexConfiguration.getTimeout()))
                    .execute().actionGet();
        }
    }

    return result;
}
 
Example #29
Source File: ProductQueryServiceImpl.java    From elasticsearch-tutorial with MIT License 4 votes vote down vote up
private TermFilterBuilder getTermFilter(String fieldName, String fieldValue)
{
    return FilterBuilders.termFilter(fieldName, fieldValue);
}
 
Example #30
Source File: AnalyticsServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 4 votes vote down vote up
@Override
public List<TimeseriesStatistics> getEndpointResponseTimeseriesStatistics(String tenantId, Criteria criteria, long interval) {
    String index = client.getIndex(tenantId);
    if (!refresh(index)) {
        return null;
    }

    StatsBuilder statsBuilder = AggregationBuilders
            .stats("stats")
            .field(ElasticsearchUtil.ELAPSED_FIELD);

    // TODO: HWKAPM-679 (related to HWKAPM-675), faults now recorded as properties. However this
    // current results in the fault count being an actual count of fault properties, where
    // the original intention of the fault count is the number of txns that have been affected
    // by a fault.
    FilterAggregationBuilder faultCountBuilder = AggregationBuilders
            .filter("faults")
            .filter(FilterBuilders.queryFilter(QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchQuery(ElasticsearchUtil.PROPERTIES_NAME_FIELD, Constants.PROP_FAULT))));

    NestedBuilder nestedFaultCountBuilder = AggregationBuilders
            .nested("nested")
            .path(ElasticsearchUtil.PROPERTIES_FIELD)
            .subAggregation(faultCountBuilder);

    DateHistogramBuilder histogramBuilder = AggregationBuilders
            .dateHistogram("histogram")
            .interval(interval)
            .field(ElasticsearchUtil.TIMESTAMP_FIELD)
            .subAggregation(statsBuilder)
            .subAggregation(nestedFaultCountBuilder);

    BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, NodeDetails.class);
    // Only interested in service endpoints, so just Consumer nodes
    query.must(QueryBuilders.termQuery(ElasticsearchUtil.TYPE_FIELD, "Consumer"));

    SearchRequestBuilder request = getNodeDetailsRequest(index, criteria, query, 0)
            .addAggregation(histogramBuilder);

    SearchResponse response = getSearchResponse(request);
    DateHistogram histogram = response.getAggregations().get("histogram");

    return histogram.getBuckets().stream()
            .map(AnalyticsServiceElasticsearch::toTimeseriesStatistics)
            .collect(Collectors.toList());
}