Java Code Examples for org.elasticsearch.search.SearchHit#getFields()
The following examples show how to use
org.elasticsearch.search.SearchHit#getFields() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticSearchService.java From samantha with MIT License | 6 votes |
public Map<Map<String, String>, List<SearchHit>> searchFieldsByKeys(String index, String type, List<String> keys, List<String> fields, JsonNode data) { Map<Map<String, String>, List<SearchHit>> keyVals = new HashMap<>(); SearchHits hits = searchHitsByKeys(index, type, keys, fields, data); for (SearchHit hit : hits) { Map<String, SearchHitField> hitFields = hit.getFields(); Map<String, String> keyVal = new HashMap<>(keys.size()); for (String key : keys) { if (hitFields.containsKey(key)) { //for some reason, this (String) is necessary for some environments/compilers keyVal.put(key, (String) hitFields.get(key).getValue()); } } if (keyVals.containsKey(keyVal)) { keyVals.get(keyVal).add(hit); } else { List<SearchHit> vals = new ArrayList<>(); vals.add(hit); keyVals.put(keyVal, vals); } } return keyVals; }
Example 2
Source File: HashJoinElasticExecutor.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
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(), null); searchHit.sourceRef(hit.getSourceRef()); onlyReturnedFields(searchHit.getSourceAsMap(), firstTableRequest.getReturnedFields(),firstTableRequest.getOriginalSelect().isSelectAll()); resultIds++; this.hashJoinComparisonStructure.insertIntoComparisonHash(comparisonID, key, searchHit); } } }
Example 3
Source File: IntersectExecutor.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
private void fillIntersectHitsFromResults(Set<ComperableHitResult> comparableHitResults) { int currentId = 1; List<SearchHit> intersectHitsList = new ArrayList<>(comparableHitResults.size()); Set<Map.Entry<String, String>> firstTableFieldToAlias = this.builder.getFirstTableFieldToAlias().entrySet(); for (ComperableHitResult result : comparableHitResults) { SearchHit originalHit = result.getOriginalHit(); SearchHit searchHit = new SearchHit(currentId, originalHit.getId(), new Text(originalHit.getType()), originalHit.getFields(), null); searchHit.sourceRef(originalHit.getSourceRef()); searchHit.getSourceAsMap().clear(); Map<String, Object> sourceAsMap = result.getFlattenMap(); for (Map.Entry<String, String> entry : firstTableFieldToAlias) { 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++; intersectHitsList.add(searchHit); } int totalSize = currentId - 1; SearchHit[] unionHitsArr = intersectHitsList.toArray(new SearchHit[totalSize]); this.intersectHits = new SearchHits(unionHitsArr, new TotalHits(totalSize, TotalHits.Relation.EQUAL_TO), 1.0f); }
Example 4
Source File: ExpanderUtilities.java From samantha with MIT License | 5 votes |
static public void parseEntityFromSearchHit(List<String> entityFields, String elasticSearchScoreName, SearchHit hit, ObjectNode entity) { if (elasticSearchScoreName != null) { entity.put(elasticSearchScoreName, hit.getScore()); } Map<String, SearchHitField> elasticSearchFields = hit.getFields(); for (String fieldName : entityFields) { if (elasticSearchFields.containsKey(fieldName)) { entity.set(fieldName, Json.toJson(elasticSearchFields .get(fieldName).value())); } } }
Example 5
Source File: BaseElasticSearchIndexBuilder.java From sakai with Educational Community License v2.0 | 5 votes |
/** * loads the field from the SearchHit. Loads from field not from source since * we aren't storing the source. * @param field * @param hit * @return */ @Override public String getFieldFromSearchHit(String field, SearchHit hit) { if (hit != null && hit.getFields() != null && hit.getFields().get(field) != null) { return hit.getFields().get(field).value(); } return null; }
Example 6
Source File: UnionExecutor.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
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(), null); 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); } }
Example 7
Source File: ElasticJoinExecutor.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
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(), null); 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; }
Example 8
Source File: MinusExecutor.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
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(), null); 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, new TotalHits(totalSize, TotalHits.Relation.EQUAL_TO), 1.0f); }
Example 9
Source File: NestedLoopsElasticExecutor.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
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(), null); searchHit.sourceRef(hitFromFirstTable.getSourceRef()); searchHit.getSourceAsMap().clear(); searchHit.getSourceAsMap().putAll(hitFromFirstTable.getSourceAsMap()); mergeSourceAndAddAliases(matchedHit.getSourceAsMap(), searchHit, t1Alias, t2Alias); return searchHit; }
Example 10
Source File: ObjectResultsExtractor.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
private List<String> createHeadersAndFillDocsMap(boolean flat, SearchHit[] hits, String scrollId, List<Map<String, Object>> docsAsMap) { Set<String> headers = new LinkedHashSet<>(); List<String> fieldNames = new ArrayList<>(); if (this.queryAction instanceof DefaultQueryAction) { fieldNames.addAll(((DefaultQueryAction) this.queryAction).getFieldNames()); } boolean hasScrollId = this.includeScrollId || fieldNames.contains("_scroll_id"); 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()); } if (hasScrollId) { doc.put("_scroll_id", scrollId); } mergeHeaders(headers, doc, flat); docsAsMap.add(doc); } List<String> list = new ArrayList<>(headers); if (!fieldNames.isEmpty()) { list.sort((o1, o2) -> { int i1 = fieldNames.indexOf(o1); int i2 = fieldNames.indexOf(o2); return Integer.compare(i1 < 0 ? Integer.MAX_VALUE : i1, i2 < 0 ? Integer.MAX_VALUE : i2); }); } return list; }
Example 11
Source File: SearchResult.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
public SearchResult(SearchResponse resp) { SearchHits hits = resp.getHits(); this.total = hits.getTotalHits().value; results = new ArrayList<>(hits.getHits().length); for (SearchHit searchHit : hits.getHits()) { if (searchHit.getSourceAsMap() != null) { results.add(searchHit.getSourceAsMap()); } else if (searchHit.getFields() != null) { Map<String, DocumentField> fields = searchHit.getFields(); results.add(toFieldsMap(fields)); } } }
Example 12
Source File: ReindexingService.java From elasticsearch-reindexing with Apache License 2.0 | 5 votes |
private void sendToLocalCluster(final String scrollId, final SearchHit[] hits) { // prepare bulk request final BulkRequestBuilder bulkRequest = client.prepareBulk(); for (final SearchHit hit : hits) { IndexRequestBuilder builder = client.prepareIndex(toIndex, toType != null ? toType : hit.getType(), hit.getId()) .setSource(hit.getSource()); Map<String, SearchHitField> fields = hit.getFields(); if (fields != null && fields.containsKey("_parent")) { SearchHitField parentField = fields.get("_parent"); if (parentField != null) { String parentId = parentField.getValue(); builder.setParent(parentId); } } bulkRequest.add(builder); } // send bulk request, if success response got, searching the next 10 results using scroll_id // using this listener (inner class) to listen to results bulkRequest.execute(new ActionListener<BulkResponse>() { @Override public void onResponse(final BulkResponse bulkResponse) { if (bulkResponse.hasFailures()) { throw new ReindexingException(bulkResponse .buildFailureMessage()); } client.prepareSearchScroll(scrollId).setScroll(scroll) .execute(ReindexingListener.this); } @Override public void onFailure(final Throwable e) { ReindexingListener.this.onFailure(e); } }); }
Example 13
Source File: ImageIntegrationTests.java From elasticsearch-image with Apache License 2.0 | 5 votes |
private void assertImageScore(SearchHits hits, String name, float score) { for (SearchHit hit : hits) { if ((hit.getSource() != null && hit.getSource().get("name").equals(name)) || (hit.getFields() != null && !hit.getFields().isEmpty() && hit.getFields().get("name").getValue().equals(name))){ assertThat(hit.getScore(), equalTo(score)); return; } } throw new AssertionError("Image " + name + " not found"); }
Example 14
Source File: BaseElasticSearchIndexBuilder.java From sakai with Educational Community License v2.0 | 5 votes |
/** * loads the field from the SearchHit. Loads from field not from source since * we aren't storing the source. * @param field * @param hit * @return */ @Override public String getFieldFromSearchHit(String field, SearchHit hit) { if (hit != null && hit.getFields() != null && hit.getFields().get(field) != null) { return hit.getFields().get(field).value(); } return null; }
Example 15
Source File: HashJoinElasticExecutor.java From elasticsearch-sql with Apache License 2.0 | 4 votes |
private List<SearchHit> createCombinedResults( TableInJoinRequestBuilder secondTableRequest) { List<SearchHit> combinedResult = new ArrayList<>(); int resultIds = 0; int totalLimit = this.requestBuilder.getTotalLimit(); Integer hintLimit = secondTableRequest.getHintLimit(); SearchResponse searchResponse; boolean finishedScrolling; if (hintLimit != null && hintLimit < MAX_RESULTS_ON_ONE_FETCH) { searchResponse = secondTableRequest.getRequestBuilder().setSize(hintLimit).get(); finishedScrolling = true; } else { searchResponse = secondTableRequest.getRequestBuilder() .setScroll(new TimeValue(60000)) .setSize(MAX_RESULTS_ON_ONE_FETCH).get(); //es5.0 no need to scroll again! // searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()).setScroll(new TimeValue(600000)).get(); finishedScrolling = false; } updateMetaSearchResults(searchResponse); boolean limitReached = false; int fetchedSoFarFromSecondTable = 0; while (!limitReached) { SearchHit[] secondTableHits = searchResponse.getHits().getHits(); fetchedSoFarFromSecondTable += secondTableHits.length; for (SearchHit secondTableHit : secondTableHits) { if (limitReached) break; //todo: need to run on comparisons. for each comparison check if exists and add. 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, secondTableHit, false, null); SearchHitsResult searchHitsResult = this.hashJoinComparisonStructure.searchForMatchingSearchHits(comparisonID, key); if (searchHitsResult != null && searchHitsResult.getSearchHits().size() > 0) { searchHitsResult.setMatchedWithOtherTable(true); List<SearchHit> searchHits = searchHitsResult.getSearchHits(); for (SearchHit matchingHit : searchHits) { String combinedId = matchingHit.getId() + "|" + secondTableHit.getId(); //in order to prevent same matching when using OR on hashJoins. if(this.alreadyMatched.contains(combinedId)){ continue; } else { this.alreadyMatched.add(combinedId); } Map<String,Object> copiedSource = new HashMap<String,Object>(); copyMaps(copiedSource,secondTableHit.getSourceAsMap()); onlyReturnedFields(copiedSource, secondTableRequest.getReturnedFields(),secondTableRequest.getOriginalSelect().isSelectAll()); SearchHit searchHit = new SearchHit(matchingHit.docId(), combinedId, new Text(matchingHit.getType() + "|" + secondTableHit.getType()), matchingHit.getFields(), null); searchHit.sourceRef(matchingHit.getSourceRef()); searchHit.getSourceAsMap().clear(); searchHit.getSourceAsMap().putAll(matchingHit.getSourceAsMap()); String t1Alias = requestBuilder.getFirstTable().getAlias(); String t2Alias = requestBuilder.getSecondTable().getAlias(); mergeSourceAndAddAliases(copiedSource, searchHit, t1Alias, t2Alias); combinedResult.add(searchHit); resultIds++; if (resultIds >= totalLimit) { limitReached = true; break; } } } } } if (!finishedScrolling) { if (secondTableHits.length > 0 && (hintLimit == null || fetchedSoFarFromSecondTable >= hintLimit)) { searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet(); } else break; } else { break; } } return combinedResult; }