org.hibernate.search.backend.elasticsearch.ElasticsearchQueries Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-search-backend-elasticsearch Show documentation
Show all versions of hibernate-search-backend-elasticsearch Show documentation
Hibernate Search backend which has indexing operations forwarded to Elasticsearch
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.search.backend.elasticsearch;
import org.hibernate.search.backend.elasticsearch.impl.ElasticsearchJsonQueryDescriptor;
import org.hibernate.search.backend.elasticsearch.impl.JsonBuilder;
import org.hibernate.search.query.engine.spi.QueryDescriptor;
import com.google.gson.JsonParser;
/**
* Creates queries to be used with the Elasticsearch backend.
*
* @author Gunnar Morling
*/
public class ElasticsearchQueries {
private ElasticsearchQueries() {
}
/**
* Creates an Elasticsearch query from the given JSON query representation. See the official
* documentation for the complete query syntax.
*/
public static QueryDescriptor fromJson(String jsonQuery) {
return new ElasticsearchJsonQueryDescriptor( new JsonParser().parse( jsonQuery ).getAsJsonObject() );
}
/**
* Creates an Elasticsearch query from the given Query String Query, as e.g. to be used with the "q" parameter in
* the Elasticsearch API. See the official
* documentation for a description of the query syntax.
*/
public static QueryDescriptor fromQueryString(String queryStringQuery) {
// Payload looks like so:
// { "query" : { "query_string" : { "query" : "abstract:Hibernate" } } }
JsonBuilder.Object query = JsonBuilder.object().add( "query",
JsonBuilder.object().add( "queryString",
JsonBuilder.object().addProperty( "query", queryStringQuery ) ) );
return new ElasticsearchJsonQueryDescriptor( query.build() );
}
}