Search in sources :

Example 1 with Result

use of org.elasticsearch.action.DocWriteResponse.Result in project fess by codelibs.

the class FessEsClient method store.

public boolean store(final String index, final String type, final Object obj) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    @SuppressWarnings("unchecked") final Map<String, Object> source = obj instanceof Map ? (Map<String, Object>) obj : BeanUtil.copyBeanToNewMap(obj);
    final String id = (String) source.remove(fessConfig.getIndexFieldId());
    final Long version = (Long) source.remove(fessConfig.getIndexFieldVersion());
    IndexResponse response;
    try {
        if (id == null) {
            // create
            response = client.prepareIndex(index, type).setSource(new DocMap(source)).setRefreshPolicy(RefreshPolicy.IMMEDIATE).setOpType(OpType.CREATE).execute().actionGet(fessConfig.getIndexIndexTimeout());
        } else {
            // create or update
            final IndexRequestBuilder builder = client.prepareIndex(index, type, id).setSource(new DocMap(source)).setRefreshPolicy(RefreshPolicy.IMMEDIATE).setOpType(OpType.INDEX);
            if (version != null && version.longValue() > 0) {
                builder.setVersion(version);
            }
            response = builder.execute().actionGet(fessConfig.getIndexIndexTimeout());
        }
        final Result result = response.getResult();
        return result == Result.CREATED || result == Result.UPDATED;
    } catch (final ElasticsearchException e) {
        throw new FessEsClientException("Failed to store: " + obj, e);
    }
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Result(org.elasticsearch.action.DocWriteResponse.Result) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) GetIndexResponse(org.elasticsearch.action.admin.indices.get.GetIndexResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) DocMap(org.codelibs.fess.util.DocMap) Map(java.util.Map) DocMap(org.codelibs.fess.util.DocMap) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)1 DocMap (org.codelibs.fess.util.DocMap)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 Result (org.elasticsearch.action.DocWriteResponse.Result)1 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)1 GetIndexResponse (org.elasticsearch.action.admin.indices.get.GetIndexResponse)1 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)1