问题
Can someone give me example for using Elasticsearch in jHipster or in Spring-boot?
I have already generated entity using jHipster. There are input with placeholder :query !
/**
* SEARCH /_search/samples/:query -> search for the sample corresponding
* to the query.
*/
@RequestMapping(value = "/_search/samples/{query}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List<Sample> search(@PathVariable String query) {
return StreamSupport
.stream(sampleSearchRepository.search(queryString(query)).spliterator(), false)
.collect(Collectors.toList());
}
How can use the elasticsearch ?
Here some script that i found in generated Entity!
I have already tried placing Object, q=field:value, Array with elastic format and always got empty Array.
Sorry for bad english!.
回答1:
Well, it seems that your index is empty. If you add entities programmatically (i.e. not from the interface), be sure to save them in the search repository as well.
Example :
Sample sample = new Sample();
sample.setName("bar");
sample = sampleRepository.save(sample);
sampleSearchRepository.save(sample);
You need to reaffect sample to get the id generated by Hibernate correctly indexed in elasticsearch.
UPDATE 10/02/2016
There is now a JHipster module to reindex elasticsearch repositories.
回答2:
You should just do a GET call to the url by substituting the placeolder with the string that you want to search in the 'sample' entity.
You can test the call by using a rest client (eg. restclient plugin for firefox) or curl. The url should be something like:
http://localhost:8080/_search/samples/queryString
You'll get the results in json format.
If you need to customize the default search behaviour check the Elasticsearch documentation on Spring:
http://docs.spring.io/spring-data/elasticsearch/docs/1.0.5.RELEASE/reference/html/elasticsearch.repositories.html
来源:https://stackoverflow.com/questions/31400746/elasticsearch-on-jhipster-spring