Search in sources :

Example 6 with DocumentField

use of org.elasticsearch.common.document.DocumentField in project opencast by opencast.

the class AbstractElasticsearchIndex method executeQuery.

/**
 * Execute a query on the index.
 *
 * @param query
 *          The query to use to find the results
 * @param request
 *          The builder to use to create the query.
 * @param toSearchResult
 *          The function to convert the results to a {@link SearchResult}
 * @param maxRetryAttempts
 *          How often to retry query in case of ElasticsearchStatusException
 * @param retryWaitingPeriod
 *          How long to wait (in ms) between retries
 * @return A {@link SearchResult} containing the relevant objects.
 *
 * @throws IOException
 *         If querying the index fails
 * @throws InterruptedException
 *         If waiting during retry is interrupted
 */
protected <T> SearchResult<T> executeQuery(SearchQuery query, SearchRequest request, Function<SearchMetadataCollection, T> toSearchResult, int maxRetryAttempts, int retryWaitingPeriod) throws IOException, InterruptedException {
    // Execute the query and try to get hold of a query response
    SearchResponse searchResponse = null;
    int retryAttempts = 0;
    do {
        try {
            searchResponse = getClient().search(request, RequestOptions.DEFAULT);
        } catch (ElasticsearchStatusException e) {
            retryAttempts++;
            if (retryAttempts <= maxRetryAttempts) {
                logger.warn("Could not query documents from index {} because of {}, retrying in {} ms.", getIndexName(), e.getMessage(), retryWaitingPeriod);
                if (retryWaitingPeriod > 0) {
                    Thread.sleep(retryWaitingPeriod);
                }
            } else {
                logger.error("Could not query documents from index {} because of {}, not retrying.", getIndexName(), e.getMessage());
                throw e;
            }
        }
    } while (searchResponse == null);
    // Create and configure the query result
    long hits = getTotalHits(searchResponse.getHits());
    long size = searchResponse.getHits().getHits().length;
    SearchResultImpl<T> result = new SearchResultImpl<>(query, hits, size);
    result.setSearchTime(searchResponse.getTook().millis());
    // Walk through response and create new items with title, creator, etc:
    for (SearchHit doc : searchResponse.getHits()) {
        // Wrap the search resulting metadata
        SearchMetadataCollection metadata = new SearchMetadataCollection(doc.getType());
        metadata.setIdentifier(doc.getId());
        for (DocumentField field : doc.getFields().values()) {
            String name = field.getName();
            SearchMetadata<Object> m = new SearchMetadataImpl<>(name);
            // Add the field values
            if (field.getValues().size() > 1) {
                for (Object v : field.getValues()) {
                    m.addValue(v);
                }
            } else {
                m.addValue(field.getValue());
            }
            // Add the metadata
            metadata.add(m);
        }
        // Get the score for this item
        float score = doc.getScore();
        // item
        try {
            T document = toSearchResult.apply(metadata);
            SearchResultItem<T> item = new SearchResultItemImpl<>(score, document);
            result.addResultItem(item);
        } catch (Throwable t) {
            logger.warn("Error during search result serialization: '{}'. Skipping this search result.", t.getMessage());
            size--;
        }
    }
    // Set the number of resulting documents
    result.setDocumentCount(size);
    return result;
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) DocumentField(org.elasticsearch.common.document.DocumentField) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 7 with DocumentField

use of org.elasticsearch.common.document.DocumentField in project urban-eureka by errir503.

the class TimestampDecoder method decode.

@Override
public void decode(SearchHit hit, Supplier<Object> getter, BlockBuilder output) {
    DocumentField documentField = hit.getFields().get(path);
    Object value = null;
    if (documentField != null) {
        if (documentField.getValues().size() > 1) {
            throw new PrestoException(ELASTICSEARCH_TYPE_MISMATCH, format("Expected single value for column '%s', found: %s", path, documentField.getValues().size()));
        }
        value = documentField.getValue();
    } else {
        value = getter.get();
    }
    if (value == null) {
        output.appendNull();
    } else {
        LocalDateTime timestamp;
        if (value instanceof String) {
            timestamp = ISO_DATE_TIME.parse((String) value, LocalDateTime::from);
        } else if (value instanceof Number) {
            timestamp = LocalDateTime.ofInstant(Instant.ofEpochMilli(((Number) value).longValue()), ZULU);
        } else {
            throw new PrestoException(NOT_SUPPORTED, format("Unsupported representation for field '%s' of type TIMESTAMP: %s [%s]", path, value.getClass().getSimpleName(), value));
        }
        long epochMillis = timestamp.atZone(zoneId).toInstant().toEpochMilli();
        TIMESTAMP.writeLong(output, epochMillis);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DocumentField(org.elasticsearch.common.document.DocumentField) PrestoException(com.facebook.presto.spi.PrestoException)

Example 8 with DocumentField

use of org.elasticsearch.common.document.DocumentField in project opencast by opencast.

the class AbstractElasticsearchIndex method executeQuery.

/**
 * Execute a query on the index.
 *
 * @param query
 *          The query to use to find the results
 * @param request
 *          The builder to use to create the query.
 * @param toSearchResult
 *          The function to convert the results to a {@link SearchResult}
 * @return A {@link SearchResult} containing the relevant objects.
 * @throws SearchIndexException
 */
protected <T> SearchResult<T> executeQuery(SearchQuery query, SearchRequest request, Function<SearchMetadataCollection, T> toSearchResult) throws SearchIndexException {
    // Execute the query and try to get hold of a query response
    SearchResponse response = null;
    try {
        response = getClient().search(request, RequestOptions.DEFAULT);
    } catch (Throwable t) {
        throw new SearchIndexException(t);
    }
    // Create and configure the query result
    long hits = getTotalHits(response.getHits());
    long size = response.getHits().getHits().length;
    SearchResultImpl<T> result = new SearchResultImpl<>(query, hits, size);
    result.setSearchTime(response.getTook().millis());
    // Walk through response and create new items with title, creator, etc:
    for (SearchHit doc : response.getHits()) {
        // Wrap the search resulting metadata
        SearchMetadataCollection metadata = new SearchMetadataCollection(doc.getType());
        metadata.setIdentifier(doc.getId());
        for (DocumentField field : doc.getFields().values()) {
            String name = field.getName();
            SearchMetadata<Object> m = new SearchMetadataImpl<>(name);
            // Add the field values
            if (field.getValues().size() > 1) {
                for (Object v : field.getValues()) {
                    m.addValue(v);
                }
            } else {
                m.addValue(field.getValue());
            }
            // Add the metadata
            metadata.add(m);
        }
        // Get the score for this item
        float score = doc.getScore();
        // item
        try {
            T document = toSearchResult.apply(metadata);
            SearchResultItem<T> item = new SearchResultItemImpl<>(score, document);
            result.addResultItem(item);
        } catch (Throwable t) {
            logger.warn("Error during search result serialization: '{}'. Skipping this search result.", t.getMessage());
            size--;
        }
    }
    // Set the number of resulting documents
    result.setDocumentCount(size);
    return result;
}
Also used : SearchIndexException(org.opencastproject.elasticsearch.api.SearchIndexException) SearchHit(org.elasticsearch.search.SearchHit) DocumentField(org.elasticsearch.common.document.DocumentField) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 9 with DocumentField

use of org.elasticsearch.common.document.DocumentField in project pancm_project by xuwujing.

the class EsScriptSearchTest method scriptSearch.

/**
 * @Author pancm
 * @Description  通过脚本进行查询
 * @Date  2019/9/6
 * @Param []
 * @return void
 */
private static void scriptSearch() throws IOException {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("p_test");
    searchRequest.types("_doc");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    /**
     *{
     *     "settings" : {
     *         "number_of_shards" : 10,
     *          "refresh_interval" : "1s"
     *     },
     *     "mappings" : {
     *         "_doc" : {
     *             "properties" : {
     *                 "uid" : { "type" : "long" },
     *                 "phone" : { "type" : "long" },
     *                 "userid" : { "type" : "keyword" },
     *                 "sendday" : { "type" : "long" },
     *                 "message" : { "type" : "keyword" },
     *                 "msgcode" : { "type" : "long" },
     *                 "price" : { "type" : "double","index": "false" },
     *              "sendtime" : {
     *                   "type" : "date",
     *                   "format" : "yyyy-MM-dd HH:mm:ss.SSS"
     *               },
     *              "sendtime2" : {
     *                   "type" : "date",
     *                   "format" : "yyyy-MM-dd HH:mm:ss.SSS"
     *               },
     *                 "sendtm" : { "type" : "long" },
     *                   "sendtm2" : { "type" : "long" }
     *             }
     *         }
     *     }
     * }
     *
     *  对应查询语句
     *  SELECT userid,sendday, (sendtm2-sendtm)as t FROM p_test where phone=12345678919 and (sendtm2-sendtm)>3801
     */
    String id = "InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(InternalSqlScriptUtils.sub(InternalSqlScriptUtils.docValue(doc,params.v0)," + "InternalSqlScriptUtils.docValue(doc,params.v1)),params.v2))";
    String lang = "painless";
    Map<String, Object> map = new HashMap<>();
    map.put("v0", "sendtm2");
    map.put("v1", "sendtm");
    map.put("v2", 3800);
    Script script = new Script(ScriptType.INLINE, lang, id, map);
    // searchSourceBuilder.scriptField("script",script);
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    boolQueryBuilder.must(QueryBuilders.termQuery("phone", "12345678919"));
    boolQueryBuilder.must(QueryBuilders.scriptQuery(script));
    // 设置查询条件
    searchSourceBuilder.query(boolQueryBuilder);
    searchSourceBuilder.storedField("_none_");
    searchSourceBuilder.docValueField("userid", "use_field_mapping");
    searchSourceBuilder.docValueField("sendday", "use_field_mapping");
    searchSourceBuilder.docValueField("sendtm2", "use_field_mapping");
    searchSourceBuilder.docValueField("sendtm", "use_field_mapping");
    System.out.println("查询语句:" + searchSourceBuilder.toString());
    searchRequest.source(searchSourceBuilder);
    // 同步查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    searchResponse.getHits().forEach(hit -> {
        Map<String, DocumentField> map1 = hit.getFields();
        Map<String, Object> map2 = new HashMap<>();
        System.out.println("\n查询的Map结果:" + map1);
        map1.forEach((s, objects) -> {
            map2.put(objects.getName(), objects.getValue());
        });
        System.out.println("\n查询的Map结果2:" + map2);
    });
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Script(org.elasticsearch.script.Script) DocumentField(org.elasticsearch.common.document.DocumentField) HashMap(java.util.HashMap) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 10 with DocumentField

use of org.elasticsearch.common.document.DocumentField in project tmail-backend by linagora.

the class ElasticSearchSearcher method extractContentFromHit.

private Optional<MessageSearchIndex.SearchResult> extractContentFromHit(SearchHit hit) {
    DocumentField mailboxId = hit.field(JsonMessageConstants.MAILBOX_ID);
    DocumentField uid = hit.field(JsonMessageConstants.UID);
    Optional<DocumentField> id = retrieveMessageIdField(hit);
    if (mailboxId != null && uid != null) {
        Number uidAsNumber = uid.getValue();
        return Optional.of(new MessageSearchIndex.SearchResult(id.map(field -> messageIdFactory.fromString(field.getValue())), mailboxIdFactory.fromString(mailboxId.getValue()), MessageUid.of(uidAsNumber.longValue())));
    } else {
        LOGGER.warn("Can not extract UID, MessageID and/or MailboxId for search result {}", hit.getId());
        return Optional.empty();
    }
}
Also used : MessageSearchIndex(org.apache.james.mailbox.store.search.MessageSearchIndex) DocumentField(org.elasticsearch.common.document.DocumentField)

Aggregations

DocumentField (org.elasticsearch.common.document.DocumentField)20 SearchHit (org.elasticsearch.search.SearchHit)8 Test (org.junit.jupiter.api.Test)6 SearchDocument (org.springframework.data.elasticsearch.core.document.SearchDocument)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 Text (org.elasticsearch.common.text.Text)5 LinkedHashMap (java.util.LinkedHashMap)4 LocalDateTime (java.time.LocalDateTime)3 HashMap (java.util.HashMap)3 GetResponse (org.elasticsearch.action.get.GetResponse)3 PrestoException (com.facebook.presto.spi.PrestoException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 GetResult (org.elasticsearch.index.get.GetResult)2 SearchHits (org.elasticsearch.search.SearchHits)2 Document (org.springframework.data.elasticsearch.core.document.Document)2 TrinoException (io.trino.spi.TrinoException)1 IOException (java.io.IOException)1 List (java.util.List)1 MessageSearchIndex (org.apache.james.mailbox.store.search.MessageSearchIndex)1