Search code examples
elasticsearchjhipsterspring-data-elasticsearch

How to make default ElasticSearch operation on entity with conditon( field LIKE not EQUAL )


I m new to ElasticSearch functionality , i ve a generated entity named project
this project entity have ( name ,description , budget and other more fields )
By default when ElasticSearch is enabled jhipster create a search box field and looks like it executes this sql request

 SELECT * from Project WHERE name="fieldValue" OR description ="fieldValue" OR ....

Here below search method in my ProjectServiceImpl

@Transactional(readOnly = true)
public Page<ProjectDTO> search(String query, Pageable pageable) { 
    return projectSearchRepository.search(queryStringQuery(query), pageable)
        .map(projectMapper::toDto);
}

But i want to list projects with ( condition WHERE name LIKE fieldValue OR description LIKE fieldValue ...)

To be honest I m new with ElasticSearch I don't know how to work with QueryStringQueryBuilder , I have done a lot googling but i don't find a concrete example and any help is appreciated.


Solution

  • I founded a workaround just i ve to use QueryBuilder and pass query parameter between two asterisks *

     public Page<ProjectDTO> search(String query, Pageable pageable) { 
    
           QueryBuilder queryBuilder = QueryBuilders.boolQuery()
                .should(QueryBuilders.queryStringQuery("*"+query+"*")
                 .lenient(true)
                    .field("name")
                    .field("description")); 
    
            return projectSearchRepository.search(queryBuilder, pageable)
                .map(projectMapper::toDto);
    }