From 84b2f994bd9b6b46155dda5953922cc0882c6514 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 1 Jun 2018 11:09:49 +0300 Subject: server: fix SphinxSE query languae mapping --- .../com/juick/service/SphinxSearchService.java | 47 +++++++++++----------- 1 file changed, 24 insertions(+), 23 deletions(-) (limited to 'juick-server-jdbc/src/main/java/com/juick/service') diff --git a/juick-server-jdbc/src/main/java/com/juick/service/SphinxSearchService.java b/juick-server-jdbc/src/main/java/com/juick/service/SphinxSearchService.java index 26cf3a16..d8330908 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/SphinxSearchService.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/SphinxSearchService.java @@ -18,12 +18,14 @@ package com.juick.service; import org.apache.commons.lang3.StringUtils; -import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; /** * Created by aalexeev on 11/18/16. @@ -41,17 +43,17 @@ public class SphinxSearchService extends BaseJdbcService implements SearchServic if (StringUtils.isBlank(searchString)) return Collections.emptyList(); - MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource() - .addValue("search", searchString) - .addValue("before", messageIdBefore - 1) - .addValue("limit", maxResult); + Map sphinxQuery = new HashMap<>(); + sphinxQuery.put("limit", String.valueOf(maxResult)); + sphinxQuery.put("mode", "any"); + sphinxQuery.put("sort", "extended:@id desc"); + if (messageIdBefore > 0) { + sphinxQuery.put("maxid", String.valueOf(messageIdBefore - 1)); + } - return getNamedParameterJdbcTemplate().queryForList( - "SELECT id FROM search WHERE query = ':search;limit=:limit;mode=any;sort=extended:@id desc" + - (messageIdBefore > 0 ? - ";maxid=:before" : StringUtils.EMPTY) + "'", - sqlParameterSource, - Integer.class); + return getJdbcTemplate().queryForList( + "SELECT id FROM search WHERE query = '?;?'", Integer.class, searchString, + sphinxQuery.keySet().stream().map(Object::toString).collect(Collectors.joining(";"))); } @Override @@ -59,18 +61,17 @@ public class SphinxSearchService extends BaseJdbcService implements SearchServic if (StringUtils.isBlank(searchString)) return Collections.emptyList(); - MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource() - .addValue("search", searchString) - .addValue("userId", userId) - .addValue("before", messageIdBefore - 1) - .addValue("limit", maxResult); - - return getNamedParameterJdbcTemplate().queryForList( - "SELECT id FROM search WHERE query = ':search;filter=user_id,:userId;sort=extended:@id desc" + - (messageIdBefore > 0 ? - ";maxid=:before" : StringUtils.EMPTY) + "'", - sqlParameterSource, - Integer.class); + Map sphinxQuery = new HashMap<>(); + sphinxQuery.put("limit", String.valueOf(maxResult)); + sphinxQuery.put("mode", "any"); + sphinxQuery.put("sort", "extended:@id desc"); + if (messageIdBefore > 0) { + sphinxQuery.put("maxid", String.valueOf(messageIdBefore - 1)); + } + return getJdbcTemplate().queryForList( + "SELECT id FROM search WHERE query = '?;filter=user_id,?;?'", + Integer.class, searchString, userId, + sphinxQuery.keySet().stream().map(Object::toString).collect(Collectors.joining(";"))); } @Override -- cgit v1.2.3