Elasticssearch 1.7.5 and spring-data-elasticsearch 1.3.2


(dpr) #1

Hi friends,

I tested a java application with elasticsearch (1.7.5) and spring-data-elasticsearch(1.3.2). When I run the java application, I don't get any data.

SpringElasticsearchExample s = (SpringElasticsearchExample) ctx.getBean("mainBean");
System.out.println("Find all merchants");
s.findAllMerchants();

public void findAllMerchants() {
Iterable userList = repository.findAll();
for(User dto : userList){
System.out.println("values1= " + dto.getUserName());
}

OUTPUT

values1= 0

But when query from commandLine, I get data. Can anyone please help me with this.


(David Pilato) #2

Did you index data? Can you see them in elasticsearch?


(dpr) #3

Yes, when I search from commandLine in elasticsearch, I am able to see the data. But from Spring Data I am not able to get the data. Please suggest how to do.


(David Pilato) #4

I don't know. I'm not using Spring data.

May be a bad configuration in your annotations? Wrong index? Wrong type?

How did you insert your data? With Spring data?


(dpr) #5

I am using Logstash to pull data from MySQL and insert data into elasticsearch. I want to use Spring-data-elasticsearch api to connect to Elasticsearch and query the data.

What is the best way to connect to elasticsearch from Java & Spring application to search the elasticsearch store. ?


(David Pilato) #6

If you want to use spring-data, use spring-data.

But without knowing your code (annotations basically), index name, type name, I really can't help.

If you want to use Spring (not specifically spring data), you could try also https://github.com/dadoonet/spring-elasticsearch


(dpr) #7

Here is my Domain class

import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "test_for_175_index", type = "book_type")
public class BookElasticDTO {

	private String id;
	private String book_id;
	private int totalBooks,totalActiveBooks,totalPendingBooks,totalInActiveBooks,totalRejectedBooks,totalDeActivatedBooks;

// getter and setter methods
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import com.pn.dtos.elasticsearch.report.BoookElasticDTO;

public interface BookElasticRepository extends ElasticsearchRepository<BookElasticDTO, Long> {
	
}
@Configuration
public class SpringElasticsearchExample {
	
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private BookElasticRepository repository;
	
	@Autowired
	private ElasticsearchTemplate template;
	
	public static void main(String[] args)  throws URISyntaxException, Exception {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        try {

        	 System.out.println("Load context");
        	 SpringElasticsearchExample s = (SpringElasticsearchExample) ctx.getBean("mainBean");
        	 System.out.println("Find all books");
        	 s.findAllBooks();

        	} finally {
        		             ctx.close();
        		      }
    	}

        public void findAllBooks() {
        			System.out.println(repository.count());
        			if(repository.exists(9L)){
        			 System.out.println("It exists");	
        			}
        			
        			BookElasticDTO dt = repository.findOne(9L);
        			System.out.println("Value = " + dt.getTotalActiveBooks());
        			
        			SearchQuery query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.termQuery("book_id", "9")).build();
        			FacetedPage<BookElasticDTO> bookList = repository.search(query);
        			System.out.println("data = " + bookList.getContent());
        			
        			}
	}

(David Pilato) #8

Please format you code. I did it for you here.

What gives a:

curl "localhost:9200/test_for_175_index/book_type/_search?pretty&size=1"

And what is the output of your application?


(dpr) #9

I identified the mistake, it was the mismatch between domain name fields and query parameters fields. Its working now...


(system) #10