public static EsHttpSourceOperations getInstance(ElasticConnection connection, String index, String type) { return new EsHttpSourceOperations(connection, index, type); }
public HttpSource load(String url) { return operations.get(url); } });
public void delete(String url) { if (url != null) { getConnection().getClient().prepareDelete(getIndex(), getType(), url.toLowerCase()).get(); } }
public HttpSource get(String url) { GetResponse response = getConnection().getClient().prepareGet(getIndex(), getType(), url) .setFetchSource(true) .get(); if (response.isExists()) { return mapToHttpSource(response.getSource()); } return null; }
public static EsHttpSourceOperations getHttpSourceOperations() { if (HTTP_SOURCE_OPERATIONS == null) { String index = Configuration.INSTANCE.getString(ElasticConstants.ES_HTTP_SOURCES_INDEX_NAME_PARAM); String type = Configuration.INSTANCE.getString(ElasticConstants.ES_HTTP_SOURCES_DOC_TYPE_PARAM); HTTP_SOURCE_OPERATIONS = EsHttpSourceOperations.getInstance(getEsConnection(), index, type); } return HTTP_SOURCE_OPERATIONS; }
public List<HttpSource> findEnabledSources() { BoolQueryBuilder filter = QueryBuilders.boolQuery() .must(QueryBuilders.termQuery("enabled", true)); SearchResponse response = getConnection().getClient() .prepareSearch(getIndex()) .setTypes(getType()) .setSearchType(SearchType.DEFAULT) .setPostFilter(filter) .addSort("updated", SortOrder.ASC) .setSize(10000) .setFetchSource(true) .setExplain(false) .execute() .actionGet(); SearchHits hits = response.getHits(); return Arrays.stream(hits.getHits()) .map(SearchHit::sourceAsMap) .map(this::mapToHttpSource) .collect(Collectors.toList()); }
public PageableList<HttpSource> filter(String text) { BoolQueryBuilder filter = QueryBuilders.boolQuery(); if (!Strings.isNullOrEmpty(text)) { filter.must(QueryBuilders .queryStringQuery(QueryParser.escape(text.trim())) .field("search_field") .defaultOperator(QueryStringQueryBuilder.DEFAULT_OPERATOR.AND)); } SearchResponse response = getConnection().getClient().prepareSearch(getIndex()) .setTypes(getType()) .setPostFilter(filter) .setSize(100) .setFetchSource(true) .setExplain(false) .execute() .actionGet(); List<HttpSource> items = Arrays.stream(response.getHits().getHits()) .map(SearchHit::getSource) .filter(Objects::nonNull) .map(this::mapToHttpSource) .sorted((o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName())) .collect(Collectors.toList()); return PageableList.create(items, response.getHits().getTotalHits()); }
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::mapToHttpSource) .map(HttpSource::getUrl) .forEach(this::delete); response = client.prepareSearchScroll(response.getScrollId()) .setScroll(keepAlive) .execute() .actionGet(); } while (response.getHits().getHits().length != 0); }
public List<HttpSource> all() { BoolQueryBuilder filter = QueryBuilders.boolQuery() .must(QueryBuilders.existsQuery("url")) .must(QueryBuilders.existsQuery("name")); Client client = getConnection().getClient(); TimeValue keepAlive = TimeValue.timeValueMinutes(10); SearchResponse response = client.prepareSearch(getIndex()) .setTypes(getType()) .setPostFilter(filter) .setSize(100) .setScroll(keepAlive) .setFetchSource(true) .setExplain(false) .execute() .actionGet(); List<HttpSource> result = Lists.newArrayList(); do { result.addAll(Arrays.stream(response.getHits().getHits()) .map(SearchHit::getSource) .filter(Objects::nonNull) .map(this::mapToHttpSource) .collect(Collectors.toList())); response = client.prepareSearchScroll(response.getScrollId()) .setScroll(keepAlive) .execute() .actionGet(); } while (response.getHits().getHits().length != 0); return result; }
public void save(HttpSource source) { try { String url = source.getUrl().toLowerCase(); getConnection().getClient().prepareIndex(getIndex(), getType(), url) .setSource(jsonBuilder() .startObject()