# Is possible to use Lucene queries with ElasticSearch?

**URL:** https://discourse.hibernate.org/t/is-possible-to-use-lucene-queries-with-elasticsearch/6617
**Category:** Hibernate Search
**Created:** [August 31, 2022, 7:58am UTC](https://discourse.hibernate.org/t/is-possible-to-use-lucene-queries-with-elasticsearch/6617 "2022-08-31T07:58:13Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Monkiki](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/monkiki/32/850_2.png) [@Monkiki](https://discourse.hibernate.org/u/Monkiki)
#### Post date: [August 31, 2022, 7:58am UTC](https://discourse.hibernate.org/t/is-possible-to-use-lucene-queries-with-elasticsearch/6617/1 "2022-08-31T07:58:13Z")

</div>

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

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

```

In Lucene it would be:

```auto
+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.

---

<div class="post-metadata">

### Author: ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)
#### Post date: [August 31, 2022, 8:06am UTC](https://discourse.hibernate.org/t/is-possible-to-use-lucene-queries-with-elasticsearch/6617/2 "2022-08-31T08:06:17Z")

</div>

> [@Monkiki](#):
>
> I wonder if it’s possible to use a Lucene query directly

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:

- either with the [`simpleQueryString` predicate](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-dsl-predicate-simple-query-string) (it offers a different syntax from your example)
- or with a [native Elasticsearch predicate](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-dsl-predicate-extensions-elasticsearch-from-json) where you’ll pass the JSON for the [`queryString` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html). We will add an API for that predicate in Hibernate Search at some point, but that’s not implemented yet ([[HSEARCH-4563] - Hibernate JIRA](https://hibernate.atlassian.net/browse/HSEARCH-4563)).

> [@Monkiki](#):
>
> if there is any sort of “query converter” implemented in HIbernate Search 6.

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.

---

<div class="post-metadata">

### Author: ![Monkiki](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/monkiki/32/850_2.png) [@Monkiki](https://discourse.hibernate.org/u/Monkiki)
#### Post date: [August 31, 2022, 8:28am UTC](https://discourse.hibernate.org/t/is-possible-to-use-lucene-queries-with-elasticsearch/6617/3 "2022-08-31T08:28:12Z")

</div>

I was able to make the query using this code, which makes use of [query string query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html):

```auto
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](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-dsl-predicate-simple-query-string) 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](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-dsl-predicate-common-multiple-fields)) but with the same value.

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

Thanks!

---

<div class="post-metadata">

### Author: ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)
#### Post date: [August 31, 2022, 8:32am UTC](https://discourse.hibernate.org/t/is-possible-to-use-lucene-queries-with-elasticsearch/6617/4 "2022-08-31T08:32:19Z")

</div>

> [@Monkiki](#):
>
> `String query = "{\"query_string\":{\"query\":\"+name:temple*\"}}";`

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.

---

<div class="post-metadata">

### Author: ![Monkiki](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/monkiki/32/850_2.png) [@Monkiki](https://discourse.hibernate.org/u/Monkiki)
#### Post date: [August 31, 2022, 9:34am UTC](https://discourse.hibernate.org/t/is-possible-to-use-lucene-queries-with-elasticsearch/6617/5 "2022-08-31T09:34:00Z")

</div>

I already clean user input, but thanks anyway.

Best regards.
