Search in sources :

Example 6 with EsAccessException

use of org.codelibs.fess.crawler.exception.EsAccessException in project fess-crawler by codelibs.

the class AbstractCrawlerService method getList.

protected <T> List<T> getList(final Class<T> clazz, final Consumer<SearchRequestBuilder> callback) {
    final SearchResponse response = getClient().get(c -> {
        final SearchRequestBuilder builder = c.prepareSearch(index).setTypes(type);
        callback.accept(builder);
        return builder.execute();
    });
    final EsResultList<T> targetList = new EsResultList<>();
    final SearchHits hits = response.getHits();
    targetList.setTotalHits(hits.getTotalHits());
    targetList.setTookInMillis(response.getTook().getMillis());
    if (hits.getTotalHits() != 0) {
        try {
            for (final SearchHit searchHit : hits.getHits()) {
                final Map<String, Object> source = searchHit.getSourceAsMap();
                final T target = BeanUtil.copyMapToNewBean(source, clazz, option -> {
                    option.converter(new EsTimestampConverter(), timestampFields).excludeWhitespace();
                    option.exclude(EsAccessResult.ACCESS_RESULT_DATA);
                });
                @SuppressWarnings("unchecked") final Map<String, Object> data = (Map<String, Object>) source.get(EsAccessResult.ACCESS_RESULT_DATA);
                if (data != null) {
                    ((EsAccessResult) target).setAccessResultData(new EsAccessResultData(data));
                }
                setId(target, searchHit.getId());
                targetList.add(target);
            }
        } catch (final Exception e) {
            throw new EsAccessException("response: " + response, e);
        }
    }
    return targetList;
}
Also used : EsAccessResultData(org.codelibs.fess.crawler.entity.EsAccessResultData) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ParseException(java.text.ParseException) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) IOException(java.io.IOException) SearchResponse(org.elasticsearch.action.search.SearchResponse) EsAccessResult(org.codelibs.fess.crawler.entity.EsAccessResult) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) SearchHits(org.elasticsearch.search.SearchHits) Map(java.util.Map) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) EsResultList(org.codelibs.fess.crawler.util.EsResultList)

Example 7 with EsAccessException

use of org.codelibs.fess.crawler.exception.EsAccessException in project fess-crawler by codelibs.

the class AbstractCrawlerService method insertAll.

protected <T> void insertAll(final List<T> list, final OpType opType, final boolean ignoreAlreadyExists) {
    final List<T> bufferedList = new ArrayList<>(bulkBufferSize);
    final StringBuilder failureBuf = new StringBuilder(100);
    list.stream().forEach(target -> {
        bufferedList.add(target);
        if (bufferedList.size() >= bulkBufferSize) {
            final BulkResponse response = doInsertAll(bufferedList, opType);
            if (response.hasFailures()) {
                final String failureMessage = buildFailureMessage(response, ignoreAlreadyExists);
                if (failureMessage.length() > 0) {
                    failureBuf.append(response.buildFailureMessage()).append('\n');
                }
            }
            bufferedList.clear();
        }
    });
    if (!bufferedList.isEmpty()) {
        final BulkResponse response = doInsertAll(bufferedList, opType);
        if (response.hasFailures()) {
            final String failureMessage = buildFailureMessage(response, ignoreAlreadyExists);
            if (failureMessage.length() > 0) {
                failureBuf.append(response.buildFailureMessage()).append('\n');
            }
        }
    }
    if (failureBuf.length() > 0) {
        throw new EsAccessException(failureBuf.toString());
    }
}
Also used : EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) ArrayList(java.util.ArrayList) BulkResponse(org.elasticsearch.action.bulk.BulkResponse)

Example 8 with EsAccessException

use of org.codelibs.fess.crawler.exception.EsAccessException in project fess-crawler by codelibs.

the class EsDataService method getAccessResultList.

public List<EsAccessResult> getAccessResultList(final Consumer<SearchRequestBuilder> callback) {
    final SearchResponse response = getClient().get(c -> {
        final SearchRequestBuilder builder = c.prepareSearch(index).setTypes(type);
        callback.accept(builder);
        builder.setFetchSource(new String[] { "parentUrl", "method", "mimeType", "sessionId", "url", "executionTime", "createTime", "contentLength", "lastModified", "ruleId", "httpStatusCode", "status" }, null);
        return builder.execute();
    });
    final EsResultList<EsAccessResult> targetList = new EsResultList<>();
    final SearchHits hits = response.getHits();
    targetList.setTotalHits(hits.getTotalHits());
    targetList.setTookInMillis(response.getTook().getMillis());
    if (hits.getTotalHits() != 0) {
        try {
            for (final SearchHit searchHit : hits.getHits()) {
                final EsAccessResult target = new EsAccessResult();
                final Map<String, Object> fields = searchHit.getSourceAsMap();
                target.setParentUrl(getFieldValue(fields.get("parentUrl"), String.class));
                target.setMethod(getFieldValue(fields.get("method"), String.class));
                target.setMimeType(getFieldValue(fields.get("mimeType"), String.class));
                target.setSessionId(getFieldValue(fields.get("sessionId"), String.class));
                target.setUrl(getFieldValue(fields.get("url"), String.class));
                target.setExecutionTime(getFieldValue(fields.get("executionTime"), Integer.class));
                target.setContentLength(getFieldValue(fields.get("contentLength"), Long.class));
                target.setRuleId(getFieldValue(fields.get("ruleId"), String.class));
                target.setHttpStatusCode(getFieldValue(fields.get("httpStatusCode"), Integer.class));
                target.setStatus(getFieldValue(fields.get("status"), Integer.class));
                target.setCreateTime(getFieldValue(fields.get("createTime"), Long.class));
                target.setLastModified(getFieldValue(fields.get("lastModified"), Long.class));
                setId(target, searchHit.getId());
                targetList.add(target);
            }
        } catch (final Exception e) {
            throw new EsAccessException("response: " + response, e);
        }
    }
    return targetList;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) SearchResponse(org.elasticsearch.action.search.SearchResponse) EsAccessResult(org.codelibs.fess.crawler.entity.EsAccessResult) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) SearchHits(org.elasticsearch.search.SearchHits) EsResultList(org.codelibs.fess.crawler.util.EsResultList)

Example 9 with EsAccessException

use of org.codelibs.fess.crawler.exception.EsAccessException in project fess-crawler by codelibs.

the class EsClient method deleteByQuery.

public int deleteByQuery(final String index, final String type, final QueryBuilder queryBuilder) {
    int count = 0;
    String scrollId = null;
    while (true) {
        final SearchResponse scrollResponse;
        if (scrollId == null) {
            scrollResponse = get(c -> c.prepareSearch(index).setTypes(type).setScroll(scrollForDelete).setSize(sizeForDelete).setQuery(queryBuilder).execute());
        } else {
            final String sid = scrollId;
            scrollResponse = get(c -> c.prepareSearchScroll(sid).setScroll(scrollForDelete).execute());
        }
        final SearchHit[] hits = scrollResponse.getHits().getHits();
        if (hits.length == 0) {
            break;
        }
        scrollId = scrollResponse.getScrollId();
        count += hits.length;
        final BulkResponse bulkResponse = get(c -> {
            final BulkRequestBuilder bulkRequest = client.prepareBulk();
            for (final SearchHit hit : hits) {
                bulkRequest.add(client.prepareDelete(hit.getIndex(), hit.getType(), hit.getId()));
            }
            return bulkRequest.execute();
        });
        if (bulkResponse.hasFailures()) {
            throw new EsAccessException(bulkResponse.buildFailureMessage());
        }
    }
    return count;
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) Arrays(java.util.Arrays) GetResponse(org.elasticsearch.action.get.GetResponse) ExplainRequest(org.elasticsearch.action.explain.ExplainRequest) LoggerFactory(org.slf4j.LoggerFactory) FieldCapabilitiesResponse(org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse) InetAddress(java.net.InetAddress) PreDestroy(javax.annotation.PreDestroy) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) Settings(org.elasticsearch.common.settings.Settings) DeleteRequestBuilder(org.elasticsearch.action.delete.DeleteRequestBuilder) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) FieldCapabilitiesRequest(org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest) TermVectorsRequest(org.elasticsearch.action.termvectors.TermVectorsRequest) ThreadPool(org.elasticsearch.threadpool.ThreadPool) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) SearchHit(org.elasticsearch.search.SearchHit) ActionRequest(org.elasticsearch.action.ActionRequest) GetRequest(org.elasticsearch.action.get.GetRequest) MultiTermVectorsRequest(org.elasticsearch.action.termvectors.MultiTermVectorsRequest) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) ExplainResponse(org.elasticsearch.action.explain.ExplainResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) MultiSearchRequestBuilder(org.elasticsearch.action.search.MultiSearchRequestBuilder) MultiTermVectorsRequestBuilder(org.elasticsearch.action.termvectors.MultiTermVectorsRequestBuilder) Scroll(org.elasticsearch.search.Scroll) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) List(java.util.List) FieldCapabilitiesRequestBuilder(org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) TransportAddress(org.elasticsearch.common.transport.TransportAddress) TermVectorsResponse(org.elasticsearch.action.termvectors.TermVectorsResponse) TermVectorsRequestBuilder(org.elasticsearch.action.termvectors.TermVectorsRequestBuilder) ClearScrollRequestBuilder(org.elasticsearch.action.search.ClearScrollRequestBuilder) AdminClient(org.elasticsearch.client.AdminClient) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) RandomUtils(org.apache.commons.lang3.RandomUtils) ExplainRequestBuilder(org.elasticsearch.action.explain.ExplainRequestBuilder) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) TransportClient(org.elasticsearch.client.transport.TransportClient) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) SearchRequest(org.elasticsearch.action.search.SearchRequest) Function(java.util.function.Function) ActionFuture(org.elasticsearch.action.ActionFuture) ClearScrollResponse(org.elasticsearch.action.search.ClearScrollResponse) ArrayList(java.util.ArrayList) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) TimeValue(org.elasticsearch.common.unit.TimeValue) IndexResponse(org.elasticsearch.action.index.IndexResponse) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) ActionRequestBuilder(org.elasticsearch.action.ActionRequestBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Logger(org.slf4j.Logger) ActionResponse(org.elasticsearch.action.ActionResponse) Client(org.elasticsearch.client.Client) StringUtil(org.codelibs.core.lang.StringUtil) MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TimeUnit(java.util.concurrent.TimeUnit) Action(org.elasticsearch.action.Action) GetRequestBuilder(org.elasticsearch.action.get.GetRequestBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ActionListener(org.elasticsearch.action.ActionListener) SearchScrollRequestBuilder(org.elasticsearch.action.search.SearchScrollRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse)

Aggregations

EsAccessException (org.codelibs.fess.crawler.exception.EsAccessException)9 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)5 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)5 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)5 SearchHit (org.elasticsearch.search.SearchHit)5 IOException (java.io.IOException)4 ParseException (java.text.ParseException)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)4 SearchHits (org.elasticsearch.search.SearchHits)4 List (java.util.List)3 StringUtil (org.codelibs.core.lang.StringUtil)3 EsAccessResult (org.codelibs.fess.crawler.entity.EsAccessResult)3 EsResultList (org.codelibs.fess.crawler.util.EsResultList)3 IndexResponse (org.elasticsearch.action.index.IndexResponse)3 TimeValue (org.elasticsearch.common.unit.TimeValue)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3