use of org.elasticsearch.index.get.GetField in project elasticsearch by elastic.
the class GetResponseTests method testToString.
public void testToString() {
GetResponse getResponse = new GetResponse(new GetResult("index", "type", "id", 1, true, new BytesArray("{ \"field1\" : " + "\"value1\", \"field2\":\"value2\"}"), Collections.singletonMap("field1", new GetField("field1", Collections.singletonList("value1")))));
assertEquals("{\"_index\":\"index\",\"_type\":\"type\",\"_id\":\"id\",\"_version\":1,\"found\":true,\"_source\":{ \"field1\" " + ": \"value1\", \"field2\":\"value2\"},\"fields\":{\"field1\":[\"value1\"]}}", getResponse.toString());
}
use of org.elasticsearch.index.get.GetField in project elasticsearch by elastic.
the class TermVectorsService method generateTermVectorsFromDoc.
private static Fields generateTermVectorsFromDoc(IndexShard indexShard, TermVectorsRequest request) throws IOException {
// parse the document, at the moment we do update the mapping, just like percolate
ParsedDocument parsedDocument = parseDocument(indexShard, indexShard.shardId().getIndexName(), request.type(), request.doc(), request.xContentType());
// select the right fields and generate term vectors
ParseContext.Document doc = parsedDocument.rootDoc();
Set<String> seenFields = new HashSet<>();
Collection<GetField> getFields = new HashSet<>();
for (IndexableField field : doc.getFields()) {
MappedFieldType fieldType = indexShard.mapperService().fullName(field.name());
if (!isValidField(fieldType)) {
continue;
}
if (request.selectedFields() != null && !request.selectedFields().contains(field.name())) {
continue;
}
if (seenFields.contains(field.name())) {
continue;
} else {
seenFields.add(field.name());
}
String[] values = doc.getValues(field.name());
getFields.add(new GetField(field.name(), Arrays.asList((Object[]) values)));
}
return generateTermVectors(indexShard, XContentHelper.convertToMap(parsedDocument.source(), true, request.xContentType()).v2(), getFields, request.offsets(), request.perFieldAnalyzer(), seenFields);
}
use of org.elasticsearch.index.get.GetField in project elasticsearch by elastic.
the class TermVectorsService method generateTermVectors.
private static Fields generateTermVectors(IndexShard indexShard, Map<String, Object> source, Collection<GetField> getFields, boolean withOffsets, @Nullable Map<String, String> perFieldAnalyzer, Set<String> fields) throws IOException {
Map<String, Collection<Object>> values = new HashMap<>();
for (GetField getField : getFields) {
String field = getField.getName();
if (fields.contains(field)) {
// some fields are returned even when not asked for, eg. _timestamp
values.put(field, getField.getValues());
}
}
if (source != null) {
for (String field : fields) {
if (values.containsKey(field) == false) {
List<Object> v = XContentMapValues.extractRawValues(field, source);
if (v.isEmpty() == false) {
values.put(field, v);
}
}
}
}
/* store document in memory index */
MemoryIndex index = new MemoryIndex(withOffsets);
for (Map.Entry<String, Collection<Object>> entry : values.entrySet()) {
String field = entry.getKey();
Analyzer analyzer = getAnalyzerAtField(indexShard, field, perFieldAnalyzer);
if (entry.getValue() instanceof List) {
for (Object text : entry.getValue()) {
index.addField(field, text.toString(), analyzer);
}
} else {
index.addField(field, entry.getValue().toString(), analyzer);
}
}
/* and read vectors from it */
return MultiFields.getFields(index.createSearcher().getIndexReader());
}
use of org.elasticsearch.index.get.GetField in project elasticsearch by elastic.
the class UpdateHelper method extractGetResult.
/**
* Applies {@link UpdateRequest#fetchSource()} to the _source of the updated document to be returned in a update response.
* For BWC this function also extracts the {@link UpdateRequest#fields()} from the updated document to be returned in a update response
*/
public GetResult extractGetResult(final UpdateRequest request, String concreteIndex, long version, final Map<String, Object> source, XContentType sourceContentType, @Nullable final BytesReference sourceAsBytes) {
if ((request.fields() == null || request.fields().length == 0) && (request.fetchSource() == null || request.fetchSource().fetchSource() == false)) {
return null;
}
SourceLookup sourceLookup = new SourceLookup();
sourceLookup.setSource(source);
boolean sourceRequested = false;
Map<String, GetField> fields = null;
if (request.fields() != null && request.fields().length > 0) {
for (String field : request.fields()) {
if (field.equals("_source")) {
sourceRequested = true;
continue;
}
Object value = sourceLookup.extractValue(field);
if (value != null) {
if (fields == null) {
fields = new HashMap<>(2);
}
GetField getField = fields.get(field);
if (getField == null) {
getField = new GetField(field, new ArrayList<>(2));
fields.put(field, getField);
}
getField.getValues().add(value);
}
}
}
BytesReference sourceFilteredAsBytes = sourceAsBytes;
if (request.fetchSource() != null && request.fetchSource().fetchSource()) {
sourceRequested = true;
if (request.fetchSource().includes().length > 0 || request.fetchSource().excludes().length > 0) {
Object value = sourceLookup.filter(request.fetchSource());
try {
final int initialCapacity = Math.min(1024, sourceAsBytes.length());
BytesStreamOutput streamOutput = new BytesStreamOutput(initialCapacity);
try (XContentBuilder builder = new XContentBuilder(sourceContentType.xContent(), streamOutput)) {
builder.value(value);
sourceFilteredAsBytes = builder.bytes();
}
} catch (IOException e) {
throw new ElasticsearchException("Error filtering source", e);
}
}
}
// TODO when using delete/none, we can still return the source as bytes by generating it (using the sourceContentType)
return new GetResult(concreteIndex, request.type(), request.id(), version, true, sourceRequested ? sourceFilteredAsBytes : null, fields);
}
use of org.elasticsearch.index.get.GetField in project elasticsearch-jetty by sonian.
the class ESLoginService method loadUser.
@Override
public UserIdentity loadUser(String user) {
Log.debug("attempting to load user [{}]", user);
try {
GetResponse response = client.prepareGet(authIndex, authType, user).setFields(passwordField, rolesField).execute().actionGet();
if (response.isExists()) {
Log.debug("user [{}] exists; looking for credentials...", user);
Credential credential = null;
GetField passwordGetField = response.getField(passwordField);
if (passwordGetField != null) {
Log.debug("user [{}] using password auth", user);
credential = Credential.getCredential((String) passwordGetField.getValue());
}
String[] roles = getStringValues(response.getField(rolesField));
return putUser(user, credential, roles);
}
} catch (IndexMissingException e) {
Log.warn("no auth index [{}]", authIndex);
} catch (Exception e) {
Log.warn("error finding user [" + user + "] in [" + authIndex + "]", e);
}
return null;
}
Aggregations