0
votes

I'm creating a Java application using Elastic Search. Here is the link for my project. https://github.com/chanakaDe/ensembl-elastic-rest

In this project, I have implemented a rest controller to take data as JSON. This is the controller class. Now it only has 2 methods. But I need to add some method like this.

@RequestMapping(value = "/find-by/{id}/{param1}/{param2}/{param3}", method = RequestMethod.GET)
public Iterable<Track> findAllWithParams(@PathVariable int id, @PathVariable String param1, @PathVariable String param2, @PathVariable String param3) {
    return trackService.someMethodWithParams(id, param1, param2, param3);
}

What I need to do is take some values from user and send them into Elastic server and make a search. I just refered some of these links and got some idea. https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search.html https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html

TrackService.java and TrackServiceImpl.java are implemented by TrackRepository.java and it's extended by ElasticsearchRepository default class. https://github.com/chanakaDe/ensembl-elastic-rest/blob/master/src/main/java/com/chanaka/book/repository/TrackRepository.java

I need to take values via REST URL and create an object like following and pass that to Elastic Server. How can I implement that with my current project configuration ?

{
"query": {
    "constant_score" : {
        "filter" : {
            "terms" : { "user" : ["kimchy", "elasticsearch"]}
        }
    }
}

}

This is my TrackService.java interface.

public interface TrackService {

Track save(Track track);

Track findOne(int id);

Iterable<Track> findAll();

}

And also this is my TrackServiceImpl.java class implemented by TrackService.java.

public class TrackServiceImpl implements TrackService {

private TrackRepository trackRepository;

@Autowired
public void setTrackRepository(TrackRepository trackRepository) {this.trackRepository = trackRepository;}

@Override
public Track save(Track track) {
    return trackRepository.save(track);
}

@Override
public Track findOne(int id) {
    return trackRepository.findOne(id + "");
}

@Override
public Iterable<Track> findAll() {
    return trackRepository.findAll();
}

}

Do I need to implement a custom method for that ? Or is there any default methods like findAll() and findOne() ? Simply pass an object and get the value ?

1
Guys no one to answer me :( - Chanaka De Silva

1 Answers

0
votes

I think, there's no such existing method and you need to create your own by using QueryBuilders.wrapperQuery(query.toString()) and ElasticsearchTemplate. Just to note, wrapperQuery supports only query not filter. But you can achieve filter context query with constant_score.