All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scaleset.search.es.ElasticSearchDao Maven / Gradle / Ivy

There is a newer version: 0.24.0
Show newest version
package com.scaleset.search.es;

import static org.elasticsearch.client.Requests.createIndexRequest;
import static org.elasticsearch.client.Requests.deleteIndexRequest;

import java.util.ArrayList;
import java.util.List;

import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder;
import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.scaleset.search.AbstractSearchDao;
import com.scaleset.search.GenericSearchDao;
import com.scaleset.search.Query;
import com.scaleset.search.QueryBuilder;
import com.scaleset.search.Results;

public class ElasticSearchDao extends AbstractSearchDao implements GenericSearchDao {

    protected Client client;
    protected SearchMapping mapping;
    private Logger log = LoggerFactory.getLogger(getClass());

    public ElasticSearchDao(Client client, SearchMapping mapping) {
        this.client = client;
        this.mapping = mapping;
    }

    @Override
    public void close() {
        client.close();
    }

    @Override
    public void delete(T entity) throws Exception {
        String index = mapping.index(entity);
        String type = mapping.type(entity);
        String id = mapping.id(entity);
        client.prepareDelete(index, type, id).execute().actionGet();
    }

    @Override
    public void deleteByKey(K key) throws Exception {
        String index = mapping.indexForKey(key);
        String type = mapping.typeForKey(key);
        String id = mapping.idForKey(key);
        client.prepareDelete(index, type, id).execute().actionGet();
    }

    @Override
    public void deleteByQuery(Query query) throws Exception {
        DeleteByQueryRequestBuilder builder = createConverter(query).deleteRequest();
        DeleteByQueryResponse response = builder.execute().actionGet();
    }

    @Override
    public T findById(K key) throws Exception {
        String index = mapping.indexForKey(key);
        String type = mapping.typeForKey(key);
        String id = mapping.idForKey(key);
        try {
            GetResponse response = client.prepareGet(index, type, id).execute().actionGet();
            String source = response.getSourceAsString();
            if (source == null) {
                return null;
            } else {
                T result = mapping.fromDocument(id, source);
                return result;
            }
        } catch (Exception e) {
            log.error("error retrieving dao", e);
            throw e;
        }
    }

    @Override
    public T save(T entity) throws Exception {
        String id = mapping.id(entity);
        String index = mapping.index(entity);
        String type = mapping.type(entity);
        String source = mapping.toDocument(entity);
        client.prepareIndex(index, type, id).setSource(source).execute().actionGet();
        T result = entity;
        return result;
    }

    public List saveBatch(List entities) throws Exception {
        List result = new ArrayList<>();
        BulkRequestBuilder bulkRequest = client.prepareBulk();

        for (T entity : entities) {
            String id = mapping.id(entity);
            String index = mapping.index(entity);
            String type = mapping.type(entity);
            String source = mapping.toDocument(entity);
            bulkRequest.add(client.prepareIndex(index, type, id).setSource(source));
            result.add(entity);
        }

        BulkResponse bulkResponse = bulkRequest.execute().actionGet();
        if (bulkResponse.hasFailures()) {
            log.error("Bulk ingest has errors");
        }
        return result;
    }

    public Results search(Query query) throws Exception {
        Query updated = new QueryBuilder(query).build();
        SearchRequestBuilder srb = convert(updated);
        SearchResponse response = srb.execute().actionGet();
        Results results = createResultsConverter(updated, response, mapping).convert();
        return results;
    }

    public SearchRequestBuilder convert(Query query) throws Exception {
        return createConverter(query).searchRequest();
    }

    /**
     * Intended only for testing
     */
    public void flush() {
        client.admin().indices().prepareRefresh().execute().actionGet();
    }

    public void createIndex(String indexName) {
        CreateIndexResponse createResponse = client.admin().indices().create(createIndexRequest(indexName)).actionGet();
    }

    public void deleteIndex(String indexName) {
        DeleteIndexResponse deleteIndexResponse = client.admin().indices().delete(deleteIndexRequest(indexName)).actionGet();
    }

    public void recreateMapping(String index, String type, String schema) {

        // drop mapping incase it exists
        try {
            client.admin().indices().prepareDeleteMapping(index).setType(type).execute().actionGet();
        } catch (Exception e) {
        }
        // recreate mapping
        client.admin().indices().preparePutMapping(index).setType(type).setSource(schema).execute().actionGet();
    }

    public boolean indexExists(String name) {
        boolean result = false;
        try {
            IndicesExistsResponse exists = client.admin().indices().exists(new IndicesExistsRequest(name)).actionGet();
            result = exists.isExists();
        } catch (Exception e) {
            log.error("Error checking index exists");
        }
        return result;
    }

    public boolean mappingExists(String indexName, String typeName) {
        boolean result = false;
        try {
            String[] indices = new String[] { indexName };
            TypesExistsResponse exists = client.admin().indices().typesExists(new TypesExistsRequest(indices, typeName)).actionGet();
            result = exists.isExists();
        } catch (Exception e) {
            log.error("Error checking type exists");
        }
        return result;
    }

    protected SearchMapping getMapping() {
        return mapping;
    }

    protected DefaultQueryConverter createConverter(Query query) throws Exception {
        String[] indices = mapping.indicesForQuery(query);
        String[] types = mapping.typesForQuery(query);
        return new DefaultQueryConverter(client, query, indices, types);
    }

    protected ResultsConverter createResultsConverter(Query query, SearchResponse response, SearchMapping mapping) throws Exception {
        return new ResultsConverter(query, response, mapping);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy