private static SearchResponse searchByAddress( final String aIndex, final Client aClient, final String aType, SearchType aSearchType, QueryBuilder aQuery) { SearchRequestBuilder searchRequest = aClient.prepareSearch(aIndex); if (aType != null) { searchRequest.setTypes(aType); } if (aSearchType != null) { searchRequest.setSearchType(aSearchType); } searchRequest.setQuery(aQuery); return searchRequest.execute().actionGet(); }
public List<String> autocomplete(String prefix, int limit) { SearchRequestBuilder builder = new SearchRequestBuilder(factory.client()); builder.setIndices(Constants.INDEX).setTypes(Constants.TYPE); builder.setSearchType(SearchType.DEFAULT); builder.setFrom(0).setSize(limit).setExplain(IS_EXPLAIN); QueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("title", prefix); builder.setQuery(queryBuilder.toString()); logger.info("Search qeury"); logger.info(builder.toString()); SearchResponse sr = builder.execute().actionGet(); logger.info("Search response"); logger.info(sr.toString()); Iterator<SearchHit> it = sr.getHits().iterator(); while (it.hasNext()) { SearchHit hit = it.next(); hit.getSource(); } return Arrays.asList("a", "b", "c"); }
public Iterator<Blog> search(String keyword, int start, int size) { SearchRequestBuilder builder = new SearchRequestBuilder(factory.client()); builder.setIndices(Constants.INDEX).setTypes(Constants.TYPE); builder.setSearchType(SearchType.DEFAULT); builder.setFrom(start).setSize(size).setExplain(IS_EXPLAIN); BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().minimumNumberShouldMatch(1); queryBuilder.should(QueryBuilders.matchPhraseQuery("title", keyword)); queryBuilder.should(QueryBuilders.matchPhraseQuery("content", keyword)); queryBuilder.should(QueryBuilders.matchPhraseQuery("createPlace", keyword)); builder.setQuery(queryBuilder.toString()); logger.info("Search qeury"); logger.info(builder.toString()); SearchResponse sr = builder.execute().actionGet(); logger.info("Search response"); logger.info(sr.toString()); try { return new IteratorImpl<Blog>( sr.getHits().iterator(), Blog.class.getConstructor(SearchHit.class)); } catch (NoSuchMethodException | SecurityException e) { throw new RuntimeException(e); } }
public void testScore() throws Exception { createIndex("test"); ensureGreen("test"); indexRandom( true, client().prepareIndex("test", "doc", "1").setSource("text", "hello goodbye"), client().prepareIndex("test", "doc", "2").setSource("text", "hello hello hello goodbye"), client().prepareIndex("test", "doc", "3").setSource("text", "hello hello goodebye")); ScoreFunctionBuilder score = ScoreFunctionBuilders.scriptFunction( new Script("1 / _score", ScriptType.INLINE, "expression", null)); SearchRequestBuilder req = client().prepareSearch().setIndices("test"); req.setQuery( QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("text", "hello"), score) .boostMode("replace")); req.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); // make sure DF is consistent SearchResponse rsp = req.get(); assertSearchResponse(rsp); SearchHits hits = rsp.getHits(); assertEquals(3, hits.getTotalHits()); assertEquals("1", hits.getAt(0).getId()); assertEquals("3", hits.getAt(1).getId()); assertEquals("2", hits.getAt(2).getId()); }
@Override public SqlElasticSearchRequestBuilder explain() throws SqlParseException { this.request = client.prepareSearch(); request.setListenerThreaded(false); setIndicesAndTypes(); setWhere(select.getWhere()); AggregationBuilder<?> lastAgg = null; for (List<Field> groupBy : select.getGroupBys()) { if (!groupBy.isEmpty()) { Field field = groupBy.get(0); lastAgg = aggMaker.makeGroupAgg(field); if (lastAgg != null && lastAgg instanceof TermsBuilder && !(field instanceof MethodField)) { ((TermsBuilder) lastAgg).size(select.getRowCount()); } if (field.isNested()) { AggregationBuilder nestedBuilder = createNestedAggregation(field); if (insertFilterIfExistsAfter(lastAgg, groupBy, nestedBuilder, 1)) { groupBy.remove(1); } else { nestedBuilder.subAggregation(lastAgg); } request.addAggregation(wrapNestedIfNeeded(nestedBuilder, field.isReverseNested())); } else { request.addAggregation(lastAgg); } for (int i = 1; i < groupBy.size(); i++) { field = groupBy.get(i); AggregationBuilder<?> subAgg = aggMaker.makeGroupAgg(field); if (subAgg instanceof TermsBuilder && !(field instanceof MethodField)) { ((TermsBuilder) subAgg).size(0); } if (field.isNested()) { AggregationBuilder nestedBuilder = createNestedAggregation(field); if (insertFilterIfExistsAfter(subAgg, groupBy, nestedBuilder, i + 1)) { groupBy.remove(i + 1); i++; } else { nestedBuilder.subAggregation(subAgg); } lastAgg.subAggregation(wrapNestedIfNeeded(nestedBuilder, field.isReverseNested())); } else { lastAgg.subAggregation(subAgg); } lastAgg = subAgg; } } } Map<String, KVValue> groupMap = aggMaker.getGroupMap(); // add field if (select.getFields().size() > 0) { setFields(select.getFields()); explanFields(request, select.getFields(), lastAgg); } // add order if (lastAgg != null && select.getOrderBys().size() > 0) { for (Order order : select.getOrderBys()) { KVValue temp = groupMap.get(order.getName()); if (temp != null) { TermsBuilder termsBuilder = (TermsBuilder) temp.value; switch (temp.key) { case "COUNT": termsBuilder.order(Terms.Order.count(isASC(order))); break; case "KEY": termsBuilder.order(Terms.Order.term(isASC(order))); // add the sort to the request also so the results get sorted as well request.addSort(order.getName(), SortOrder.valueOf(order.getType())); break; case "FIELD": termsBuilder.order(Terms.Order.aggregation(order.getName(), isASC(order))); break; default: throw new SqlParseException(order.getName() + " can not to order"); } } else { request.addSort(order.getName(), SortOrder.valueOf(order.getType())); } } } setLimitFromHint(this.select.getHints()); request.setSearchType(SearchType.DEFAULT); updateRequestWithIndexAndRoutingOptions(select, request); SqlElasticSearchRequestBuilder sqlElasticRequestBuilder = new SqlElasticSearchRequestBuilder(request); return sqlElasticRequestBuilder; }