Search in sources :

Example 1 with EsAccessResultData

use of org.codelibs.fess.crawler.entity.EsAccessResultData in project fess-crawler by codelibs.

the class AbstractCrawlerService method get.

protected <T> T get(final Class<T> clazz, final String sessionId, final String url) {
    final String id = getId(sessionId, url);
    final GetResponse response = getClient().get(c -> c.prepareGet(index, type, id).execute());
    if (response.isExists()) {
        final Map<String, Object> source = response.getSource();
        final T bean = 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) bean).setAccessResultData(new EsAccessResultData(data));
        }
        setId(bean, id);
        return bean;
    }
    return null;
}
Also used : EsAccessResultData(org.codelibs.fess.crawler.entity.EsAccessResultData) EsAccessResult(org.codelibs.fess.crawler.entity.EsAccessResult) GetResponse(org.elasticsearch.action.get.GetResponse) Map(java.util.Map) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap)

Example 2 with EsAccessResultData

use of org.codelibs.fess.crawler.entity.EsAccessResultData 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 3 with EsAccessResultData

use of org.codelibs.fess.crawler.entity.EsAccessResultData in project fess-crawler by codelibs.

the class EsDataService method iterate.

@Override
public void iterate(final String sessionId, final AccessResultCallback<EsAccessResult> callback) {
    SearchResponse response = null;
    while (true) {
        if (response == null) {
            response = getClient().get(c -> c.prepareSearch(index).setTypes(type).setScroll(new TimeValue(scrollTimeout)).setQuery(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery(SESSION_ID, sessionId))).setSize(scrollSize).execute());
        } else {
            final String scrollId = response.getScrollId();
            response = getClient().get(c -> c.prepareSearchScroll(scrollId).setScroll(new TimeValue(scrollTimeout)).execute());
        }
        final SearchHits searchHits = response.getHits();
        if (searchHits.getHits().length == 0) {
            break;
        }
        for (final SearchHit searchHit : searchHits) {
            final Map<String, Object> source = searchHit.getSourceAsMap();
            final EsAccessResult accessResult = BeanUtil.copyMapToNewBean(source, EsAccessResult.class, 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) {
                accessResult.setAccessResultData(new EsAccessResultData(data));
            }
            callback.iterate(accessResult);
        }
    }
}
Also used : BeanUtil(org.codelibs.core.beans.util.BeanUtil) SearchHit(org.elasticsearch.search.SearchHit) AccessResultCallback(org.codelibs.fess.crawler.util.AccessResultCallback) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) SearchHits(org.elasticsearch.search.SearchHits) DataService(org.codelibs.fess.crawler.service.DataService) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) Consumer(java.util.function.Consumer) OpType(org.elasticsearch.action.DocWriteRequest.OpType) List(java.util.List) EsAccessResultData(org.codelibs.fess.crawler.entity.EsAccessResultData) EsAccessResult(org.codelibs.fess.crawler.entity.EsAccessResult) Map(java.util.Map) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) TimeValue(org.elasticsearch.common.unit.TimeValue) PostConstruct(javax.annotation.PostConstruct) SearchResponse(org.elasticsearch.action.search.SearchResponse) EsResultList(org.codelibs.fess.crawler.util.EsResultList) EsAccessResultData(org.codelibs.fess.crawler.entity.EsAccessResultData) SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse) EsAccessResult(org.codelibs.fess.crawler.entity.EsAccessResult) SearchHits(org.elasticsearch.search.SearchHits) Map(java.util.Map) TimeValue(org.elasticsearch.common.unit.TimeValue)

Aggregations

Map (java.util.Map)3 EsAccessResult (org.codelibs.fess.crawler.entity.EsAccessResult)3 EsAccessResultData (org.codelibs.fess.crawler.entity.EsAccessResultData)3 EsAccessException (org.codelibs.fess.crawler.exception.EsAccessException)2 EsResultList (org.codelibs.fess.crawler.util.EsResultList)2 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)2 SearchHit (org.elasticsearch.search.SearchHit)2 SearchHits (org.elasticsearch.search.SearchHits)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 PostConstruct (javax.annotation.PostConstruct)1 BeanUtil (org.codelibs.core.beans.util.BeanUtil)1 DataService (org.codelibs.fess.crawler.service.DataService)1 AccessResultCallback (org.codelibs.fess.crawler.util.AccessResultCallback)1 OpType (org.elasticsearch.action.DocWriteRequest.OpType)1 GetResponse (org.elasticsearch.action.get.GetResponse)1