private void fillMinusHitsFromOneField(String fieldName, Set<Object> fieldValues, SearchHit someHit) { List<SearchHit> minusHitsList = new ArrayList<>(); int currentId = 1; for(Object result : fieldValues){ Map<String,DocumentField> fields = new HashMap<>(); ArrayList<Object> values = new ArrayList<Object>(); values.add(result); fields.put(fieldName,new DocumentField(fieldName, values)); SearchHit searchHit = new SearchHit(currentId,currentId+"", new Text(someHit.getType()), fields); searchHit.sourceRef(someHit.getSourceRef()); searchHit.getSourceAsMap().clear(); Map<String, Object> sourceAsMap = new HashMap<>(); sourceAsMap.put(fieldName,result); searchHit.getSourceAsMap().putAll(sourceAsMap); currentId++; minusHitsList.add(searchHit); } int totalSize = currentId - 1; SearchHit[] unionHitsArr = minusHitsList.toArray(new SearchHit[totalSize]); this.minusHits = new SearchHits(unionHitsArr, totalSize,1.0f); }
doc.put("_type",hit.getType());
private List<String> createHeadersAndFillDocsMap(boolean flat, SearchHit[] hits, List<Map<String, Object>> docsAsMap) { Set<String> headers = new LinkedHashSet<>(); if (this.queryAction instanceof DefaultQueryAction) { headers.addAll(((DefaultQueryAction) this.queryAction).getFieldNames()); } for (SearchHit hit : hits) { Map<String, Object> doc = hit.getSourceAsMap(); Map<String, DocumentField> fields = hit.getFields(); for (DocumentField searchHitField : fields.values()) { doc.put(searchHitField.getName(), searchHitField.getValue()); } if (this.includeScore) { doc.put("_score", hit.getScore()); } if (this.includeType) { doc.put("_type", hit.getType()); } if (this.includeId) { doc.put("_id", hit.getId()); } mergeHeaders(headers, doc, flat); docsAsMap.add(doc); } return new ArrayList<>(headers); }
private void fillMinusHitsFromResults(Set<ComperableHitResult> comperableHitResults) { int currentId = 1; List<SearchHit> minusHitsList = new ArrayList<>(); for(ComperableHitResult result : comperableHitResults){ ArrayList<Object> values = new ArrayList<Object>(); values.add(result); SearchHit originalHit = result.getOriginalHit(); SearchHit searchHit = new SearchHit(currentId,originalHit.getId(), new Text(originalHit.getType()), originalHit.getFields()); searchHit.sourceRef(originalHit.getSourceRef()); searchHit.getSourceAsMap().clear(); Map<String, Object> sourceAsMap = result.getFlattenMap(); for(Map.Entry<String,String> entry : this.builder.getFirstTableFieldToAlias().entrySet()){ if(sourceAsMap.containsKey(entry.getKey())){ Object value = sourceAsMap.get(entry.getKey()); sourceAsMap.remove(entry.getKey()); sourceAsMap.put(entry.getValue(),value); } } searchHit.getSourceAsMap().putAll(sourceAsMap); currentId++; minusHitsList.add(searchHit); } int totalSize = currentId - 1; SearchHit[] unionHitsArr = minusHitsList.toArray(new SearchHit[totalSize]); this.minusHits = new SearchHits(unionHitsArr, totalSize,1.0f); }
private void fillInternalSearchHits(List<SearchHit> unionHits, SearchHit[] hits, Map<String, String> fieldNameToAlias) { for(SearchHit hit : hits){ SearchHit searchHit = new SearchHit(currentId, hit.getId(), new Text(hit.getType()), hit.getFields()); searchHit.sourceRef(hit.getSourceRef()); searchHit.getSourceAsMap().clear(); Map<String, Object> sourceAsMap = hit.getSourceAsMap(); if(!fieldNameToAlias.isEmpty()){ updateFieldNamesToAlias(sourceAsMap, fieldNameToAlias); } searchHit.getSourceAsMap().putAll(sourceAsMap); currentId++; unionHits.add(searchHit); } }
public static String hitsAsStringResult(SearchHits results, MetaSearchResult metaResults) throws IOException { if(results == null) return null; Object[] searchHits; searchHits = new Object[(int) results.getTotalHits()]; int i = 0; for(SearchHit hit : results) { HashMap<String,Object> value = new HashMap<>(); value.put("_id",hit.getId()); value.put("_type", hit.getType()); value.put("_score", hit.getScore()); value.put("_source", hit.getSourceAsMap()); searchHits[i] = value; i++; } HashMap<String,Object> hits = new HashMap<>(); hits.put("total",results.getTotalHits()); hits.put("max_score",results.getMaxScore()); hits.put("hits",searchHits); XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); builder.startObject(); builder.field("took", metaResults.getTookImMilli()); builder.field("timed_out",metaResults.isTimedOut()); builder.field("_shards", ImmutableMap.of("total", metaResults.getTotalNumOfShards(), "successful", metaResults.getSuccessfulShards() , "failed", metaResults.getFailedShards())); builder.field("hits",hits) ; builder.endObject(); return BytesReference.bytes(builder).utf8ToString(); } }
protected SearchHit createUnmachedResult( List<Field> secondTableReturnedFields, int docId, String t1Alias, String t2Alias, SearchHit hit) { String unmatchedId = hit.getId() + "|0"; Text unamatchedType = new Text(hit.getType() + "|null"); SearchHit searchHit = new SearchHit(docId, unmatchedId, unamatchedType, hit.getFields()); searchHit.sourceRef(hit.getSourceRef()); searchHit.getSourceAsMap().clear(); searchHit.getSourceAsMap().putAll(hit.getSourceAsMap()); Map<String,Object> emptySecondTableHitSource = createNullsSource(secondTableReturnedFields); mergeSourceAndAddAliases(emptySecondTableHitSource, searchHit,t1Alias,t2Alias); return searchHit; }
for (SearchHit hit : searchResponse.getHits().hits()) { Map<String, Object> source = new HashMap<String, Object>(); String riverName = hit.getType(); RiverSettings riverSettings = new RiverSettings(null, hit.getSource()); MongoDBRiverDefinition definition = MongoDBRiverDefinition.parseSettings(riverName, riverIndexName, riverSettings, null);
private SearchHit getMergedHit(int currentCombinedResults, String t1Alias, String t2Alias, SearchHit hitFromFirstTable, SearchHit matchedHit) { onlyReturnedFields(matchedHit.getSourceAsMap(), nestedLoopsRequest.getSecondTable().getReturnedFields(),nestedLoopsRequest.getSecondTable().getOriginalSelect().isSelectAll()); SearchHit searchHit = new SearchHit(currentCombinedResults, hitFromFirstTable.getId() + "|" + matchedHit.getId(), new Text(hitFromFirstTable.getType() + "|" + matchedHit.getType()), hitFromFirstTable.getFields()); searchHit.sourceRef(hitFromFirstTable.getSourceRef()); searchHit.getSourceAsMap().clear(); searchHit.getSourceAsMap().putAll(hitFromFirstTable.getSourceAsMap()); mergeSourceAndAddAliases(matchedHit.getSourceAsMap(), searchHit, t1Alias, t2Alias); return searchHit; }
private void createKeyToResultsAndFillOptimizationStructure(Map<String,Map<String, List<Object>>> optimizationTermsFilterStructure, TableInJoinRequestBuilder firstTableRequest) { List<SearchHit> firstTableHits = fetchAllHits(firstTableRequest); int resultIds = 1; for (SearchHit hit : firstTableHits) { HashMap<String, List<Map.Entry<Field, Field>>> comparisons = this.hashJoinComparisonStructure.getComparisons(); for (Map.Entry<String, List<Map.Entry<Field, Field>>> comparison : comparisons.entrySet()) { String comparisonID = comparison.getKey(); List<Map.Entry<Field, Field>> t1ToT2FieldsComparison = comparison.getValue(); String key = getComparisonKey(t1ToT2FieldsComparison, hit, true, optimizationTermsFilterStructure.get(comparisonID)); //int docid , id SearchHit searchHit = new SearchHit(resultIds, hit.getId(), new Text(hit.getType()), hit.getFields()); searchHit.sourceRef(hit.getSourceRef()); onlyReturnedFields(searchHit.getSourceAsMap(), firstTableRequest.getReturnedFields(),firstTableRequest.getOriginalSelect().isSelectAll()); resultIds++; this.hashJoinComparisonStructure.insertIntoComparisonHash(comparisonID, key, searchHit); } } }
/** * Delete documents using a query. Check what would be deleted first with a normal search query! * Elasticsearch once provided a native prepareDeleteByQuery method, but this was removed * in later versions. Instead, there is a plugin which iterates over search results, * see https://www.elastic.co/guide/en/elasticsearch/plugins/current/plugins-delete-by-query.html * We simulate the same behaviour here without the need of that plugin. * * @param q * @return delete document count */ public int deleteByQuery(String indexName, final QueryBuilder q) { Map<String, String> ids = new TreeMap<>(); // FIXME: deprecated, "will be removed in 3.0, you should do a regular scroll instead, ordered by `_doc`" @SuppressWarnings("deprecation") SearchResponse response = elasticsearchClient.prepareSearch(indexName).setSearchType(SearchType.SCAN) .setScroll(new TimeValue(60000)).setQuery(q).setSize(100).execute().actionGet(); while (true) { // accumulate the ids here, don't delete them right now to prevent an interference of the delete with the // scroll for (SearchHit hit : response.getHits().getHits()) { ids.put(hit.getId(), hit.getType()); } response = elasticsearchClient.prepareSearchScroll(response.getScrollId()).setScroll(new TimeValue(600000)) .execute().actionGet(); // termination if (response.getHits().getHits().length == 0) break; } return deleteBulk(indexName, ids); }
private void deleteBulkRequest(String objectId, String index, String type, String routing, String parent) { if (logger.isTraceEnabled()) { logger.trace("bulkDeleteRequest - objectId: {} - index: {} - type: {} - routing: {} - parent: {}", objectId, index, type, routing, parent); } if (definition.getParentTypes() != null && definition.getParentTypes().contains(type)) { QueryBuilder builder = QueryBuilders.hasParentQuery(type, QueryBuilders.termQuery(MongoDBRiver.MONGODB_ID_FIELD, objectId)); SearchResponse response = esClient.prepareSearch(index).setQuery(builder).setRouting(routing) .addField(MongoDBRiver.MONGODB_ID_FIELD).execute().actionGet(); for (SearchHit hit : response.getHits().getHits()) { getBulkProcessor(index, hit.getType()).deleteBulkRequest(hit.getId(), routing, objectId); } } getBulkProcessor(index, type).deleteBulkRequest(objectId, routing, parent); }
@Override public String getType() { return delegate.getType(); }
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());
public Map<String, Object> query(final String indexName, final String typeName, final QueryBuilder query) { SearchRequestBuilder request = elasticsearchClient.prepareSearch(indexName); if (typeName != null) request.setTypes(typeName); request .setSearchType(SearchType.QUERY_THEN_FETCH) .setQuery(query) .setFrom(0) .setSize(1).setTerminateAfter(1); // get response SearchResponse response = request.execute().actionGet(); // evaluate search result //long totalHitCount = response.getHits().getTotalHits(); SearchHit[] hits = response.getHits().getHits(); if (hits.length == 0) return null; assert hits.length == 1; Map<String, Object> map = hits[0].getSourceAsMap(); if (!map.containsKey("id")) map.put("id", hits[0].getId()); if (!map.containsKey("type")) map.put("type", hits[0].getType()); return map; }
ids.put(hit.getId(), hit.getType());
Set<String> parentFields = new HashSet<>(); for (SearchHit hit : hits) { ParentFieldMapper parentFieldMapper = mapperService.documentMapper(hit.getType()).parentFieldMapper(); if (parentFieldMapper.active()) { parentFields.add(parentFieldMapper.name()); Map<String, SortedDocValues> docValuesMap = new HashMap<>(); for (SearchHit hit : hits) { ParentFieldMapper parentFieldMapper = mapperService.documentMapper(hit.getType()).parentFieldMapper(); if (parentFieldMapper.active() == false) { continue;
innerHits.setUid(new Uid(hit.getType(), hit.getId())); fetchPhase.execute(innerHits); FetchSearchResult fetchResult = innerHits.fetchResult();
@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 (RescoreContext rescore : context.rescore()) { explanation = rescore.rescorer().explain(topLevelDocId, context.searcher(), 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().getType() + "#" + hitContext.hit().getId() + "]", e); } finally { context.clearReleasables(SearchContext.Lifetime.COLLECTION); } } }