Java 类org.elasticsearch.index.query.FilterBuilders 实例源码

项目:soundwave    文件:EsInstanceStore.java   
@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;
}
项目:soundwave    文件:EsStore.java   
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);
}
项目:oneops    文件:DeploymentPlanProcessor.java   
private CmsRelease fetchReleaseRecord(String nsPath, Date ts, int genTime) throws InterruptedException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(CmsConstants.SEARCH_TS_PATTERN);
    Thread.sleep(3000);
    SearchQuery latestRelease = new NativeSearchQueryBuilder()
            .withIndices("cms-*")
            .withTypes("release").withFilter(
                    FilterBuilders.andFilter(
                            FilterBuilders.queryFilter(QueryBuilders.termQuery("nsPath.keyword", nsPath)),
                            FilterBuilders.queryFilter(QueryBuilders.rangeQuery("created").
                                    from(simpleDateFormat.format(DateUtils.addMinutes(ts, -(genTime + 10)))).
                                    to(simpleDateFormat.format(ts))))).
                    withSort(SortBuilders.fieldSort("created").order(SortOrder.DESC)).build();

    List<CmsReleaseSearch> ciList = indexer.getTemplate().queryForList(latestRelease, CmsReleaseSearch.class);
    if (!ciList.isEmpty()) {
        return ciList.get(0);
    }
    throw new RuntimeException("Cant find bom release for deployment plan generation event");
}
项目:geeCommerce-Java-Shop-Software-and-PIM    文件:DefaultSearchService.java   
private FilterBuilder getFilterBuilder(SearchQuery searchQuery) {
    Map<String, Object> filterMap = searchQuery.getFilter();
    List<FilterBuilder> filterBuilders = new ArrayList<>();

    if (filterMap != null && filterMap.size() > 0) {
        for (String key : filterMap.keySet()) {
            filterBuilders.add(FilterBuilders.termFilter(key, filterMap.get(key)));
        }
    }

    if (searchQuery.getPriceFrom() != null || searchQuery.getPriceTo() != null)
        filterBuilders.add(
            FilterBuilders.rangeFilter("price").from(searchQuery.getPriceFrom()).to(searchQuery.getPriceTo()));

    if (searchQuery.isShowEvent()) {
        filterBuilders.add(FilterBuilders.termFilter("is_special", true));
    }

    if (searchQuery.isShowSale()) {
        filterBuilders.add(FilterBuilders.termFilter("is_sale", true));
    }

    return FilterBuilders.andFilter(filterBuilders.toArray(new FilterBuilder[filterBuilders.size()]));
}
项目:es-service-parent    文件:EsQueryBuilder.java   
/**
 * 获取过滤器
 * 
 * @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;
}
项目:hawkular-apm    文件:AnalyticsServiceElasticsearch.java   
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();
    }
}
项目:bts    文件:ElasticTest.java   
public void getContributionsByCandName(Client client, String candName, int i)
{
    QueryBuilder matchQuery = QueryBuilders.matchQuery("candNm", candName);
    FilterBuilder contribRangeFilter = FilterBuilders.rangeFilter("contbReceiptAmt").gte(i);
    StatisticalFacetBuilder facet = FacetBuilders.statisticalFacet("stat1").field("contbReceiptAmt");
    SearchRequestBuilder request = client.prepareSearch("contributions")
            .addSort(SortBuilders.fieldSort("contbReceiptAmt").order(SortOrder.DESC))
            .setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(matchQuery).setPostFilter(contribRangeFilter)
            .addFacet(facet).setFrom(0).setSize(100)
            .addFields("contbrNm", "candNm", "contbrEmployer", "contbReceiptAmt");
    System.out.println("SEARCH QUERY: " + request.toString());

    SearchResponse response = request.execute().actionGet();
    SearchHits searchHits = response.getHits();
    SearchHit[] hits = searchHits.getHits();
    for (SearchHit hit : hits)
    {
        Map<String, SearchHitField> fields = hit.getFields();
        System.out.println(hit.getId() + ", contbrEmployer=" + fields.get("contbrEmployer").getValue().toString());
    }
}
项目:bts    文件:GeneralBTSObjectControllerImpl.java   
private List<FilterBuilder> makeFilterList(BTSConfigItem configItem, BTSObject object) {

        Set<String> referenceTypes = configurationController.getReferenceTypesSet(object, configItem);

        List<FilterBuilder> filters = new ArrayList<FilterBuilder>();
        for (String ref : referenceTypes) {
            if (ref.contains(BTSConstants.OWNER_REFERENCED_TYPES_PATH_SEPERATOR)) {
                String[] split = ref.split("\\.");
                if (split.length == 2) {
                    filters.add(FilterBuilders.termFilter(new String("type"),
                            ref));
                } else if (split.length == 3) {
                    filters.add(FilterBuilders.termFilter(
                            new String("subtype"), ref));
                }

            }

        }
        return filters;

    }
项目:blackmarket    文件:ExileToolsSearchClientTest.java   
@Test
@Ignore
public void testQuery() throws Exception {
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    FilterBuilder filter = FilterBuilders.boolFilter()
            .must(FilterBuilders.termFilter("attributes.league", "Flashback Event HC (IC002)"))
            .must(FilterBuilders.termFilter("attributes.equipType", "Jewel"))
            .must(FilterBuilders.rangeFilter("modsTotal.#% increased maximum Life").gt(4))
            .must(FilterBuilders.termFilter("shop.verified", "yes"))
           // .must(FilterBuilders.termFilter("attributes.rarity", "Magic"))
            ;

    searchSourceBuilder
            .query(QueryBuilders.filteredQuery(QueryBuilders.boolQuery().minimumNumberShouldMatch(2)
                    .should(QueryBuilders.rangeQuery("modsTotal.#% increased Area Damage"))
                    .should(QueryBuilders.rangeQuery("modsTotal.#% increased Projectile Damage"))
                    .should(QueryBuilders.rangeQuery("modsTotal.#% increased Chaos Damage")), filter))
            .sort("_score");
    SearchResult result = client.execute(searchSourceBuilder.toString()).getSearchResult();
    List<Hit<ExileToolsHit, Void>> hits = result.getHits(ExileToolsHit.class);
    hits.stream().map(hit -> hit.source).forEach(System.out::println);
}
项目:blackmarket    文件:ExileToolsSearchClientTest.java   
/**
     * As per ES documentation/tome, the best way to do our search is via Filters
     */
    @Test
    @Ignore
    public void testExecuteMjolnerUsingFilters() throws Exception {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        List<FilterBuilder> filters = new LinkedList<>();

        filters.add(FilterBuilders.termFilter("attributes.league", "Flashback Event (IC001)"));
//      filters.add(FilterBuilders.termFilter("info.name", "Mjolner"));
        filters.add(FilterBuilders.termFilter("info.name", "Hegemony's Era"));
        filters.add(FilterBuilders.rangeFilter("properties.Weapon.Physical DPS").from(400));

        FilterBuilder filter = FilterBuilders.andFilter(filters.toArray(new FilterBuilder[filters.size()]));
        searchSourceBuilder.query(QueryBuilders.filteredQuery(null, filter));
        searchSourceBuilder.size(100);
        SearchResult result = client.execute(searchSourceBuilder.toString()).getSearchResult();
        List<Hit<ExileToolsHit, Void>> hits = result.getHits(ExileToolsHit.class);
        for (Hit<ExileToolsHit, Void> hit : hits) {
//          logger.info(hit.source.toString());
//          hit.source.getQuality().ifPresent( q -> logger.info(q.toString()) );
            hit.source.getPhysicalDPS().ifPresent( q -> logger.info(q.toString()) );
//          logger.info(hit.source.toString());
//          logger.info(hit.source.getRequirements().getLevel().toString());
//          logger.info(hit.source.getExplicitMods().toString());
        }
    }
项目:blackmarket    文件:ExileToolsSearchClientTest.java   
@Test
@Ignore
public void testDistinctItemTypeValues() throws Exception {
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.filtered(null, FilterBuilders.boolFilter()
            .must(FilterBuilders.termFilter("attributes.itemType", "Card"))
            ));
    searchSourceBuilder.aggregation(AggregationBuilders.terms("rarities").field("info.name")
            .size(0));
    SearchResult result = client.execute(searchSourceBuilder.toString()).getSearchResult();
    logger.info(result.getJsonString());
    System.out.println("-------");
    result.getAggregations().getTermsAggregation("rarities").getBuckets().stream()
    .map(e -> e.getKey())
    .sorted()
    .forEach(k -> System.out.println(k));
}
项目:searchahouse.com    文件:PropertyServiceImpl.java   
@Override
public List<Property> findPropertiesByLocation(final GeoPoint geoPoint, Double distance, final SortOrder sortOrder) {
    GeoDistanceFilterBuilder filter = FilterBuilders.geoDistanceFilter("location").point(geoPoint.getLat(), geoPoint.getLon())
            .distance(distance, DistanceUnit.KILOMETERS);

    SearchQuery searchQuery = new NativeSearchQueryBuilder()
            .withFilter(filter)
            .withSort(
                    SortBuilders.geoDistanceSort("location").point(geoPoint.getLat(), geoPoint.getLon())
                            .order(sortOrder == null ? SortOrder.ASC : sortOrder)).build();

    searchQuery.addIndices("searchahouse");
    searchQuery.addTypes("property");

    List<Property> properties = this.elasticsearchOperations.queryForList(searchQuery, Property.class);

    return properties;
}
项目:bluemarlin    文件:ExileToolsSearchClientTest.java   
@Test
    @Ignore
    public void testQuery() throws Exception {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        FilterBuilder filter = FilterBuilders.boolFilter()
                .must(FilterBuilders.termFilter("attributes.league", "Flashback Event HC (IC002)"))
                .must(FilterBuilders.termFilter("attributes.equipType", "Jewel"))
                .must(FilterBuilders.rangeFilter("modsTotal.#% increased maximum Life").gt(4))
                .must(FilterBuilders.termFilter("shop.verified", "yes"))
               // .must(FilterBuilders.termFilter("attributes.rarity", "Magic"))
                ;

        searchSourceBuilder
                .query(QueryBuilders.filteredQuery(QueryBuilders.boolQuery().minimumNumberShouldMatch(2)
                        .should(QueryBuilders.rangeQuery("modsTotal.#% increased Area Damage"))
                        .should(QueryBuilders.rangeQuery("modsTotal.#% increased Projectile Damage"))
                        .should(QueryBuilders.rangeQuery("modsTotal.#% increased Chaos Damage")), filter))
                .sort("_score");
//      SearchResult result = client.execute(searchSourceBuilder.toString()).getSearchResult();
//      List<Hit<ExileToolsHit, Void>> hits = result.getHits(ExileToolsHit.class);
//      hits.stream().map(hit -> hit.source).forEach(System.out::println);
    }
项目:bluemarlin    文件:ExileToolsSearchClientTest.java   
/**
     * As per ES documentation/tome, the best way to do our search is via Filters
     */
    @Test
    @Ignore
    public void testExecuteMjolnerUsingFilters() throws Exception {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        List<FilterBuilder> filters = new LinkedList<>();

        filters.add(FilterBuilders.termFilter("attributes.league", "Flashback Event (IC001)"));
//      filters.add(FilterBuilders.termFilter("info.name", "Mjolner"));
        filters.add(FilterBuilders.termFilter("info.name", "Hegemony's Era"));
        filters.add(FilterBuilders.rangeFilter("properties.Weapon.Physical DPS").from(400));

        FilterBuilder filter = FilterBuilders.andFilter(filters.toArray(new FilterBuilder[filters.size()]));
        searchSourceBuilder.query(QueryBuilders.filteredQuery(null, filter));
        searchSourceBuilder.size(100);
//      SearchResult result = client.execute(searchSourceBuilder.toString()).getSearchResult();
//      List<Hit<ExileToolsHit, Void>> hits = result.getHits(ExileToolsHit.class);
//      for (Hit<ExileToolsHit, Void> hit : hits) {
////            logger.info(hit.source.toString());
////            hit.source.getQuality().ifPresent( q -> logger.info(q.toString()) );
//          hit.source.getPhysicalDPS().ifPresent( q -> logger.info(q.toString()) );
////            logger.info(hit.source.toString());
////            logger.info(hit.source.getRequirements().getLevel().toString());
////            logger.info(hit.source.getExplicitMods().toString());
//      }
    }
项目:elasticsearch-tutorial    文件:ProductQueryServiceImpl.java   
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;
    }
项目:elasticsearch-akka    文件:ProductQueryServiceImpl.java   
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;
}
项目:async-elastic-orm    文件:QueryTest.java   
@Test
public void testScriptFilter() {
    for (int i = 0; i < 10; i++) {
        TestEmbedHolder testEmbedHolder = new TestEmbedHolder();
        testEmbedHolder.testEmbed1 = new TestEmbed();
        testEmbedHolder.testEmbed1.x = i;
        testEmbedHolder.testEmbed1.z = 10;
        getGDS().save().entity(testEmbedHolder).now();
    }

    refreshIndex();

    Assert.assertEquals(10, getGDS().query(TestEmbedHolder.class).asList().size());

    String script = "_source.testEmbed1.x > 5";
    Assert.assertEquals(4, getGDS().query(TestEmbedHolder.class)
            .filter(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.scriptFilter(script)))
            .asList().size());

    script = "_source.testEmbed1.x * _source.testEmbed1.z < 25";
    Assert.assertEquals(3, getGDS().query(TestEmbedHolder.class)
            .filter(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.scriptFilter(script)))
            .asList().size());

}
项目:disthene-reader    文件:IndexService.java   
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);
}
项目:elasticsearch-river-github    文件:GitHubRiver.java   
/**
 * 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;
    }
}
项目:elasticsearch-rest-command    文件:AST_Search.java   
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;
}
项目:searchanalytics-bigdata    文件:ProductQueryServiceImpl.java   
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;
}
项目:ezelastic    文件:EzBakeVisibilityFilterIntegrationTest.java   
private static ScriptFilterBuilder createVisibilityFilter(
        Set<String> formalAuths, Set<String> externalAuths, Set<Long> platformAuths,
        boolean docSourceAccessRequired) throws Exception {
    final Authorizations auths = new Authorizations();
    auths.setFormalAuthorizations(formalAuths);
    auths.setExternalCommunityAuthorizations(externalAuths);
    auths.setPlatformObjectAuthorizations(platformAuths);

    final String requiredPerms = docSourceAccessRequired ? Permission.READ.name() : Permission.DISCOVER.name();

    final ScriptFilterBuilder visibilityFilter = FilterBuilders.scriptFilter("visibility").lang("native");
    visibilityFilter.addParam(EzSecurityVisibilityFilter.VISIBILITY_FIELD_PARAM, VISIBILITY_FIELD);
    visibilityFilter.addParam(EzSecurityVisibilityFilter.AUTHS_PARAM, serializeToBase64(auths));
    visibilityFilter.addParam(EzSecurityVisibilityFilter.REQUIRED_PERMISSIONS_PARAM, requiredPerms);

    return visibilityFilter;
}
项目:ezelastic    文件:ElasticClientTest.java   
@Test
public void testQueryWithFilter() throws Exception {
    // Prepare
    populateWithTestDocs();

    // Action: Two matches as 42Six HQ has the word in the title and 42Six
    // DC has it in the comments
    final String filter = FilterBuilders.andFilter(
            FilterBuilders.rangeFilter("rating").from(0).to(100), FilterBuilders.termFilter("title", "42six"))
            .toString();
    final SearchResult results = client.get(
            QueryBuilders.matchAllQuery().toString(), TEST_TYPE, null, null, null, filter, 0, (short) -1, null,
            USER_TOKEN);

    // Assertion
    final List<String> expectedIds = Arrays.asList(oneaaColumbiaDoc.get_id(), oneaaNavyYardDoc.get_id());
    assertEquals(expectedIds.size(), results.getTotalHits());
    for (final Document result : results.getMatchingDocuments()) {
        assertTrue(expectedIds.contains(result.get_id()));
    }
}
项目:ezelastic    文件:ElasticClientTest.java   
@Test
public void testQueryWithElasticJsonAndFilter() throws Exception {
    // Prepare
    populateWithTestDocs();

    // Action: Filter will match two but query should only match one due to
    // rating range of query.
    final String filter = FilterBuilders.andFilter(
            FilterBuilders.rangeFilter("rating").from(0).to(100), FilterBuilders.termFilter("title", "42six"))
            .toString();
    final String query = QueryBuilders.boolQuery().must(
            QueryBuilders.rangeQuery("rating").from(35).to(98).includeLower(true).includeLower(true))
            .must(QueryBuilders.matchQuery("comments", "42six")).mustNot(QueryBuilders.prefixQuery("title", "W"))
            .toString();

    final SearchResult results =
            client.get(query, TEST_TYPE, null, null, null, filter, 0, (short) -1, null, USER_TOKEN);

    // Assertion
    final List<String> expectedIds = Collections.singletonList(oneaaColumbiaDoc.get_id());
    assertEquals(expectedIds.size(), results.getTotalHits());
    for (final Document result : results.getMatchingDocuments()) {
        assertTrue(expectedIds.contains(result.get_id()));
    }
}
项目:ezelastic    文件:ElasticClientTest.java   
@Test
public void testFilterShouldNotFilterFacets() throws Exception {
    populateWithTestDocs();

    final String filter = FilterBuilders.termFilter("title", "42six").toString();

    final TermsFacet tf = new TermsFacet(Collections.singletonList("rating"));
    final FacetRequest request = new FacetRequest();
    request.setTermsFacet(tf);
    final Facet facet = new Facet().setLabel("magic").setFacet(request);

    final SearchResult results = client.get(
            QueryBuilders.matchAllQuery().toString(), TEST_TYPE, null, null, Collections.singletonList(facet),
            filter, 0, (short) -1, null, USER_TOKEN);

    assertEquals(2, results.getTotalHits());
    final List<TermsFacetEntry> entries = results.getFacets().get("magic").getTermsFacetResult().getEntries();
    for (final TermsFacetEntry entry : entries) {
        // if the filter gets applied to the facets this will only be 1
        if ("92".equalsIgnoreCase(entry.getTerm())) {
            assertEquals(2, entry.getCount());
        }
    }
}
项目:ezelastic    文件:IT_EzElasticIntegrationTest.java   
@Test
public void testQueryWithFilter() throws Exception {
    // Prepare: Setup Thrift Client and add some docs
    client = pool.getClient(SERVICE_NAME, EzElastic.Client.class);
    client.setTypeMapping(TEST_TYPE, getMappingForTest(), fakeSecurity);
    client.bulkPut(
            Arrays.asList(washingtonMonumentDoc, whiteHouseDoc, oneaaColumbiaDoc, oneaaNavyYardDoc), fakeSecurity);
    final String filter = FilterBuilders.andFilter(
            FilterBuilders.rangeFilter("rating").from(0).to(100), FilterBuilders.termFilter("title", "42six"))
            .toString();

    // Action: Create and execute a match all query with date bucket facets
    final Query query =
            new Query(QueryBuilders.matchAllQuery().toString()).setType(TEST_TYPE).setFilterJson(filter);
    final SearchResult results = client.query(query, fakeSecurity);

    // Assert: All records should be a match and the facet buckets should be organized as expected
    final List<String> expectedIds = Arrays.asList(oneaaColumbiaDoc.get_id(), oneaaNavyYardDoc.get_id());
    assertEquals(expectedIds.size(), results.getTotalHits());
    for (final Document result : results.getMatchingDocuments()) {
        assertTrue(expectedIds.contains(result.get_id()));
    }

    pool.returnToPool(client);
}
项目:scaleset-search    文件:GeoPolygonFilterConverter.java   
@Override
public FilterBuilder convert(Filter filter) {
    String field = filter.getString("field");
    Geometry geometry = filter.get(Geometry.class, "geometry");
    ShapeBuilder shapeBuilder = ShapeBuilderUtil.toShapeBuilder(geometry);
    GeoPolygonFilterBuilder result = null;
    if (geometry instanceof Polygon) {
        result = FilterBuilders.geoPolygonFilter(field);
        for (Coordinate coord : ((Polygon) geometry).getExteriorRing().getCoordinates()) {
            result.addPoint(coord.y, coord.x);
        }
    } else {
        // throw an error?
    }
    return result;
}
项目:elasticsearch-java-client-examples    文件:QueryDslFilterOfficial.java   
@Test
public void testAndFilter() {
  // basic steps:
  // 1. create FilterBuilder by FilterBuilders (optionally caching)
  FilterBuilder andFilter =
      FilterBuilders.andFilter(FilterBuilders.rangeFilter("speech_number").from(2).to(5),
          FilterBuilders.prefixFilter("text_entry", "against")).cache(true);

  // 2. create filteredQuery type query and then add filter
  QueryBuilder filteredQuery =
      QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), andFilter);

  // 3. execute the search as usual
  SearchResponse response = srb.setQuery(filteredQuery).execute().actionGet();

  // read response
  long totalMatched = response.getHits().getTotalHits();
  System.out.println("Total matched: " + totalMatched);
}
项目:elasticsearch-java-client-examples    文件:QueryDslFilterOfficial.java   
@Test
public void testBoolFilter() {
  FilterBuilder boolFilter =
      FilterBuilders.boolFilter().must(FilterBuilders.termFilter("speaker", "WESTMORELAND"))
          .mustNot(FilterBuilders.termFilter("speech_number", 2));

  QueryBuilder filteredQuery =
      QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), boolFilter);

  // 3. execute the search as usual
  SearchResponse response = srb.setQuery(filteredQuery).execute().actionGet();

  // read response
  long totalMatched = response.getHits().getTotalHits();
  System.out.println("Total matched: " + totalMatched);
}
项目:elasticsearch-java-client-examples    文件:QueryDslFilterOfficial.java   
@Test
public void testBoolFilterWithMatchQuery() {
  FilterBuilder boolFilter =
      FilterBuilders.boolFilter().must(FilterBuilders.termFilter("speaker", "WESTMORELAND"))
          .mustNot(FilterBuilders.termFilter("speech_number", 2));

  QueryBuilder filteredQuery =
      QueryBuilders.filteredQuery(QueryBuilders.matchQuery("text_entry", "day"), boolFilter);

  // 3. execute the search as usual
  SearchResponse response = srb.setQuery(filteredQuery).execute().actionGet();

  // read response
  long totalMatched = response.getHits().getTotalHits();
  System.out.println("Total matched: " + totalMatched);
}
项目:elasticsearch-java-client-examples    文件:QueryDslFilterOfficial.java   
@Test
public void testTermsFilter() {
  // execution:
  // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html#_execution_mode
  FilterBuilder termFilter =
      FilterBuilders.termsFilter("text_entry", "havoc", "gallant").execution("plain");

  QueryBuilder filteredQuery =
      QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), termFilter);

  SearchResponse response = srb.setQuery(filteredQuery).execute().actionGet();

  // read response
  long totalMatched = response.getHits().getTotalHits();
  System.out.println("Total matched: " + totalMatched);
}
项目:elasticsearch-java-client-examples    文件:QueryDslOfficial.java   
@Test
public void testConstantScoreQuery() {
  // Using with Filters
  QueryBuilder qb1 =
      QueryBuilders.constantScoreQuery(FilterBuilders.termFilter(fieldShakeSpeare, "henry"))
          .boost(2.0f);

  // With Queries
  QueryBuilder qb2 =
      QueryBuilders.constantScoreQuery(QueryBuilders.termQuery(fieldShakeSpeare, "henry")).boost(
          2.0f);

  SearchResponse response1 = srb.setQuery(qb1).execute().actionGet();
  SearchResponse response2 = srb.setQuery(qb2).execute().actionGet();

  // read response
  long totalMatched = response1.getHits().getTotalHits();
  System.out.println("Total matched by filter query: " + totalMatched);

  totalMatched = response2.getHits().getTotalHits();
  System.out.println("Total matched by pure query: " + totalMatched);
}
项目:elasticsearch-java-client-examples    文件:SearchApiOfficial.java   
@Test
public void testSearch() {
  SearchResponse searchResponse =
      client.prepareSearch(indexName).setTypes(typeName)
          .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
          // set query
          .setQuery(QueryBuilders.termQuery("message", "try"))
          // set filter
          .setPostFilter(FilterBuilders.rangeFilter("age").from(15).to(25)).setFrom(0)
          .setSize(60).setExplain(true).execute().actionGet();

  // smallest search call
  // SearchResponse searchResponse = client.prepareSearch(indexName).execute().actionGet();

  // read response
  System.out.println(searchResponse.toString());
}
项目:elasticsearch-java-client-examples    文件:FacetsApiOfficial.java   
@Test
public void testFilterFacet() {
  FilterFacetBuilder filterFacet =
      FacetBuilders
          .filterFacet(filterFacetName, FilterBuilders.termFilter("speaker", "MORTIMER"));

  SearchResponse response =
      client.prepareSearch(indexShakeSpeare)
          .setQuery(QueryBuilders.termQuery(fieldShakeSpeare, targetText)).addFacet(filterFacet)
          .execute().actionGet();

  FilterFacet f = (FilterFacet) response.getFacets().facetsAsMap().get(filterFacetName);

  // Number of docs that matched
  System.out.println(String.format("Number of docs that matched: %d", f.getCount()));
}
项目:dcp-api    文件:ProxyQueryBuilder.java   
public static SearchRequestBuilder prepareConversationThreadForReferencesQuery(int count,
        SearchRequestBuilder builder, String[] references, String project, String mail_list) {
    builder
            .setFrom(0)
            .setSize(count)
            .addFields("date", "subject", "mail_list", "message_id", "message_id_original", "author.not_analyzed",
                    "references")
            .addScriptField("millis", "doc['date'].date.millis")
            .setQuery(
                    QueryBuilders.constantScoreQuery(FilterBuilders
                            .boolFilter()
                            .must(
                                    FilterBuilders.orFilter(FilterBuilders.termsFilter("references", references),
                                            FilterBuilders.termsFilter("message_id_original", references)))
                            .must(FilterBuilders.queryFilter(QueryBuilders.termQuery("project", project)))
                            .must(FilterBuilders.queryFilter(QueryBuilders.termQuery("mail_list", mail_list)))));
    return builder;
}
项目:searchisko    文件:SearchClientService.java   
/**
 * Perform filter query into one ES index and type with one field and value. Be careful, this returns max 10 records!
 * 
 * @param indexName to search in
 * @param indexType to search
 * @param fieldName name of field to search
 * @param fieldValue value to search for
 * @return SearchResponse.
 */
public SearchResponse performFilterByOneField(String indexName, String indexType, String fieldName, String fieldValue)
        throws SearchIndexMissingException {
    try {
        SearchRequestBuilder searchBuilder = getClient().prepareSearch(indexName).setTypes(indexType).setSize(10);

        searchBuilder.setPostFilter(FilterBuilders.queryFilter(QueryBuilders.matchQuery(fieldName, fieldValue)));
        searchBuilder.setQuery(QueryBuilders.matchAllQuery());

        final SearchResponse response = searchBuilder.execute().actionGet();
        return response;
    } catch (IndexMissingException e) {
        log.log(Level.WARNING, e.getMessage());
        throw new SearchIndexMissingException(e);
    }
}
项目:searchisko    文件:SearchClientService.java   
/**
 * Perform filter query into one ES index and type to get records with any value stored in one field. Be careful, this
 * method returns SCROLL response, so you have to use {@link #executeESScrollSearchNextRequest(SearchResponse)} to get
 * real data and go over them as is common ES scroll mechanism.
 * 
 * @param indexName to search in
 * @param indexType to search
 * @param fieldName name of field to search for any value in it
 * @param sortByField name of field to sort results by
 * @return Scroll SearchResponse
 */
public SearchResponse performQueryByOneFieldAnyValue(String indexName, String indexType, String fieldName,
        String sortByField) throws SearchIndexMissingException {
    try {
        SearchRequestBuilder searchBuilder = getClient().prepareSearch(indexName).setTypes(indexType)
                .setScroll(new TimeValue(ES_SCROLL_KEEPALIVE)).setSearchType(SearchType.SCAN).setSize(10);

        searchBuilder.setPostFilter(FilterBuilders.notFilter(FilterBuilders.missingFilter(fieldName)));
        searchBuilder.setQuery(QueryBuilders.matchAllQuery());
        searchBuilder.addSort(sortByField, SortOrder.ASC);
        final SearchResponse response = searchBuilder.execute().actionGet();
        return response;
    } catch (IndexMissingException e) {
        log.log(Level.WARNING, e.getMessage());
        throw new SearchIndexMissingException(e);
    }
}
项目:atlas-deer    文件:FiltersBuilder.java   
private static void addFilterForTopicId(
        BoolFilterBuilder filterBuilder,
        InclusionExclusionId id
) {
    NestedFilterBuilder filterForId = FilterBuilders.nestedFilter(
            EsContent.TOPICS + "." + EsTopic.TYPE_NAME,
            FilterBuilders.termFilter(
                    EsContent.TOPICS + "." + EsTopic.TYPE_NAME + "." + EsContent.ID,
                    id.getId()
            )
    );
    if (id.isExcluded()) {
        filterBuilder.mustNot(filterForId);
    } else {
        filterBuilder.must(filterForId);
    }
}
项目:atlas-deer    文件:FiltersBuilder.java   
private static FilterBuilder buildAvailabilityFilter() {
    NestedFilterBuilder rangeFilter = FilterBuilders.nestedFilter(
            EsContent.LOCATIONS,
            FilterBuilders.andFilter(
                    FilterBuilders.rangeFilter(EsLocation.AVAILABILITY_TIME)
                            .lte(ElasticsearchUtils.clampDateToFloorMinute(DateTime.now())
                                    .toString()),
                    FilterBuilders.rangeFilter(EsLocation.AVAILABILITY_END_TIME)
                            .gte(ElasticsearchUtils.clampDateToFloorMinute(DateTime.now())
                                    .toString())
            )
    );
    return FilterBuilders.orFilter(
            rangeFilter,
            FilterBuilders.hasChildFilter(EsContent.CHILD_ITEM, rangeFilter)
    );
}
项目:atlas-deer    文件:FiltersBuilder.java   
@SuppressWarnings("unchecked")
private static FilterBuilder buildRegionFilter(
        Id regionId,
        ChannelGroupResolver channelGroupResolver
) {
    ChannelGroup region;
    try {
        Resolved<ChannelGroup<?>> resolved = Futures.get(
                channelGroupResolver.resolveIds(ImmutableList.of(regionId)), IOException.class
        );
        region = resolved.getResources().first().get();
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }

    ImmutableList<ChannelNumbering> channels = ImmutableList.copyOf(region.<ChannelNumbering>getChannels());
    ImmutableList<Long> channelsIdsForRegion = channels.stream()
            .map(c -> c.getChannel().getId())
            .map(Id::longValue)
            .collect(MoreCollectors.toImmutableList());

    return FilterBuilders.termsFilter(
            EsContent.BROADCASTS + "." + EsBroadcast.CHANNEL,
            channelsIdsForRegion
    ).cache(true);
}