Heroku Elastic Search Example

Elastic search configuration

package com.example.elasticsearch;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.ClientConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JestConfiguration {

	@Bean
	public JestClient jestClient(){
		// Configuration
		ClientConfig clientConfig = new ClientConfig.Builder(System.getenv("SEARCHBOX_URL"))
		.multiThreaded(true).build();

		// Construct a new Jest client according to configuration via factory
		JestClientFactory factory = new JestClientFactory();
		factory.setClientConfig(clientConfig);
		JestClient client = factory.getObject();
		return client;
	}
}

Elastic search service

package com.example.elasticsearch;

import java.util.List;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Bulk;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.IndicesExists;

import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;

import com.example.jpa.JpaCustomerService;
import com.example.model.Customer;
import com.example.service.CustomerService;

@Service
@Import(JpaCustomerService.class)
public class JestService {

	@Autowired @Qualifier("jpaRepository") CustomerService jpaCustomerService;
	
	@Autowired JestClient jestClient;
	
    @Autowired
	public void createCustomerIndex(){
		try {
			IndicesExists indicesExists = new IndicesExists.Builder().build();
	        JestResult result = jestClient.execute(indicesExists);

            if (!result.isSucceeded()) {
                // Create customer index
                CreateIndex createIndex = new CreateIndex.Builder("customers").build();
                jestClient.execute(createIndex);
            }
      
            Bulk bulk = new Bulk.Builder()
		 	.addAction(new Index.Builder(jpaCustomerService.getAllCustomers()).index("customer").type("customer").build())
		    .build();

            result = jestClient.execute(bulk);

            System.out.println(result.getJsonString());
	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<Customer> searchCustomers(String customerQuery){
		try {
			SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
	        searchSourceBuilder.query(QueryBuilders.queryString(customerQuery));
	        
	        Search search = new Search.Builder(searchSourceBuilder.toString())
	        .addIndex("customer")
	        .addType("customer")
	        .build();
	
	        JestResult result = jestClient.execute(search);
			return result.getSourceAsObjectList(Customer.class);
		        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return null;
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s