@Override public <E> List<E> getColumnsById(String schemaName, String tableName, String pKeyColumnName, String columnName, Object pKeyColumnValue, Class columnJavaType) { // fetch list ADDRESS_ID for given PERSON_ID QueryBuilder filterBuilder = new TermQueryBuilder(pKeyColumnName, pKeyColumnValue); SearchResponse response = txClient.prepareSearch(schemaName.toLowerCase()).setTypes(tableName) .setPostFilter(filterBuilder).addField(columnName).execute().actionGet(); SearchHits hits = response.getHits(); List columns = new ArrayList(); for (SearchHit hit : hits.getHits()) { Map<String, SearchHitField> fields = hit.getFields(); columns.add(fields.get(columnName).getValue()); } return columns; }
@Override public <V> V value() { return this.searchHitField.value(); }
if (response.getHits() != null) { for (SearchHit hit : response.getHits()) { ESSearchHit esSearchHit = new ESSearchHit(); if (!hit.getFields().isEmpty()) { Map<String, ESDocumentField> esFields = new HashMap<>(); for (Map.Entry<String, SearchHitField> entry : hit.getFields().entrySet()) { esFields.put(entry.getKey(), new ESDocumentField(entry.getKey(), entry.getValue().getValues())); esSearchHit.setIndex(hit.getIndex()); esSearchHit.setId(hit.getId()); esSearchHit.setSourceAsMap(hit.getSourceAsMap()); esSearchResponse.setTotalHits(response.getHits().getTotalHits()); if (response.getAggregations() != null) {
final SearchHits hits = response.getHits(); if (hits.getTotalHits() != 0) { final SearchHit[] searchHits = hits.getHits(); final SearchHitField field = searchHits[0].getFields().get( userIdField); if (field != null) { currentId = field.getValue(); client.prepareSearch(index).setTypes(type) .setQuery(QueryBuilders.matchAllQuery()).addField(userIdField) .addSort(userIdField, SortOrder.DESC).setSize(1) .execute(on(responseListener, failureListener));
RangeFilterBuilder filterBuilder = rangeFilter("timestamp").gte(lastSeen); SearchResponse searchResponse = client.prepareSearch() .setIndices(pubSubIndexName) .setTypes("publish") .setSearchType(SearchType.SCAN) .setScroll(scrollTimeout) .setQuery(queryBuilder) .setSize(scrollSize) .execute().actionGet(); boolean failed = searchResponse.getFailedShards() > 0 || searchResponse.isTimedOut(); if (failed) { logger.error("searching for messages for topic {} failed: failed shards={} timeout={}", topic, searchResponse.getFailedShards(), searchResponse.isTimedOut()); return; long totalHits = searchResponse.getHits().getTotalHits(); boolean zero = totalHits == 0L; if (zero) { .execute().actionGet(); for (SearchHit hit : searchResponse.getHits()) { Long timestamp = (Long) hit.field("timestamp").getValues().get(0); Map<String, Object> data = hit.field("data").getValue(); channel.write(new NettyInteractiveResponse("message", createPublishMessage(timestamp, data)).response()); if (searchResponse.getHits().hits().length == 0) { break;
response = client .prepareSearch(preferenceIndex) .setTypes(preferenceType) .setQuery(QueryBuilders.boolQuery() .must(QueryBuilders.termQuery(itemIdField, itemID)) .must(QueryBuilders.termQuery(userIdField, userID)) .filter(getLastAccessedFilterQuery())) .addFields(timestampField) .addSort(timestampField, SortOrder.DESC).setSize(1) .execute().actionGet(); final SearchHits hits = response.getHits(); final long totalHits = hits.getTotalHits(); if (totalHits == 0) { return null; final SearchHit[] searchHits = hits.getHits(); if (searchHits.length > 0) { final SearchHitField result = searchHits[0].field(timestampField); if (result != null) { final Date date = result.getValue(); final long time = date.getTime(); if (cache != null) {
public void addDeletion(SearchRequestBuilder searchRequest) { .addSort("_doc", SortOrder.ASC) .setScroll(TimeValue.timeValueMinutes(5)) .setSize(100) .setFetchSource(false); SearchHit[] hits = searchResponse.getHits().getHits(); for (SearchHit hit : hits) { SearchHitField routing = hit.getField("_routing"); DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete(hit.getIndex(), hit.getType(), hit.getId()); if (routing != null) { deleteRequestBuilder.setRouting(routing.getValue()); String scrollId = searchResponse.getScrollId(); if (scrollId == null) { break;
while (true) { if (response == null) { response = client.prepareSearch(index).setTypes(type) .setScroll(new TimeValue(keepAlive.longValue())) .setQuery(QueryBuilders.queryStringQuery(userQuery)) .addField(fieldName).setSize(size.intValue()).execute() .actionGet(); } else { response = client.prepareSearchScroll(response.getScrollId()) .setScroll(new TimeValue(keepAlive.longValue())) .execute().actionGet(); final SearchHits hits = response.getHits(); if (targetIDs == null) { targetIDs = new long[(int) hits.getTotalHits()]; if (logger.isDebugEnabled()) { logger.debug("{} users are found by {}", hits.getTotalHits(), userQuery); if (hits.getHits().length == 0) { break; final SearchHitField searchHitField = hit.getFields().get( fieldName); final Number value = searchHitField.getValue(); targetIDs[count] = value.longValue(); count++;
@Override public Set<String> getFolderNames() throws IOException, MessagingException { createIndexIfNotExists(); client.admin().indices().refresh(new RefreshRequest()).actionGet(); final HashSet<String> uids = new HashSet<String>(); SearchResponse scrollResp = client.prepareSearch().setIndices(index).setTypes(type).setSearchType(SearchType.SCAN) .setQuery(QueryBuilders.matchAllQuery()).addField("folderFullName").setScroll(new TimeValue(1000)).setSize(1000).execute() .actionGet(); while (true) { scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(1000)).execute().actionGet(); boolean hitsRead = false; for (final SearchHit hit : scrollResp.getHits()) { hitsRead = true; uids.add((String) hit.getFields().get("folderFullName").getValue()); } if (!hitsRead) { break; } } if (logger.isDebugEnabled()) { logger.debug("Currently locally stored folders: {}", uids); } return uids; }
final SearchHits hits = searchResponse.getHits(); if (hits.totalHits() == 0) { onError(channel, new NotFoundException("No " + info.getIdField() final SearchHit[] searchHits = hits.getHits(); final long[] targetIds = new long[hits.getHits().length]; for (int i = 0; i < targetIds.length && i < searchHits.length; i++) { final SearchHit hit = searchHits[i]; final SearchHitField field = hit.field(info.getIdField()); final Number targetId = field.getValue(); if (targetId != null) { targetIds[i] = targetId.longValue(); client.prepareSearch(info.getIdIndex()).setTypes(info.getIdType()) .setQuery(boolQueryBuilder).addField(info.getIdField()) .addSort(info.getTimestampField(), SortOrder.DESC) .setSize(systemIds.length)
@Override protected boolean isContentUpdated(final CrawlerClient client, final UrlQueue<?> urlQueue) { final RiverConfigManager riverConfigManager = SingletonLaContainer.getComponent(RiverConfigManager.class); final RiverConfig riverConfig = riverConfigManager.get(crawlerContext.getSessionId()); if (riverConfig.isIncremental()) { final EsClient esClient = SingletonLaContainer.getComponent(EsClient.class); try { final SearchResponse response = esClient.prepareSearch(riverConfig.getIndex()).setTypes(riverConfig.getType()) .setQuery(QueryBuilders.termQuery("url", urlQueue.getUrl())).addField("lastModified") .addSort("lastModified", SortOrder.DESC).execute().actionGet(); final SearchHits hits = response.getHits(); if (hits.getTotalHits() > 0) { final SearchHitField lastModifiedField = hits.getAt(0).getFields().get("lastModified"); if (lastModifiedField != null) { final Date lastModified = ConversionUtil.convert(lastModifiedField.getValue(), Date.class); if (lastModified != null) { urlQueue.setLastModified(lastModified.getTime()); } } } } catch (final Exception e) { logger.debug("Failed to retrieve lastModified.", e); } } return super.isContentUpdated(client, urlQueue); } }
/** * It basically searches and returns a set of tweet ids for a given keyword. * * @param keyword * @return */ public Set<String> search(String keyword) { SearchResponse response; try { response = client.prepareSearch() .setIndices("hackaton") .setTypes("tweets") .addFields("id", "text") .setQuery(QueryBuilders.fieldQuery("text", keyword)).execute().actionGet(); } catch (Throwable e) { return new HashSet<String>(); } Set<String> result = new HashSet<String>(); for (SearchHit hit : response.getHits()) { Long id = hit.field("id").<Long>getValue(); result.add(String.valueOf(id)); } return result; }
final SearchHits searchHits = response.getHits(); final SearchHit[] hits = searchHits.getHits(); if (hits.length == 0) { scrollSearchGate.countDown(); .prepareMultiTermVectors(); for (final SearchHit hit : hits) { final String id = hit.getId(); final SearchHitField searchHitField = hit.field(idField); if (searchHitField != null) { idMap.put(id, new DocInfo((String) searchHitField.getValue(), hit.getSource())); requestBuilder.execute(mTVListener); client.prepareSearchScroll(response.getScrollId()) .setScroll(new TimeValue(keepAlive.longValue())) .execute(this);
SearchHits hits = response.getHits(); results.setMaxScore(hits.getMaxScore()); results.setNumFound(hits.getTotalHits()); results.setStart(request.paramAsInt("start", 0)); for (SearchHit hit : hits.getHits()) { SolrDocument doc = new SolrDocument(); doc.addField("score", hit.score()); Map<String, SearchHitField> fields = hit.getFields(); Map<String, Object> source = hit.sourceAsMap(); if (fields.isEmpty()) { if (source != null) { for (String fieldName : fields.keySet()) { SearchHitField field = fields.get(fieldName); Object fieldValue = field.getValue();
void deleteHits(String scrollId, SearchResponse scrollResponse) { final SearchHit[] docs = scrollResponse.getHits().getHits(); final String nextScrollId = scrollResponse.getScrollId(); addShardFailures(scrollResponse.getShardFailures()); DeleteRequest delete = new DeleteRequest(request).index(doc.index()).type(doc.type()).id(doc.id()).version(doc.version()); SearchHitField routing = doc.field("_routing"); if (routing != null) { delete.routing((String) routing.value()); SearchHitField parent = doc.field("_parent"); if (parent != null) { delete.parent((String) parent.value());
@Override public List<User> search(QuotaQuery query) { Stream<User> results = new ScrollIterable(client, prepareSearch(query)) .stream() .flatMap(searchResponse -> Arrays.stream(searchResponse.getHits() .getHits())) .map(hit -> hit.field(USER)) .map(field -> (String) field.getValue()) .map(User::fromUsername) .skip(query.getOffset().getValue()); return query.getLimit().getValue() .map(results::limit) .orElse(results) .collect(Guavate.toImmutableList()); }
SearchHits hits = response.getHits(); if (fieldsToSelect != null && fieldsToSelect.length > 1 && !(fieldsToSelect[1] == null)) for (SearchHit hit : hits.getHits()) .getFields() .get(((AbstractAttribute) metaModel.entity(clazz).getAttribute(fieldsToSelect[1])) .getJPAColumnName()).getValue()); .getFields() .get(((AbstractAttribute) metaModel.entity(clazz).getAttribute(fieldsToSelect[i])) .getJPAColumnName()).getValue());
@Override public void run() throws IOException { if (isCollapseRequest() && searchResponse.hits().getHits().length > 0) { SearchRequest searchRequest = context.getRequest(); CollapseBuilder collapseBuilder = searchRequest.source().collapse(); multiRequest.maxConcurrentSearchRequests(collapseBuilder.getMaxConcurrentGroupRequests()); for (SearchHit hit : searchResponse.hits().getHits()) { BoolQueryBuilder groupQuery = new BoolQueryBuilder(); Object collapseValue = hit.field(collapseBuilder.getField()).getValue(); if (collapseValue != null) { groupQuery.filter(QueryBuilders.matchQuery(collapseBuilder.getField(), collapseValue)); ActionListener.wrap(response -> { Iterator<MultiSearchResponse.Item> it = response.iterator(); for (SearchHit hit : searchResponse.hits.getHits()) { for (InnerHitBuilder innerHitBuilder : innerHitBuilders) { MultiSearchResponse.Item item = it.next(); return; SearchHits innerHits = item.getResponse().getHits(); if (hit.getInnerHits() == null) { hit.setInnerHits(new HashMap<>(innerHitBuilders.size())); hit.getInnerHits().put(innerHitBuilder.getName(), innerHits);
for (SearchHit hit : response.getHits()) { Map<String, Object> objectMap = hit.sourceAsMap(); if(objectMap==null){ if(hit.fields()!=null){ objectMap=new HashMap<String, Object> (); for (SearchHitField filed : hit.fields().values()){ objectMap.put( filed.name(),filed.getValue()); {response.toXContent(builder, request);} channel.sendResponse(new XContentRestResponse(request, response.status(), builder)); } catch (Exception e) { if (logger.isDebugEnabled()) {
ObjectNode jsonObject; try { jsonObject = mapper.readValue(hit.getSourceAsString(), ObjectNode.class); item = new StreamsDatum(jsonObject, hit.getId()); item.getMetadata().put("id", hit.getId()); item.getMetadata().put("index", hit.getIndex()); item.getMetadata().put("type", hit.getType()); if ( hit.fields().containsKey("_timestamp")) { DateTime timestamp = new DateTime(((Long) hit.field("_timestamp").getValue()).longValue()); item.setTimestamp(timestamp); if ( hit.fields().containsKey("_parent")) { item.getMetadata().put("parent", hit.fields().get("_parent").value());