0
votes

I am trying a elastic search example after inserting 6 records into my elastic search DB.

The records inserted are simple json with two properties Id and text

"OCC-001","Car Damaged During Loading"
"OCC-002","Car jango  jango  jango  Damaged Loading"
"OCC-003","hjjkhjkasd hjkhjklhas d Cars asdasd jango asdsas  jango  jango  Damaged asdsad asdasdasd Loading"
"OCC-004","This should not come at all "
"OCC-005","This may come Car"
"OCC-006","Will this come Cars"

I am doing a search like below

SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(
                matchQuery("title","Car Damaged"))
                .build();

        FacetedPage<Occurrence> occurrences = myElasticSearchRepository.search(searchQuery); 
        return occurrences; 

I am getting the incorrect results and in wrong order

"OCC-005","This may come Car"
"OCC-001","Car Damaged During Loading"
"OCC-002","Car jango  jango  jango  Damaged Loading"

When i do the same search using the below query I get right results but in different order

http://localhost:9200/myIndex/cars/_search?q=Car%20Damaged

"OCC-001","Car Damaged During Loading"
"OCC-002","Car jango  jango  jango  Damaged Loading"
"OCC-005","This may come Car"
"OCC-003","hjjkhjkasd hjkhjklhas d Cars asdasd jango asdsas  jango  jango  Damaged asdsad asdasdasd Loading"

Looks like I am dong something fundamentally incorrect.

Kindly advise.

3

3 Answers

0
votes

if you are new to Elasticsearch, i would suggest you try with Request Body Search instead of URI search. You can find it here. If you need a query tool, you can try Sense.

For the Java client library to interact with Elasticsearch here because i have faced a problem 1 year ago as Spring data elasticsearch is quite slow in update version to match with Elasticsearch version.

0
votes

I was using embedded elastic search for this example. After debugging for some time , I tried to drop and recreate the index.

You can monitor the indices using http://localhost:9200/_cat/indices?v. There are a cheat sheet for elastic search for all major functions - clear cache , refresh index etc.

Probably my index was corrupted. I dropped the old index and recreated index in different name and it worked as expected.

The search results were relevant and the score is matching the expected results.

0
votes

If you have more than one shard and small amount of data then you might get wrong scores when using default search type which is query_then_fetch, because when small amount of data gets scattered across several shards then IDF algorithm starts to give bad results. Most likely in this scenario you are looking for another search type which is dfs_query_then_fetch. It would be something like this

SearchQuery searchQuery = new NativeSearchQueryBuilder()
                                   .withQuery(matchQuery("title","Car Damaged"))                                    
                                   .withSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                                   .build();

Additional info about search type and why it is important can be found here https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-type.html#search-request-search-type