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

com.lordofthejars.nosqlunit.demo.elasticsearch.BookManager Maven / Gradle / Ivy

There is a newer version: 1.0.0-rc.5
Show newest version
package com.lordofthejars.nosqlunit.demo.elasticsearch;

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

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.search.SearchHit;

import com.lordofthejars.nosqlunit.demo.custom.MapBookConverter;
import com.lordofthejars.nosqlunit.demo.model.Book;

public class BookManager {

	private Client client;

	public BookManager(Client client) {
		this.client = client;
	}

	public void create(Book book) {
		IndexRequestBuilder indexRequestBuilder = this.client.prepareIndex("books", "book", null).setSource(
				MapBookConverter.toMap(book));
		indexRequestBuilder.execute().actionGet();
	}

	public Book findBookById(String id) {
		GetResponse actionGet = this.client.prepareGet("books", "book", id).execute().actionGet();
		return MapBookConverter.toBook(actionGet.getSource());
	}

	public List searchAllBooks() {
		
		List books = new ArrayList();
		
		SearchResponse response = this.client.prepareSearch().execute().actionGet();
		
		for (SearchHit hit : response.getHits()) {
			books.add(MapBookConverter.toBook(hit.getSource()));
		}
		
		return books;
		
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy