Is any posability to make mustache query (or use existed script) in hibernate?

23 Views Asked by At

I have saved script in opensearch cluste. Is it possible to call it somehow via hibernate-search or make something like "native query" with mustache script?

1

There are 1 best solutions below

0
yrodiere On

You can embed native JSON predicates in Hibernate Search queries targeting Elasticsearch/OpenSearch, so this should work:

JsonObject jsonObject = /* your JSON using mustache */; 
List<Book> hits = searchSession.search( Book.class )
        .extension( ElasticsearchExtension.get() ) 
        .where( f -> f.fromJson( jsonObject ) ) 
        .fetchHits( 20 );

See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html for the syntax of script queries, and https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html for the syntax of the "script" object, so that you know how to structure your JSON.

Basically you'll need to build a JSON object looking like this:

{
        "script": {
          "lang": "mustache",
          "id": "your-stored-script-id"
        }
}