spring-data-elasticsearch

@Query match _all function


I am new to spring data elasticsearch ,I want to impl this rest query

GET _search
{"query": {

"match": {
  "_all": "...myquerystring..."
}
}
}

@Query annotation write like this:

@Query("{\"match\":{\"_all\":\"?0\"}}") 

then I donn't know how to write query function

eg:

 @Query("{\"bool\" : {\"must\" : {\"term\" : {\"message\" : \"?0\"}}}}")
    Page<Book> findByMessage(String message, Pageable pageable);

how I write find function for _all query


Solution

  • Just try like this , @query annotation is not compulsory :-

    With Query :-

    @Query("{\"bool\" : {\"must\" : [ {\"match\" : {\"?0\" : \"?1\"}} ]}}")
    Page<MyDoc> getbyAll(String a, String b, Pageable pageable);
    
    int aPageReuestcount = (int) myRepositiry.count();
    Page<MyDoc> aData = myRepositiry.getbyAll("_all", "HelloWorld", new PageRequest(0, aPageReuestcount));
    List<MyDoc> aDataList = aData.getContent();
    

    Without Query :-

    @Override
    public List<MyDoc> getDatawithAllFields(String a) {
        NativeSearchQueryBuilder aNativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        aNativeSearchQueryBuilder.withIndices(indexName).withTypes(type);
        final BoolQueryBuilder aQuery = new BoolQueryBuilder();
    
        aQuery.must(QueryBuilders.matchQuery("_all",  a));
    
        NativeSearchQuery nativeSearchQuery = aNativeSearchQueryBuilder.withQuery(aQuery).build();
        List<MyDoc> aData = elasticsearchTemplate.queryForList(nativeSearchQuery, MyDoc.class);
        return aData;
    }
    

    Imports :-

    import org.elasticsearch.index.query.BoolQueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    

    follow this link .