Elasticsearch Java API fuzzy search test -


i have problems native elasticsearch java api. wanted create method search object name attribute. far easy, after wanted create junit test method , here starts problem.

    @test public void namesearchtest() throws elasticsearchunavailableexception, ioexception{     string nametosearch = "fuzzytext";     trainingtocreate t = new trainingtocreate();     t.setname(nametosearch);     //create 2 trainings find sth     string id1 = elasticindexer.index(t);     string id2 = elasticindexer.index(t);     //for creating delay, throws exception if id doesn't exist     elasticgetter.gettrainingbyid(id1);     elasticgetter.gettrainingbyid(id2);      int hits = 0;     arraylist<training> trainings = elasticsearch.fuzzysearchtrainingbyname(nametosearch, integer.max_value, 0);     system.out.println("first id: " + id1);     system.out.println("second id: " + id2);     string idoftraining;     if(trainings.size() == 0){         system.out.println("zero hits found.");     }     //just printing id's of results     //-------------------------------------------------     (int = 0; < trainings.size(); i++) {         idoftraining = trainings.get(i).getid();         system.out.println("training: "+i+" id: "+ idoftraining);     }     //-------------------------------------------------     (training training : trainings) {         if(training.getid().equals(id1)||training.getid().equals(id2)){             hits++;         }     }     asserttrue(hits>=2);     elasticdelete.deletetrainingbyid(id1);     elasticdelete.deletetrainingbyid(id2); } 

sometimes test works without problem, other times results of search contains nothing, if have created documents assure found. if in database of elasticsearch documents exists, guess implentation isn't right or search api has serious delay.

here code that's being tested:

public static arraylist<training> fuzzysearchtrainingbyname(string name, int size, int offset) throws elasticsearchunavailableexception, jsonparseexception, jsonmappingexception, ioexception {     client client = clientfactory.getclient(configservice.getconfig().getelasticsearchip(), configservice             .getconfig().getelasticsearchport());     return elasticsearch.fuzzysearchdocument(client, "trainings", "training", "name", name, size, offset); }  private static arraylist<training> fuzzysearchdocument(client client, string index, string type, string field, string value, int size, int offset) throws jsonparseexception, jsonmappingexception, ioexception {     querybuilder query = fuzzyquery(field, value);      searchresponse response = client.preparesearch(index).settypes(type)             .setquery(query).setsize(size).setfrom(offset).execute().actionget();      searchhits hits = response.gethits();      trainingtocreate source = null;     objectmapper mapper = new objectmapper();     arraylist<training> trainings = new arraylist<training>();      (searchhit searchhit : hits) {         source = mapper.readvalue(searchhit.getsourceasstring(), trainingtocreate.class);         trainings.add(trainingfactory.gettraining(searchhit.getid(), source));     }     return trainings;  } 

i working @ java 8 elastic 1.7.0 reconize position of problem? if needs further information, please feel free ask.

elasticsearch near real time, means there latency (default 1s) between moment index document , moment searchable. can overcome refreshing indices before running query.

so either after indexed sample documents...

public void namesearchtest() throws elasticsearchunavailableexception, ioexception{     string nametosearch = "fuzzytext";     trainingtocreate t = new trainingtocreate();     t.setname(nametosearch);     //create 2 trainings find sth     string id1 = elasticindexer.index(t);     string id2 = elasticindexer.index(t);      // refresh indices (just after indexing)     client().admin().indices().preparerefresh().execute().actionget(); 

... or @ beginning of fuzzysearchdocument

 private static arraylist<training> fuzzysearchdocument(client client, string index, string type, string field, string value, int size, int offset) throws jsonparseexception, jsonmappingexception, ioexception {      // refresh indices (just before searching)      client().admin().indices().preparerefresh().execute().actionget();       querybuilder query = fuzzyquery(field, value);      ... 

if run several test cases on sample documents, go first option, otherwise option do.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -