protected MultiSearchRequest toMultiSearchRequest(final MultiGetRequest multiGetRequest) { final MultiSearchRequest msearch = new MultiSearchRequest(); msearch.copyContextFrom(multiGetRequest); for (final Iterator<Item> iterator = multiGetRequest.iterator(); iterator.hasNext();) { final Item item = iterator.next(); final SearchRequest st = new SearchRequest(); st.routing(item.routing()); st.indices(item.indices()); st.types(item.type()); st.preference(multiGetRequest.preference()); st.source(SearchSourceBuilder.searchSource().query(new IdsQueryBuilder(item.type()).addIds(item.id()))); msearch.add(st); } return msearch; }
/** * phrase query * use whitespace analyzer in the query * @param field * @param phrase already stemmed * @param ids * @param slop * @return */ public SearchResponse matchPhrase(String field, String phrase, String[] ids, int slop) { IdsQueryBuilder idsFilterBuilder = new IdsQueryBuilder(documentType); idsFilterBuilder.addIds(ids); SearchResponse response = client.prepareSearch(indexName).setSize(ids.length) .setTrackScores(false) .setFetchSource(false).setExplain(false).setFetchSource(false). setQuery(QueryBuilders.boolQuery().must(QueryBuilders.matchPhraseQuery(field, phrase) .slop(slop).analyzer("whitespace")).filter(idsFilterBuilder)) .execute().actionGet(); // debug // XContentBuilder builder = XContentFactory.jsonBuilder(); // builder.startObject(); // System.out.println(response.toXContent(builder, ToXContent.EMPTY_PARAMS)); // builder.endObject(); // System.out.println(builder.string()); return response; }
public long count(Ngram ngram, String[] ids){ String field = ngram.getField(); int slop = ngram.getSlop(); boolean inOrder = ngram.isInOrder(); SpanNearQueryBuilder queryBuilder = QueryBuilders.spanNearQuery(new SpanTermQueryBuilder(field, ngram.getTerms()[0]), slop); for (int i = 1; i < ngram.getTerms().length; i++){ queryBuilder.addClause(new SpanTermQueryBuilder(field, ngram.getTerms()[1])); } queryBuilder.inOrder(inOrder); IdsQueryBuilder idsFilterBuilder = new IdsQueryBuilder(documentType); idsFilterBuilder.addIds(ids); long hits = client.prepareSearch(this.indexName).setQuery(queryBuilder) .execute().actionGet().getHits().getTotalHits(); return hits; }
private static QueryBuilder randomIDsQuery() { QueryBuilder q = QueryBuilders.idsQuery(); int numIDs = randomInt(100); for (int i = 0; i < numIDs; i++) { ((IdsQueryBuilder)q).addIds(String.valueOf(randomInt())); } if (randomBoolean()) { ((IdsQueryBuilder)q).boost(randomFloat()); } return q; }
protected static QueryBuilder randomNestedFilter() { int id = randomIntBetween(0, 2); switch(id) { case 0: return (new MatchAllQueryBuilder()).boost(randomFloat()); case 1: return (new IdsQueryBuilder()).boost(randomFloat()); case 2: return (new TermQueryBuilder( randomAsciiOfLengthBetween(1, 10), randomDouble()).boost(randomFloat())); default: throw new IllegalStateException("Only three query builders supported for testing sort"); } }
public void testHasParentInnerQueryType() { assertAcked(prepareCreate("test").addMapping("parent-type").addMapping("child-type", "_parent", "type=parent-type")); client().prepareIndex("test", "child-type", "child-id").setParent("parent-id").setSource("{}", XContentType.JSON).get(); client().prepareIndex("test", "parent-type", "parent-id").setSource("{}", XContentType.JSON).get(); refresh(); //make sure that when we explicitly set a type, the inner query is executed in the context of the parent type instead SearchResponse searchResponse = client().prepareSearch("test").setTypes("child-type").setQuery( QueryBuilders.hasParentQuery("parent-type", new IdsQueryBuilder().addIds("parent-id"), false)).get(); assertSearchHits(searchResponse, "child-id"); }
public void testHasChildInnerQueryType() { assertAcked(prepareCreate("test").addMapping("parent-type").addMapping("child-type", "_parent", "type=parent-type")); client().prepareIndex("test", "child-type", "child-id").setParent("parent-id").setSource("{}", XContentType.JSON).get(); client().prepareIndex("test", "parent-type", "parent-id").setSource("{}", XContentType.JSON).get(); refresh(); //make sure that when we explicitly set a type, the inner query is executed in the context of the child type instead SearchResponse searchResponse = client().prepareSearch("test").setTypes("parent-type").setQuery( QueryBuilders.hasChildQuery("child-type", new IdsQueryBuilder().addIds("child-id"), ScoreMode.None)).get(); assertSearchHits(searchResponse, "parent-id"); }
@Override public Map<String, Object> findProductById(Id productId) { Client client = ElasticSearch.CLIENT.get(); ApplicationContext appCtx = app.context(); Merchant merchant = appCtx.getMerchant(); Store store = appCtx.getStore(); IdsQueryBuilder qb = QueryBuilders.idsQuery(SEARCH_TYPE_PRODUCT); qb.addIds(productId.str()); SearchRequestBuilder searchRequestBuilder = client .prepareSearch(elasticsearchIndexHelper.indexName(merchant.getId(), store.getId(), Product.class)) .setTypes(Annotations.getIndexedCollectionName(Product.class)) .setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(qb).setExplain(false); SearchResponse response = searchRequestBuilder.execute().actionGet(); SearchHits hits = response.getHits(); Map<String, Object> productData = null; if (hits.getTotalHits() > 0) { SearchHit searchHit = hits.getAt(0); if (searchHit != null) productData = searchHit.getSource(); } return productData; }
protected SearchRequest toSearchRequest(final RestRequest request) { final SearchRequest searchRequest = new SearchRequest(); searchRequest.routing(request.param("routing")); searchRequest.copyContextFrom(request); searchRequest.preference(request.param("preference")); searchRequest.indices(request.param("index")); searchRequest.types(request.param("type")); searchRequest.source(SearchSourceBuilder.searchSource().query( new IdsQueryBuilder(request.param("type")).addIds(request.param("id")))); return searchRequest; }
protected MultiSearchRequest toMultiSearchRequest(final RestRequest request) throws Exception { final MultiGetRequest multiGetRequest = new MultiGetRequest(); multiGetRequest.refresh(request.paramAsBoolean("refresh", multiGetRequest.refresh())); multiGetRequest.preference(request.param("preference")); multiGetRequest.realtime(request.paramAsBoolean("realtime", null)); multiGetRequest.ignoreErrorsOnGeneratedFields(request.paramAsBoolean("ignore_errors_on_generated_fields", false)); String[] sFields = null; final String sField = request.param("fields"); if (sField != null) { sFields = Strings.splitStringByCommaToArray(sField); } final FetchSourceContext defaultFetchSource = FetchSourceContext.parseFromRestRequest(request); multiGetRequest.add(request.param("index"), request.param("type"), sFields, defaultFetchSource, request.param("routing"), RestActions.getRestContent(request), settings.getAsBoolean(AbstractACRestFilter.REST_ACTION_MULTI_ALLOW_EXPLICIT_INDEX, true)); final MultiSearchRequest msearch = new MultiSearchRequest(); msearch.copyContextFrom(request); for (final Iterator<Item> iterator = multiGetRequest.iterator(); iterator.hasNext();) { final Item item = iterator.next(); final SearchRequest st = new SearchRequest(); st.routing(item.routing()); st.indices(item.indices()); st.types(item.type()); st.preference(request.param("preference")); st.source(SearchSourceBuilder.searchSource().query(new IdsQueryBuilder(item.type()).addIds(item.id()))); msearch.add(st); } return msearch; }
protected SearchRequest toSearchRequest(final GetRequest request) { final SearchRequest searchRequest = new SearchRequest(); searchRequest.routing(request.routing()); searchRequest.copyContextFrom(request); searchRequest.preference(request.preference()); searchRequest.indices(request.indices()); searchRequest.types(request.type()); searchRequest.source(SearchSourceBuilder.searchSource().query(new IdsQueryBuilder(request.type()).addIds(request.id()))); return searchRequest; }
/** * Return the search hit based on the UUID and sensor type. * A callback can be specified to transform the hit into a type T. * If more than one hit happens, the first one will be returned. */ <T> List<T> searchByGuids(Collection<String> guids, Collection<String> sensorTypes, Function<SearchHit, Optional<T>> callback) { if(guids == null || guids.isEmpty()) { return Collections.EMPTY_LIST; } QueryBuilder query = null; IdsQueryBuilder idsQuery = null; if (sensorTypes != null) { String[] types = sensorTypes.stream().map(sensorType -> sensorType + "_doc").toArray(String[]::new); idsQuery = QueryBuilders.idsQuery(types); } else { idsQuery = QueryBuilders.idsQuery(); } for(String guid : guids) { query = idsQuery.addIds(guid); } SearchRequestBuilder request = client.prepareSearch() .setQuery(query) .setSize(guids.size()) ; org.elasticsearch.action.search.SearchResponse response = request.get(); SearchHits hits = response.getHits(); List<T> results = new ArrayList<>(); for (SearchHit hit : hits) { Optional<T> result = callback.apply(hit); if (result.isPresent()) { results.add(result.get()); } } return results; }
/** * simple match * use whitespace analyzer * @param field * @param phrase already stemmed * @param ids * @param operator * @return */ public SearchResponse match(String field, String phrase, String[] ids, Operator operator){ IdsQueryBuilder idsFilterBuilder = new IdsQueryBuilder(documentType); idsFilterBuilder.addIds(ids); SearchResponse response = client.prepareSearch(indexName).setSize(ids.length). setTrackScores(false). setFetchSource(false).setExplain(false).setFetchSource(false). setQuery(QueryBuilders.boolQuery() .should(QueryBuilders.matchQuery(field, phrase) .operator(operator).analyzer("whitespace")) .must(idsFilterBuilder)) .execute().actionGet(); return response; }
public static void deleteByIds( Client client, String index, String type, Set<String> ids, EzSecurityToken token, VisibilityFilterConfig filterConfig) throws TException { final SearchRequestBuilder idRequest = createIdSearch(client, index, type, ids, token, filterConfig); idRequest.setNoFields(); final SearchResponse idResponse = idRequest.get(); final IdsQueryBuilder deleteQuery = QueryBuilders.idsQuery(type); for (final SearchHit hit : idResponse.getHits().getHits()) { deleteQuery.addIds(hit.getId()); } client.prepareDeleteByQuery(index).setQuery(deleteQuery).get(); refreshIndex(client, index); }
public QueryBuilder buildIdsQuery(Collection<String> ids) { IdsQueryBuilder queryBuilder = QueryBuilders.idsQuery(getIndexType()); queryBuilder.ids().addAll(ids); return queryBuilder; }
private void registerQueryParsers(List<SearchPlugin> plugins) { registerQuery(new QuerySpec<>(MatchQueryBuilder.NAME, MatchQueryBuilder::new, MatchQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MatchPhraseQueryBuilder.NAME, MatchPhraseQueryBuilder::new, MatchPhraseQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MatchPhrasePrefixQueryBuilder.NAME, MatchPhrasePrefixQueryBuilder::new, MatchPhrasePrefixQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MultiMatchQueryBuilder.NAME, MultiMatchQueryBuilder::new, MultiMatchQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(NestedQueryBuilder.NAME, NestedQueryBuilder::new, NestedQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(HasChildQueryBuilder.NAME, HasChildQueryBuilder::new, HasChildQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(HasParentQueryBuilder.NAME, HasParentQueryBuilder::new, HasParentQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(DisMaxQueryBuilder.NAME, DisMaxQueryBuilder::new, DisMaxQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(IdsQueryBuilder.NAME, IdsQueryBuilder::new, IdsQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MatchAllQueryBuilder.NAME, MatchAllQueryBuilder::new, MatchAllQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(QueryStringQueryBuilder.NAME, QueryStringQueryBuilder::new, QueryStringQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(BoostingQueryBuilder.NAME, BoostingQueryBuilder::new, BoostingQueryBuilder::fromXContent)); BooleanQuery.setMaxClauseCount(INDICES_MAX_CLAUSE_COUNT_SETTING.get(settings)); registerQuery(new QuerySpec<>(BoolQueryBuilder.NAME, BoolQueryBuilder::new, BoolQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(TermQueryBuilder.NAME, TermQueryBuilder::new, TermQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(TermsQueryBuilder.NAME, TermsQueryBuilder::new, TermsQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(FuzzyQueryBuilder.NAME, FuzzyQueryBuilder::new, FuzzyQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(RegexpQueryBuilder.NAME, RegexpQueryBuilder::new, RegexpQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(RangeQueryBuilder.NAME, RangeQueryBuilder::new, RangeQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(PrefixQueryBuilder.NAME, PrefixQueryBuilder::new, PrefixQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(WildcardQueryBuilder.NAME, WildcardQueryBuilder::new, WildcardQueryBuilder::fromXContent)); registerQuery( new QuerySpec<>(ConstantScoreQueryBuilder.NAME, ConstantScoreQueryBuilder::new, ConstantScoreQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanTermQueryBuilder.NAME, SpanTermQueryBuilder::new, SpanTermQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanNotQueryBuilder.NAME, SpanNotQueryBuilder::new, SpanNotQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanWithinQueryBuilder.NAME, SpanWithinQueryBuilder::new, SpanWithinQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanContainingQueryBuilder.NAME, SpanContainingQueryBuilder::new, SpanContainingQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(FieldMaskingSpanQueryBuilder.NAME, FieldMaskingSpanQueryBuilder::new, FieldMaskingSpanQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanFirstQueryBuilder.NAME, SpanFirstQueryBuilder::new, SpanFirstQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanNearQueryBuilder.NAME, SpanNearQueryBuilder::new, SpanNearQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanOrQueryBuilder.NAME, SpanOrQueryBuilder::new, SpanOrQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MoreLikeThisQueryBuilder.NAME, MoreLikeThisQueryBuilder::new, MoreLikeThisQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(WrapperQueryBuilder.NAME, WrapperQueryBuilder::new, WrapperQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(CommonTermsQueryBuilder.NAME, CommonTermsQueryBuilder::new, CommonTermsQueryBuilder::fromXContent)); registerQuery( new QuerySpec<>(SpanMultiTermQueryBuilder.NAME, SpanMultiTermQueryBuilder::new, SpanMultiTermQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(FunctionScoreQueryBuilder.NAME, FunctionScoreQueryBuilder::new, FunctionScoreQueryBuilder::fromXContent)); registerQuery( new QuerySpec<>(SimpleQueryStringBuilder.NAME, SimpleQueryStringBuilder::new, SimpleQueryStringBuilder::fromXContent)); registerQuery(new QuerySpec<>(TypeQueryBuilder.NAME, TypeQueryBuilder::new, TypeQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(ScriptQueryBuilder.NAME, ScriptQueryBuilder::new, ScriptQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(GeoDistanceQueryBuilder.NAME, GeoDistanceQueryBuilder::new, GeoDistanceQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(GeoBoundingBoxQueryBuilder.NAME, GeoBoundingBoxQueryBuilder::new, GeoBoundingBoxQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(GeoPolygonQueryBuilder.NAME, GeoPolygonQueryBuilder::new, GeoPolygonQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(ExistsQueryBuilder.NAME, ExistsQueryBuilder::new, ExistsQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MatchNoneQueryBuilder.NAME, MatchNoneQueryBuilder::new, MatchNoneQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(ParentIdQueryBuilder.NAME, ParentIdQueryBuilder::new, ParentIdQueryBuilder::fromXContent)); if (ShapesAvailability.JTS_AVAILABLE && ShapesAvailability.SPATIAL4J_AVAILABLE) { registerQuery(new QuerySpec<>(GeoShapeQueryBuilder.NAME, GeoShapeQueryBuilder::new, GeoShapeQueryBuilder::fromXContent)); } registerFromPlugin(plugins, SearchPlugin::getQueries, this::registerQuery); }
@Test public void testSecureRandomScript1() throws Exception { // Create a new index final String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") .startObject("name").field("type", "string").endObject().startObject("number").field("type", "integer").endObject() .endObject().endObject().endObject().string(); assertAcked(prepareCreate("test").addMapping("type", mapping)); assertAcked(prepareCreate("test1").addMapping("type", mapping)); index("test", "type", "1doc", "{\"a\":1}"); index("test", "type", "2doc", "{\"a\":2}"); index("test", "type", "3doc", "{\"a\":3}"); index("test", "type", "4doc", "{\"a\":4}"); index("test", "type", "5doc", "{\"a\":5,\"b\":3}"); for (int i = 0; i < 30; i++) { client().prepareIndex("test", "type").setSource("{\"dummy\":0}").get(); } final int bulkCount = 4000; final List<IndexRequestBuilder> indexBuilders = new ArrayList<IndexRequestBuilder>(); for (int i = 0; i < bulkCount; i++) { indexBuilders.add(client().prepareIndex("test", "type", Integer.toString(i)).setSource( XContentFactory.jsonBuilder().startObject().field("name", "rec " + i).field("number", i).endObject())); } indexRandom(true, false, indexBuilders); client().prepareUpdate("test", "type", "5doc").setScript("ctx._source.b++", ScriptType.INLINE).get(); client().prepareIndex("test", "type", "4doc").setSource("{\"a\":44}").get(); client().prepareDelete("test", "type", "4doc").get(); client().prepareDeleteByQuery("test", "test1").setQuery(new IdsQueryBuilder("type").addIds("1doc", "2doc")).get(); refresh(); SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).execute().actionGet(); assertNoFailures(searchResponse); assertHitCountWithMsg("testsearch", searchResponse, 32 + bulkCount); int flushCount = 0; do { final FlushResponse res = client().admin().cluster().execute(FlushAction.INSTANCE, new FlushRequest()).actionGet(); flushCount = res.getCount(); refresh(); logger.info("Flushed {} msgs", flushCount); } while (flushCount > 0); searchResponse = client().prepareSearch("audit").setQuery(matchAllQuery()).execute().actionGet(); assertNoFailures(searchResponse); System.out.println("Audit search result " + searchResponse.getHits().totalHits()); //assertHitCountWithMsg("auditsearch",searchResponse, flushCount+bulkCount); }
public SearchResponse spanNot(SpanNotNgram ngram, String[] ids){ Ngram include = ngram.getInclude(); String field1 = include.getField(); int slop1 = include.getSlop(); boolean inOrder1 = include.isInOrder(); SpanNearQueryBuilder queryBuilder1 = QueryBuilders.spanNearQuery(new SpanTermQueryBuilder(field1, include.getTerms()[0]), slop1); for (int i = 1; i < include.getTerms().length; i++){ queryBuilder1.addClause(new SpanTermQueryBuilder(field1, include.getTerms()[i])); } queryBuilder1.inOrder(inOrder1); Ngram exclude = ngram.getExclude(); String field2 = exclude.getField(); int slop2 = exclude.getSlop(); boolean inOrder2 = exclude.isInOrder(); SpanNearQueryBuilder queryBuilder2 = QueryBuilders.spanNearQuery(new SpanTermQueryBuilder(field2, exclude.getTerms()[0]), slop2); for (int i = 1; i < exclude.getTerms().length; i++){ queryBuilder2.addClause(new SpanTermQueryBuilder(field2, exclude.getTerms()[i])); } queryBuilder2.inOrder(inOrder2); int pre = ngram.getPre(); int post = ngram.getPost(); SpanNotQueryBuilder spanNotQueryBuilder = new SpanNotQueryBuilder(queryBuilder1, queryBuilder2); //todo upgrade to 1.5 // .pre(pre).post(post); IdsQueryBuilder idsFilterBuilder = new IdsQueryBuilder(documentType); idsFilterBuilder.addIds(ids); SearchResponse response = client.prepareSearch(indexName).setSize(ids.length). setTrackScores(false). setFetchSource(false).setExplain(false).setFetchSource(false). setQuery(QueryBuilders.boolQuery() .must(idsFilterBuilder) .should(spanNotQueryBuilder)) .execute().actionGet(); return response; }
/** * Creates query for specific id of type. * * @param type * type name * @param ids * list of ids * @return query for id */ protected QueryBuilder buildIdQuery(String type, List<String> ids) { return new IdsQueryBuilder().types(type).addIds(ids.toArray(new String[1])); }