Java API to query Elasticsearch with SQL

Due to business needs, there are many query requirements, which leads to a requirement to write a java api query method for elasticsearch. Later, I accidentally found a query using sql for elasticsearch, and it is relatively flexible to use. I will share it with you here. If there is something wrong, please correct me.

1. First add the jar package to the pom.xml file of the project

<dependency>
	<groupId>org.elasticsearch</groupId>
	<artifactId>elasticsearch</artifactId>
	<version>2.3.5</version>
</dependency>
<dependency>
	<groupId>org.elasticsearch.plugin</groupId>
	<artifactId>delete-by-query</artifactId>
	<version>2.3.2</version>
</dependency>
<dependency>
	<groupId>org.nlpcn</groupId>
	<artifactId>elasticsearch-sql</artifactId>
	<version>2.3.5.0</version>
</dependency>
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid</artifactId>
	<version>1.0.15</version>
</dependency>

 It should be noted here that the versions of elasticsearch and elasticsearch-sql need to be the same, otherwise some methods may not be found.

 

2. The public method of sql conversion (I use mysql format for sql here, other DB formats have not been verified)

/**
 * Assemble ES query query statement according to the expression
 *
 * @param indexName - index name
 * @param express-query condition: (f1=2 and f2=1) or (f3=1 and f4=1)
 * @return
 */
public static QueryBuilder createQueryBuilderByWhere(String indexName, String whereExpress) {
	BoolQueryBuilder boolQuery = null;
	try {
		String sql = "select * from " + indexName;
		String whereTemp = "";
		if (StringUtils.isNotBlank(whereExpress)) {
			whereTemp = " where " + whereExpress;
		}
		SQLQueryExpr sqlExpr = (SQLQueryExpr) toSqlExpr(sql + whereTemp);
		SqlParser sqlParser = new SqlParser();
		MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) sqlExpr.getSubQuery().getQuery();
		WhereParser whereParser = new WhereParser(sqlParser, query);
		Where where = whereParser.findWhere();
		if (where != null) {
			boolQuery = QueryMaker.explan(where);
		}
	} catch (SqlParseException e) {
		LOGGER.warn("EsQueryUtil.createQueryBuilderByExpress-Exception", e);
	}
	return boolQuery;
}

/**
 * Verify sql
 *
 * @param sql sql query statement
 * @return
 */
private static SQLExpr toSqlExpr(String sql) {
	SQLExprParser parser = new ElasticSqlExprParser(sql);
	SQLExpr expr = parser.expr();

	if (parser.getLexer().token() != Token.EOF) {
		throw new ParserException("illegal sql expr : " + sql);
	}
	return expr;
}

 

3. Simple query method

 

/**
 * Total number of query data
 *
 * @param indexName index name
 * @param whereExpress query condition
 * @return
 */
public static long searchTotalByApi(String indexName, String whereExpress) {
	try {
		// Get the Elasticsearch service
		Client client = EsClientUtil.getEsServer ();
		// Convert query conditions in Elasticsearch format
		QueryBuilder queryBuilder = createQueryBuilderByWhere(indexName, whereExpress);
		// Reduce resource consumption, only query the total number
		long resultNum = client.prepareSearch(indexName).setQuery(queryBuilder).setFrom(0).setSize(0).execute()
				.actionGet().getHits().getTotalHits();
		if (0 == resultNum) {
			LOGGER.info("EsQueryUtil.seatchTotalByApi-queryBuilder:{}", queryBuilder);
		}
		return resultNum;
	} catch (Exception e) {
		LOGGER.warn("EsQueryUtil.seatchTotalByApi-Exception{}", e);
	}
	return 0;
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326415362&siteId=291194637