All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.search.backend.elasticsearch.ElasticsearchQueries Maven / Gradle / Ivy

There is a newer version: 5.6.0.Alpha3
Show newest version
/*
 * 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() );
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy