0

I'm using the following code to filter by elastic search java api,it works fine and return result if i use string query ,but If i use text with spaces or uppercase letters it don't return any data

if use

    String query={"bool":{"should":[{"term":{"name":"test"}}]}}

return data

and if i use

  String query={"bool":{"should":[{"term":{"name":"test airportone"}}]}}  

or

  String query={"bool":{"should":[{"term":{"name":"TEST"}}]}}

return no data

String query={"bool":{"should":[{"term":{"name":"test airport one"}}]}}
BoolQueryBuilder bool = new BoolQueryBuilder();
    bool.must(new WrapperQueryBuilder(query));

SearchQuery searchQuery = new 
             NativeSearchQueryBuilder()
            .withQuery(bool)
            .build();
    Page<Asset> asset =
            elasticsearchTemplate.queryForPage(searchQuery,Asset.class);
        return asset.getContent();
Ali-Alrabi
  • 1,515
  • 6
  • 27
  • 60
  • possible duplicate of http://stackoverflow.com/questions/21933787/elasticsearch-not-returning-results-for-terms-query-against-string-property – ChintanShah25 Dec 12 '15 at 17:30

1 Answers1

0

You have two options depending on your use-case.

First option: You can use match instead of term to search for a string if you want to get advantage of ElasticSearch full text search capabilities.

{
    "bool": {
        "should": [{
            "match": {
                "name": "test airportone"
            }
        }]
    }
}

Second option: You can also specify that the name field is not analyzed when mapping your index so ElasticSearch will always store it as it is and always will get the exact match.

"mappings": {
    "user": {
        "properties": {
            "name": {
                "type": "string"
                "index": "not_analyzed"
            }
        }
    }
}
MIE
  • 444
  • 2
  • 9
  • Thanks for you replay , I'm using IndexQuery indexQuery = new IndexQueryBuilder().withId(id).withObject(asset).build(); elasticsearchTemplate.index(indexQuery); How can I index with not_analyzed – Ali-Alrabi Dec 13 '15 at 10:25
  • Using `@Field(type = String, index = not_analyzed)` `private String name;` you can import `not_analyzed` from `org.springframework.data.elasticsearch.annotations.FieldIndex` – MIE Dec 14 '15 at 05:49