0
votes

How to retrieve data from elasticsearch containing special characters like . / - ... using spring data ?

I have document defined like this:

@Document(indexName = "audit-2018.135", type = "audit")
public class Trace {

    @Id
    private String id;

    @Field(type = FieldType.Text)
    private String uri;

    // setters & getters
}

I need to retrieve data from elasticSearch by field uri.

Here's an example how data looks:

martin-int-vip.vs.cz:5080/kib/api/runtime/case/CASE0000000000324223

When using kibana I can use:

uri: "martin-int-vip.vs.cz:5080/kib"

and I get back the record above which contains desired substring.

Now I need to achieve the same from java however in spring data it's not working as expected.

I have elasticSearchRepository defined like this:

public interface TraceRepository extends ElasticsearchRepository<Trace, String> {    
    List<Trace> findByUriContaining(String uri);
}

when I call method findByUriContaining with parameter uri as:

martin-int-vip.vs.cz:5080\/kib

or even this

martin-int

I get back 0 results. When I send "kib" as parameter it returns correctly all records containing word "kib" however it's not working with special characters like . / - etc. How should I query my elasticSearch from java to get all records which contains my desired substring ? Thanks

1

1 Answers

0
votes

I found out that by default elasticSearch analyzer tokenize your query. Instead of "martin-int-vip.vs.cz:5080/kib" it looks for a field which contains martin and int and vip...

In order to query these kind of fields you need to change behaviour of analyzer or you can use elasticSearchTemplate and Criteria to build more flexible queries.