Search in sources :

Example 1 with GetField

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());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) GetField(org.elasticsearch.index.get.GetField) GetResultTests.mutateGetResult(org.elasticsearch.index.get.GetResultTests.mutateGetResult) GetResult(org.elasticsearch.index.get.GetResult) GetResultTests.randomGetResult(org.elasticsearch.index.get.GetResultTests.randomGetResult) GetResultTests.copyGetResult(org.elasticsearch.index.get.GetResultTests.copyGetResult)

Example 2 with GetField

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);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) GetField(org.elasticsearch.index.get.GetField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) ParseContext(org.elasticsearch.index.mapper.ParseContext) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) HashSet(java.util.HashSet)

Example 3 with GetField

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());
}
Also used : GetField(org.elasticsearch.index.get.GetField) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) HashMap(java.util.HashMap) Collection(java.util.Collection) List(java.util.List) Analyzer(org.apache.lucene.analysis.Analyzer) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 4 with GetField

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);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) GetField(org.elasticsearch.index.get.GetField) SourceLookup(org.elasticsearch.search.lookup.SourceLookup) GetResult(org.elasticsearch.index.get.GetResult) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 5 with GetField

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;
}
Also used : Credential(org.eclipse.jetty.util.security.Credential) GetField(org.elasticsearch.index.get.GetField) IndexMissingException(org.elasticsearch.indices.IndexMissingException) GetResponse(org.elasticsearch.action.get.GetResponse) IndexMissingException(org.elasticsearch.indices.IndexMissingException)

Aggregations

GetField (org.elasticsearch.index.get.GetField)5 GetResult (org.elasticsearch.index.get.GetResult)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 IndexableField (org.apache.lucene.index.IndexableField)1 MemoryIndex (org.apache.lucene.index.memory.MemoryIndex)1 Credential (org.eclipse.jetty.util.security.Credential)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 GetResponse (org.elasticsearch.action.get.GetResponse)1 BytesArray (org.elasticsearch.common.bytes.BytesArray)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1