Is possible to use Lucene queries with ElasticSearch?

According to the documentation ElasticSearch queries are in JSON format like this:

{"wildcard":{"name":"temple*"}}

In Lucene it would be:

+name:temple*

But I wonder if it’s possible to use a Lucene query directly, or if there is any sort of “query converter” implemented in HIbernate Search 6.

Thanks in advance.

You cannot use an instance of org.apache.lucene.search.Query with the Elasticsearch backend, no.

You can pass a query string with the Lucene syntax to Elasticsearch, though:

There is not, and there never will be. We tried that in Hibernate Search 5 and it was a nightmare to maintain. 0/10, won’t do it again.

I was able to make the query using this code, which makes use of query string query:

String query = "{\"query_string\":{\"query\":\"+name:temple*\"}}";
SearchResult<NodeDocument> result = seSession.search(MyEntity.class)
		.extension(ElasticsearchExtension.get())
		.where(f -> f.fromJson(query))
		.fetch(20);

The simpleQueryString predicate is interesting, but I need to filter by several fields and values. I’ve seen I can filter by several fields (Targeting multiple fields in one predicate) but with the same value.

It would be nice to expand the simpleQueryString syntax to allow more powerful searches.

Thanks!

Just be careful about concatenating strings that contain user input: you expose yourself to injection vulnerabilities.

It’s way safer to use a JSON library to build objects and then render them as a JSON string. See for example JsonObject in GSON.

I already clean user input, but thanks anyway.

Best regards.