public PageableList<NamedQuery> filter(String prefix) { BoolQueryBuilder filter = QueryBuilders.boolQuery(); if (!Strings.isNullOrEmpty(prefix)) { filter.must(QueryBuilders .prefixQuery("name", prefix.trim())); } SearchResponse response = getConnection().getClient().prepareSearch(getIndex()) .setTypes(getType()) .setPostFilter(filter) .setSize(100) .setFetchSource(true) .setExplain(false) .execute() .actionGet(); List<NamedQuery> items = Arrays.stream(response.getHits().getHits()) .map(SearchHit::getSource) .filter(Objects::nonNull) .map(this::mapToNamedQuery) .collect(Collectors.toList()); return PageableList.create(items, response.getHits().getTotalHits()); }
public List<String> suggest(String prefix) { CompletionSuggestionBuilder suggestionBuilder = new CompletionSuggestionBuilder("name_suggest") .prefix(prefix); SearchResponse response = getConnection().getClient().prepareSearch(getIndex()) .setTypes(getType()) .suggest(new SuggestBuilder().addSuggestion("suggestion", suggestionBuilder)) .setSize(100) .setFetchSource(true) .setExplain(false) .execute() .actionGet(); return response.getSuggest().filter(CompletionSuggestion.class).stream() .flatMap(s -> s.getOptions().stream()) .sorted(Comparator.comparingDouble(Suggest.Suggestion.Entry.Option::getScore)) .map(Suggest.Suggestion.Entry.Option::getText) .map(Text::toString) .collect(Collectors.toList()); }
public List<NamedQuery> all() { Client client = getConnection().getClient(); TimeValue keepAlive = TimeValue.timeValueMinutes(10); SearchResponse response = client.prepareSearch(getIndex()) .setTypes(getType()) .setQuery(QueryBuilders.matchAllQuery()) .setSize(100) .setScroll(keepAlive) .setFetchSource(true) .setExplain(false) .execute() .actionGet(); List<NamedQuery> result = Lists.newArrayList(); do { result.addAll(Arrays.stream(response.getHits().getHits()) .map(SearchHit::getSource) .filter(Objects::nonNull) .map(this::mapToNamedQuery) .collect(Collectors.toList())); response = client.prepareSearchScroll(response.getScrollId()) .setScroll(keepAlive) .execute() .actionGet(); } while (response.getHits().getHits().length != 0); return result; }
public void deleteAll() { Client client = getConnection().getClient(); TimeValue keepAlive = TimeValue.timeValueMinutes(10); SearchResponse response = client.prepareSearch(getIndex()) .setTypes(getType()) .setPostFilter(QueryBuilders.matchAllQuery()) .setSize(100) .setScroll(keepAlive) .setFetchSource(true) .setExplain(false) .execute() .actionGet(); do { Arrays.stream(response.getHits().getHits()) .map(SearchHit::getSource) .filter(Objects::nonNull) .map(this::mapToNamedQuery) .forEach(this::delete); response = client.prepareSearchScroll(response.getScrollId()) .setScroll(keepAlive) .execute() .actionGet(); } while (response.getHits().getHits().length != 0); }
public NamedQuery get(String name) { GetResponse response = getConnection().getClient() .prepareGet(getIndex(), getType(), name.toLowerCase()) .setFetchSource(true) .get(); if (response.isExists()) { return mapToNamedQuery(response.getSource()); } return null; }
public void delete(NamedQuery nq) { if (nq != null && nq.getName() != null) { getConnection().getClient().prepareDelete(getIndex(), getType(), nq.getName()).get(); } }
public void save(NamedQuery nq) { try { getConnection().getClient().prepareIndex(getIndex(), getType(), nq.getName().toLowerCase()) .setSource(jsonBuilder() .startObject() .field("name", nq.getName()) .startObject("name_suggest") .field("input", Lists.newArrayList(nq.getName())) .endObject() .field("stemmed_case_sensitive", nq.getStemmedCaseSensitive()) .field("stemmed_case_insensitive", nq.getStemmedCaseInSensitive()) .field("not_stemmed_case_sensitive", nq.getNotStemmedCaseSensitive()) .field("not_stemmed_case_insensitive", nq.getNotStemmedCaseInSensitive()) .field("advanced", nq.getAdvanced()) .field("updated", new Date()) .endObject()) .get(); } catch (IOException e) { LOG.error("Failed to save HTTP source test with url '{}'", nq.getName()); } }