private QueryBuilder buildSearchQuery() { BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); for (ElasticsearchColumnHandle column : columns) { BoolQueryBuilder columnQueryBuilder = new BoolQueryBuilder(); Type type = column.getColumnType(); if (tupleDomain.getDomains().isPresent()) { Domain domain = tupleDomain.getDomains().get().get(column); if (domain != null) { columnQueryBuilder.should(buildPredicate(column.getColumnJsonPath(), domain, type)); } } boolQueryBuilder.must(columnQueryBuilder); } if (boolQueryBuilder.hasClauses()) { return boolQueryBuilder; } return new MatchAllQueryBuilder(); }
@Override protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { BoolQueryBuilder newBuilder = new BoolQueryBuilder(); boolean changed = false; final int clauses = mustClauses.size() + mustNotClauses.size() + filterClauses.size() + shouldClauses.size(); if (clauses == 0) { return new MatchAllQueryBuilder().boost(boost()).queryName(queryName()); } changed |= rewriteClauses(queryRewriteContext, mustClauses, newBuilder::must); changed |= rewriteClauses(queryRewriteContext, mustNotClauses, newBuilder::mustNot); changed |= rewriteClauses(queryRewriteContext, filterClauses, newBuilder::filter); changed |= rewriteClauses(queryRewriteContext, shouldClauses, newBuilder::should); // lets do some early termination and prevent any kind of rewriting if we have a mandatory query that is a MatchNoneQueryBuilder Optional<QueryBuilder> any = Stream.concat(newBuilder.mustClauses.stream(), newBuilder.filterClauses.stream()) .filter(b -> b instanceof MatchNoneQueryBuilder).findAny(); if (any.isPresent()) { return any.get(); } if (changed) { newBuilder.adjustPureNegative = adjustPureNegative; newBuilder.minimumShouldMatch = minimumShouldMatch; newBuilder.boost(boost()); newBuilder.queryName(queryName()); return newBuilder; } return this; }
@Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); printBoostAndQueryName(builder); builder.endObject(); }
/** * Count the number of documents matching the given {@link Query}. * * @param entityType must not be {@literal null}. * @return a {@link Mono} emitting the nr of matching documents. */ default Mono<Long> count(Class<?> entityType) { return count(new StringQuery(QueryBuilders.matchAllQuery().toString()), entityType, null); }
@Test public void testSingleShardSearch() throws Exception { Random random = new Random(); int docs = 999 + random.nextInt(10001 - 999); int batchSize = 1 + random.nextInt(5000 - 1); long totalHitsReceived = 0; elastic.populate(1, 0, schema, table, docs, ElasticsearchCluster.PRIMITIVE_TYPES); assertThat(elastic.search(schema, table).count, equalTo((long) docs)); ClusterState state = state(schema); IndexRoutingTable irt = routing(state, schema); ShardsIterator iter = irt.randomAllActiveShardsIt(); DiscoveryNodes nodes = state.nodes(); assertThat(iter.size(), equalTo(1)); ShardRouting routing = iter.nextOrNull(); assertNotNull(routing); assertNull(iter.nextOrNull()); DiscoveryNode dn = nodes.get(routing.currentNodeId()); logger.info("--> executing search on: [{}] batch size: [{}]", routing, batchSize); Search<byte[]> search = new SearchBytes() .setQuery(String.format("{ \"query\": %s } ", QueryBuilders.matchAllQuery().toString())) .setResource(schema + "/" + table) .setParameter("scroll", "10s") .setParameter("size", Integer.toString(batchSize)); byte[] result = connection.execute(search); totalHitsReceived = ElasticsearchCluster.asJsonObject(result).get(ElasticsearchConstants.HITS).getAsJsonObject().get(ElasticsearchConstants.TOTAL_HITS).getAsInt(); assertThat(totalHitsReceived, equalTo((long) docs)); }
/** * Creates a function_score query that executes the provided filters and functions on all documents * * @param filterFunctionBuilders the filters and functions */ public FunctionScoreQueryBuilder(FilterFunctionBuilder[] filterFunctionBuilders) { this(new MatchAllQueryBuilder(), filterFunctionBuilders); }
@Override protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { BoolQueryBuilder newBuilder = new BoolQueryBuilder(); boolean changed = false; final int clauses = mustClauses.size() + mustNotClauses.size() + filterClauses.size() + shouldClauses.size(); if (clauses == 0) { return new MatchAllQueryBuilder().boost(boost()).queryName(queryName()); } changed |= rewriteClauses(queryRewriteContext, mustClauses, newBuilder::must); changed |= rewriteClauses(queryRewriteContext, mustNotClauses, newBuilder::mustNot); changed |= rewriteClauses(queryRewriteContext, filterClauses, newBuilder::filter); changed |= rewriteClauses(queryRewriteContext, shouldClauses, newBuilder::should); // lets do some early termination and prevent any kind of rewriting if we have a mandatory query that is a MatchNoneQueryBuilder Optional<QueryBuilder> any = Stream.concat(newBuilder.mustClauses.stream(), newBuilder.filterClauses.stream()) .filter(b -> b instanceof MatchNoneQueryBuilder).findAny(); if (any.isPresent()) { return any.get(); } if (changed) { newBuilder.adjustPureNegative = adjustPureNegative; newBuilder.minimumShouldMatch = minimumShouldMatch; newBuilder.boost(boost()); newBuilder.queryName(queryName()); return newBuilder; } return this; }
@Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); printBoostAndQueryName(builder); builder.endObject(); }
@Test public void testMultipleShardSearch() throws Exception { Random random = new Random(); int docs = 3 + random.nextInt(10001 - 3);; int batchSize = 1 + random.nextInt(5000 - 1); int shards = 1 + random.nextInt(5 - 1); long totalHitsReceived = 0; elastic.populate(shards, 0, schema, table, docs, ElasticsearchCluster.PRIMITIVE_TYPES); assertThat(elastic.search(schema, table).count, equalTo((long) docs)); ClusterState state = state(schema); IndexRoutingTable irt = routing(state, schema); ShardsIterator iter = irt.randomAllActiveShardsIt(); DiscoveryNodes nodes = state.nodes(); assertThat(iter.size(), equalTo(shards)); ShardRouting routing; while ((routing = iter.nextOrNull()) != null) { DiscoveryNode dn = nodes.get(routing.currentNodeId()); logger.info("--> executing search on: [{}] batch size: [{}]", routing, batchSize); Search<byte[]> search = new SearchBytes() .setQuery(String.format("{ \"query\": %s } ", QueryBuilders.matchAllQuery().toString())) .setResource(schema + "/" + table) .setParameter("scroll", "10s") .setParameter("preference", "_shards:" + routing.getId()) .setParameter("size", Integer.toString(batchSize)); byte[] result = connection.execute(search); totalHitsReceived += ElasticsearchCluster.asJsonObject(result).get("hits").getAsJsonObject().get("total").getAsInt(); } logger.info("--> total hits received across [{}] shards: [{}]", iter.size(), totalHitsReceived); assertThat(totalHitsReceived, equalTo((long) docs)); }
/** * A query that matches on all documents. */ public static MatchAllQueryBuilder matchAllQuery() { return new MatchAllQueryBuilder(); }
@Override protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { BoolQueryBuilder newBuilder = new BoolQueryBuilder(); boolean changed = false; final int clauses = mustClauses.size() + mustNotClauses.size() + filterClauses.size() + shouldClauses.size(); if (clauses == 0) { return new MatchAllQueryBuilder().boost(boost()).queryName(queryName()); } changed |= rewriteClauses(queryRewriteContext, mustClauses, newBuilder::must); changed |= rewriteClauses(queryRewriteContext, mustNotClauses, newBuilder::mustNot); changed |= rewriteClauses(queryRewriteContext, filterClauses, newBuilder::filter); changed |= rewriteClauses(queryRewriteContext, shouldClauses, newBuilder::should); // lets do some early termination and prevent any kind of rewriting if we have a mandatory query that is a MatchNoneQueryBuilder Optional<QueryBuilder> any = Stream.concat(newBuilder.mustClauses.stream(), newBuilder.filterClauses.stream()) .filter(b -> b instanceof MatchNoneQueryBuilder).findAny(); if (any.isPresent()) { return any.get(); } if (changed) { newBuilder.adjustPureNegative = adjustPureNegative; newBuilder.minimumShouldMatch = minimumShouldMatch; newBuilder.boost(boost()); newBuilder.queryName(queryName()); return newBuilder; } return this; }
@Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); printBoostAndQueryName(builder); builder.endObject(); }
public FilterFunctionBuilder(ScoreFunctionBuilder<?> scoreFunctionBuilder) { this(new MatchAllQueryBuilder(), scoreFunctionBuilder); }
@Override protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { BoolQueryBuilder newBuilder = new BoolQueryBuilder(); boolean changed = false; final int clauses = mustClauses.size() + mustNotClauses.size() + filterClauses.size() + shouldClauses.size(); if (clauses == 0) { return new MatchAllQueryBuilder().boost(boost()).queryName(queryName()); } changed |= rewriteClauses(queryRewriteContext, mustClauses, newBuilder::must); changed |= rewriteClauses(queryRewriteContext, mustNotClauses, newBuilder::mustNot); changed |= rewriteClauses(queryRewriteContext, filterClauses, newBuilder::filter); changed |= rewriteClauses(queryRewriteContext, shouldClauses, newBuilder::should); if (changed) { newBuilder.adjustPureNegative = adjustPureNegative; newBuilder.disableCoord = disableCoord; newBuilder.minimumShouldMatch = minimumShouldMatch; newBuilder.boost(boost()); newBuilder.queryName(queryName()); return newBuilder; } return this; }
@Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); printBoostAndQueryName(builder); builder.endObject(); }
/** * Creates a function_score query that will execute the function provided on all documents * * @param scoreFunctionBuilder score function that is executed */ public FunctionScoreQueryBuilder(ScoreFunctionBuilder<?> scoreFunctionBuilder) { this(new MatchAllQueryBuilder(), new FilterFunctionBuilder[]{new FilterFunctionBuilder(scoreFunctionBuilder)}); }
/** * BWC serialization for collapsing {@link InnerHitBuilder}. * Should only be used to send collapsing inner hits to nodes pre 5.5. */ public void writeToCollapseBWC(StreamOutput out) throws IOException { assert out.getVersion().before(Version.V_5_5_0) : "invalid output version, must be < " + Version.V_5_5_0.toString(); writeToBWC(out, new MatchAllQueryBuilder(), null, null); }
public static QueryBuilder simpleQueryBuilder(String q, boolean or, Boosts boosts) { if (q.equals("yacyall")) return new MatchAllQueryBuilder(); final MultiMatchQueryBuilder qb = QueryBuilders .multiMatchQuery(q) .operator(or ? Operator.OR : Operator.AND) .zeroTermsQuery(ZeroTermsQuery.ALL); boosts.forEach((mapping, boost) -> qb.field(mapping.getMapping().name(), boost)); return qb; }
if ((from() != null && from().toString().contains("now")) || (to() != null && to().toString().contains("now"))) { return new MatchAllQueryBuilder();
/** * A query that matches on all documents. */ public static MatchAllQueryBuilder matchAllQuery() { return new MatchAllQueryBuilder(); }