@Override public String type() { return this.searchHit.type(); }
//Scroll until no hits are returned while (true) { scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet(); //Break condition: No hits are returned if (scrollResp.getHits().getHits().length == 0) { logger.info("Closing the bulk processor"); bulkProcessor.close(); break; } // Get results from a scan search and add it to bulk ingest for (SearchHit hit: scrollResp.getHits()) { IndexRequest request = new IndexRequest("new_index", hit.type(), hit.id()); Map source = ((Map) ((Map) hit.getSource())); request.source(source); bulkProcessor.add(request); } }
private FieldMapper getMapperForField(String fieldName, SearchContext searchContext, HitContext hitContext) { DocumentMapper documentMapper = searchContext.mapperService().documentMapper(hitContext.hit().type()); // TODO: no need to lookup the doc mapper with unambiguous field names? just look at the mapper service return documentMapper.mappers().smartNameFieldMapper(fieldName); } }
private void deleteIndexType(final String fromIndex, final String fromType) { final SearchRequestBuilder builder = client.prepareSearch(fromIndex).setTypes(fromType).setScroll("1m"); SearchResponse searchResponse = builder.get(); SearchHit[] hits = searchResponse.getHits().getHits(); for (SearchHit hit : hits) client.prepareDelete(hit.index(), hit.type(), hit.id()).get(); while (hits.length != 0) { searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()).setScroll("1m").get(); hits = searchResponse.getHits().getHits(); for (SearchHit hit : hits) client.prepareDelete(hit.index(), hit.type(), hit.id()).get(); } }
@Override public GetResponse getDelegatedFromInstigator(final SearchResponse searchResponse) { if (searchResponse.getHits().getTotalHits() <= 0) { return new GetResponse(new GetResult(request.index(), request.type(), request.id(), request.version(), false, null, null)); } else if (searchResponse.getHits().getTotalHits() > 1) { throw new RuntimeException("cannot happen"); } else { final SearchHit sh = searchResponse.getHits().getHits()[0]; return new GetResponse(new GetResult(sh.index(), sh.type(), sh.id(), sh.version(), true, sh.getSourceRef(), null)); } } });
void dumpSave(Client client, Builder builder) throws IOException { TimeValue scrollDuration = TimeValue.timeValueSeconds(30); SearchResponse searchResponse = client.prepareSearch(builder.indices()) .setTypes(builder.types()).setSearchType(SearchType.SCAN) .setQuery(builder.query()).setSize(100).setScroll(scrollDuration).execute().actionGet(); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(builder.path(), true), builder.charset()); int i = 0; try { while (true) { searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()) .setScroll(scrollDuration).execute().actionGet(); if (searchResponse.getHits().hits().length == 0) { break; } for (SearchHit hit : searchResponse.getHits()) { Document document = Document.fromSource(hit.sourceAsString(), hit.index(), hit.type(), hit.id()); writer.write(document.getDump()); i++; writer.write('\n'); } } } finally { writer.close(); console.println("Saved " + i + " documents to " + builder.path()); } }
ImmutableMap.Builder<String, Object> hitProperties = ImmutableMap.builder(); hitProperties.put("_index", hit.index()); hitProperties.put("_type", hit.type()); hitProperties.put("_id", hit.id()); hitProperties.put("_version", hit.version());
@Override public void hitExecute(SearchContext context, HitContext hitContext) { if (context.storedFieldsContext() != null && context.storedFieldsContext().fetchFields() == false) { return ; } ParentFieldMapper parentFieldMapper = context.mapperService().documentMapper(hitContext.hit().type()).parentFieldMapper(); if (parentFieldMapper.active() == false) { return; } String parentId = getParentId(parentFieldMapper, hitContext.reader(), hitContext.docId()); if (parentId == null) { // hit has no _parent field. Can happen for nested inner hits if parent hit is a p/c document. return; } Map<String, SearchHitField> fields = hitContext.hit().fieldsOrNull(); if (fields == null) { fields = new HashMap<>(); hitContext.hit().fields(fields); } fields.put(ParentFieldMapper.NAME, new SearchHitField(ParentFieldMapper.NAME, Collections.singletonList(parentId))); }
@Override public GetResponse getDelegatedFromInstigator(final MultiSearchResponse searchResponse) { if (searchResponse.getResponses() == null || searchResponse.getResponses().length <= 0) { final Item item = request.getItems().get(0); return new GetResponse(new GetResult(item.index(), item.type(), item.id(), item.version(), false, null, null)); } else if (searchResponse.getResponses().length > 1) { throw new RuntimeException("cannot happen"); } else { final org.elasticsearch.action.search.MultiSearchResponse.Item item = searchResponse.getResponses()[0]; final SearchHit sh = item.getResponse().getHits().getHits()[0]; return new GetResponse(new GetResult(sh.index(), sh.type(), sh.id(), sh.version(), true, sh.getSourceRef(), null)); } } });
@Override protected IndexRequest buildIndexRequest(SearchHit doc) { IndexRequest index = new IndexRequest(mainRequest); index.index(doc.index()); index.type(doc.type()); index.id(doc.id()); index.source(doc.sourceRef()); index.versionType(VersionType.INTERNAL); index.version(doc.version()); return index; }
@Override protected IndexRequest buildIndexRequest(SearchHit doc) { IndexRequest index = new IndexRequest(mainRequest); index.index(doc.index()); index.type(doc.type()); index.id(doc.id()); index.source(doc.sourceRef()); index.versionType(VersionType.INTERNAL); index.version(doc.version()); return index; }
bulkRequestBuilder.add(client.prepareDelete(hit.index(), hit.type(), hit.id()));
bulkRequestBuilder.add(client.prepareDelete(hit.index(), hit.type(), hit.id()));
bulkRequestBuilder.add(client.prepareDelete(hit.index(), hit.type(), hit.id()));
for (SearchHit hit : scrollResponse.getHits().hits()) { DeleteRequest delete = new DeleteRequest().index(hit.index()) .type(hit.type()) .id(hit.id()); bulkRequest.add(delete);
for (SearchHit hit : scrollResponse.getHits().hits()) { DeleteRequest delete = new DeleteRequest().index(hit.index()) .type(hit.type()) .id(hit.id()); bulkRequest.add(delete);
@Override public void hitExecute(SearchContext context, HitContext hitContext) { if (context.explain() == false) { return; } try { final int topLevelDocId = hitContext.hit().docId(); Explanation explanation = context.searcher().explain(context.query(), topLevelDocId); for (RescoreSearchContext rescore : context.rescore()) { explanation = rescore.rescorer().explain(topLevelDocId, context, rescore, explanation); } // we use the top level doc id, since we work with the top level searcher hitContext.hit().explanation(explanation); } catch (IOException e) { throw new FetchPhaseExecutionException(context, "Failed to explain doc [" + hitContext.hit().type() + "#" + hitContext.hit().id() + "]", e); } finally { context.clearReleasables(SearchContext.Lifetime.COLLECTION); } } }
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) {
index.type(doc.type()); } else { index.type(mainRequest.getDestination().type());
index.type(doc.type()); } else { index.type(mainRequest.getDestination().type());