Java Code Examples for org.elasticsearch.search.SearchHitField#getValue()
The following examples show how to use
org.elasticsearch.search.SearchHitField#getValue() .
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: IdStrategy.java From vertexium with Apache License 2.0 | 5 votes |
public ExtendedDataRowId extendedDataRowIdFromSearchHit(SearchHit hit) { SearchHitField elementTypeField = hit.getFields().get(Elasticsearch5SearchIndex.ELEMENT_TYPE_FIELD_NAME); if (elementTypeField == null) { throw new VertexiumException("Could not find field: " + Elasticsearch5SearchIndex.ELEMENT_TYPE_FIELD_NAME); } ElementType elementType = ElasticsearchDocumentType.parse(elementTypeField.getValue().toString()).toElementType(); SearchHitField elementIdField = hit.getFields().get(Elasticsearch5SearchIndex.ELEMENT_ID_FIELD_NAME); if (elementIdField == null) { throw new VertexiumException("Could not find field: " + Elasticsearch5SearchIndex.ELEMENT_ID_FIELD_NAME); } String elementId = elementIdField.getValue(); SearchHitField tableNameField = hit.getFields().get(Elasticsearch5SearchIndex.EXTENDED_DATA_TABLE_NAME_FIELD_NAME); if (tableNameField == null) { throw new VertexiumException("Could not find field: " + Elasticsearch5SearchIndex.EXTENDED_DATA_TABLE_NAME_FIELD_NAME); } String tableName = tableNameField.getValue(); SearchHitField rowIdField = hit.getFields().get(Elasticsearch5SearchIndex.EXTENDED_DATA_TABLE_ROW_ID_FIELD_NAME); if (rowIdField == null) { throw new VertexiumException("Could not find field: " + Elasticsearch5SearchIndex.EXTENDED_DATA_TABLE_ROW_ID_FIELD_NAME); } String rowId = rowIdField.getValue(); return new ExtendedDataRowId(elementType, elementId, tableName, rowId); }
Example 2
Source File: IdStrategy.java From vertexium with Apache License 2.0 | 5 votes |
private String elementIdFromSearchHit(SearchHit hit) { SearchHitField elementIdField = hit.getFields().get(Elasticsearch5SearchIndex.ELEMENT_ID_FIELD_NAME); if (elementIdField == null) { throw new VertexiumException("Could not find field: " + Elasticsearch5SearchIndex.ELEMENT_ID_FIELD_NAME); } return elementIdField.getValue(); }
Example 3
Source File: ElasticSearchResult.java From sakai with Educational Community License v2.0 | 5 votes |
@Override public String[] getValues(String string) { String[] values = new String[hit.getFields().size()]; int i=0; for (SearchHitField field: hit.getFields().values()) { values[i++] = field.getValue(); } return values; }
Example 4
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 5
Source File: ElasticsearchDataModel.java From elasticsearch-taste with Apache License 2.0 | 5 votes |
protected long getLongValue(final SearchHit hit, final String field) { final SearchHitField result = hit.field(field); if (result == null) { throw new TasteException(field + " is not found."); } final Number longValue = result.getValue(); if (longValue == null) { throw new TasteException("The result of " + field + " is null."); } return longValue.longValue(); }
Example 6
Source File: ElasticsearchDataModel.java From elasticsearch-taste with Apache License 2.0 | 5 votes |
protected float getFloatValue(final SearchHit hit, final String field) { final SearchHitField result = hit.field(field); if (result == null) { throw new TasteException(field + " is not found."); } final Number floatValue = result.getValue(); if (floatValue == null) { throw new TasteException("The result of " + field + " is null."); } return floatValue.floatValue(); }
Example 7
Source File: ElasticSearchResult.java From sakai with Educational Community License v2.0 | 5 votes |
@Override public String[] getValues(String string) { String[] values = new String[hit.getFields().size()]; int i=0; for (SearchHitField field: hit.getFields().values()) { values[i++] = field.getValue(); } return values; }
Example 8
Source File: ElasticsearchDataModel.java From elasticsearch-taste with Apache License 2.0 | 4 votes |
@Override public Float getPreferenceValue(final long userID, final long itemID) { if (cache != null) { final DmValue dmValue = cache.getIfPresent(DmKey.key( DmKey.PREFERENCE_VALUE, userID, itemID)); if (dmValue != null) { return dmValue.getValue(); } } SearchResponse response; try { response = client.prepareSearch(preferenceIndex) .setTypes(preferenceType) .setQuery(QueryBuilders.boolQuery() .must(QueryBuilders.termQuery(itemIdField, itemID)) .must(QueryBuilders.termQuery(userIdField, userID)) .filter(getLastAccessedFilterQuery())) .addFields(valueField) .addSort(timestampField, SortOrder.DESC).setSize(1) .execute().actionGet(); } catch (final ElasticsearchException e) { throw new TasteException("Failed to get the preference by (" + userID + "," + itemID + ")", e); } final SearchHits hits = response.getHits(); final long totalHits = hits.getTotalHits(); if (totalHits == 0) { return null; } else if (totalHits > 1) { logger.warn( "ItemID {} of UserID {} has {} preferences. Use the latest value.", itemID, userID, totalHits); } final SearchHit[] searchHits = hits.getHits(); if (searchHits.length > 0) { final SearchHitField result = searchHits[0].field(valueField); if (result != null) { final Number value = result.getValue(); final float floatValue = value.floatValue(); if (cache != null) { cache.put(DmKey.create(DmKey.PREFERENCE_VALUE, userID, itemID), new DmValue(floatValue, 16)); } return floatValue; } } return null; }
Example 9
Source File: ElasticsearchDataModel.java From elasticsearch-taste with Apache License 2.0 | 4 votes |
@Override public Long getPreferenceTime(final long userID, final long itemID) { if (cache != null) { final DmValue dmValue = cache.getIfPresent(DmKey.key( DmKey.PREFERENCE_TIME, userID, itemID)); if (dmValue != null) { return dmValue.getValue(); } } SearchResponse response; try { 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(); } catch (final ElasticsearchException e) { throw new TasteException("Failed to get the timestamp by (" + userID + "," + itemID + ")", e); } final SearchHits hits = response.getHits(); final long totalHits = hits.getTotalHits(); if (totalHits == 0) { return null; } else if (totalHits > 1) { logger.warn( "ItemID {} of UserID {} has {} preferences. Use the latest value.", itemID, userID, totalHits); } 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) { cache.put( DmKey.create(DmKey.PREFERENCE_TIME, userID, itemID), new DmValue(time, 16)); } return time; } } return null; }
Example 10
Source File: ActionHandler.java From elasticsearch-taste with Apache License 2.0 | 4 votes |
protected long[] getTargetIDs(final String index, final String type, final String fieldName, final String targetName) { final Map<String, Object> userSettings = SettingsUtils.get( rootSettings, targetName); final String userQuery = SettingsUtils.get(userSettings, "query"); if (StringUtils.isBlank(userQuery)) { return null; } final Number size = SettingsUtils.get(userSettings, "size", 1000); final Number keepAlive = SettingsUtils.get(userSettings, "keep_alive", 60000); //1min int count = 0; long[] targetIDs = null; SearchResponse response = null; 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; } for (final SearchHit hit : hits) { final SearchHitField searchHitField = hit.getFields().get( fieldName); final Number value = searchHitField.getValue(); targetIDs[count] = value.longValue(); count++; } } return targetIDs; }
Example 11
Source File: ItemRequestHandler.java From elasticsearch-taste with Apache License 2.0 | 4 votes |
private void doItemCreation(final Params params, final RequestHandler.OnErrorListener listener, final Map<String, Object> requestMap, final Map<String, Object> paramMap, final Map<String, Object> itemMap, final String index, final String type, final String itemIdField, final String timestampField, final RequestHandlerChain chain) { final OnResponseListener<SearchResponse> responseListener = response -> { validateRespose(response); Number currentId = null; final SearchHits hits = response.getHits(); if (hits.getTotalHits() != 0) { final SearchHit[] searchHits = hits.getHits(); final SearchHitField field = searchHits[0].getFields().get( itemIdField); if (field != null) { currentId = field.getValue(); } } final Long itemId; if (currentId == null) { itemId = Long.valueOf(1); } else { itemId = Long.valueOf(currentId.longValue() + 1); } doItemUpdate(params, listener, requestMap, paramMap, itemMap, index, type, itemIdField, timestampField, itemId, OpType.CREATE, chain); }; final OnFailureListener failureListener = t -> { final List<Throwable> errorList = getErrorList(paramMap); if (errorList.size() >= maxRetryCount) { listener.onError(t); } else { sleep(t); errorList.add(t); doItemIndexExists(params, listener, requestMap, paramMap, chain); } }; client.prepareSearch(index).setTypes(type) .setQuery(QueryBuilders.matchAllQuery()).addField(itemIdField) .addSort(itemIdField, SortOrder.DESC).setSize(1) .execute(on(responseListener, failureListener)); }
Example 12
Source File: UserRequestHandler.java From elasticsearch-taste with Apache License 2.0 | 4 votes |
private void doUserCreation(final Params params, final RequestHandler.OnErrorListener listener, final Map<String, Object> requestMap, final Map<String, Object> paramMap, final Map<String, Object> userMap, final String index, final String type, final String userIdField, final String timestampField, final RequestHandlerChain chain) { final OnResponseListener<SearchResponse> responseListener = response -> { validateRespose(response); Number currentId = 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(); } } final Long userId; if (currentId == null) { userId = Long.valueOf(1); } else { userId = Long.valueOf(currentId.longValue() + 1); } doUserUpdate(params, listener, requestMap, paramMap, userMap, index, type, userIdField, timestampField, userId, OpType.CREATE, chain); }; final OnFailureListener failureListener = t -> { final List<Throwable> errorList = getErrorList(paramMap); if (errorList.size() >= maxRetryCount) { listener.onError(t); } else { sleep(t); errorList.add(t); doUserIndexExists(params, listener, requestMap, paramMap, chain); } }; client.prepareSearch(index).setTypes(type) .setQuery(QueryBuilders.matchAllQuery()).addField(userIdField) .addSort(userIdField, SortOrder.DESC).setSize(1) .execute(on(responseListener, failureListener)); }
Example 13
Source File: TasteSearchRestAction.java From elasticsearch-taste with Apache License 2.0 | 4 votes |
@Override protected void handleRequest(final RestRequest request, final RestChannel channel, final Client client) { final Info info = new Info(request); final String systemId = request.param("systemId"); if (StringUtils.isBlank(systemId)) { onError(channel, new NotFoundException("No system_id.")); return; } final String[] systemIds = systemId.trim().split(","); if (systemIds.length == 0) { onError(channel, new NotFoundException("No system_id.")); return; } if (info.getIdIndex() == null) { onError(channel, new NotFoundException("No search type.")); return; } final OnResponseListener<SearchResponse> responseListener = searchResponse -> { final SearchHits hits = searchResponse.getHits(); if (hits.totalHits() == 0) { onError(channel, new NotFoundException("No " + info.getIdField() + " data for " + systemId + " in " + info.getIdIndex() + "/" + info.getIdType())); return; } 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(); } } if (targetIds.length == 0) { onError(channel, new NotFoundException("No " + info.getIdField() + " for " + systemId + " in " + info.getIdIndex() + "/" + info.getIdType())); return; } doSearchRequest(request, channel, client, info, targetIds); }; final BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); for (final String id : systemIds) { boolQueryBuilder.should(QueryBuilders.termQuery("system_id", id)); } boolQueryBuilder.minimumNumberShouldMatch(1); client.prepareSearch(info.getIdIndex()).setTypes(info.getIdType()) .setQuery(boolQueryBuilder).addField(info.getIdField()) .addSort(info.getTimestampField(), SortOrder.DESC) .setSize(systemIds.length) .execute(on(responseListener, t -> onError(channel, t))); }