From 6afdad578701d38dd45d7d1c8ef446269f3a1275 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 16 Jul 2018 15:53:38 +0300 Subject: sphinx search: page by offset --- .../com/juick/service/MessagesServiceImpl.java | 24 ++++------------------ .../com/juick/service/SphinxSearchService.java | 12 +++++------ 2 files changed, 10 insertions(+), 26 deletions(-) (limited to 'juick-server/src/main/java/com/juick/service') diff --git a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java index d64ecc04..a78c9203 100644 --- a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -628,15 +628,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Transactional(readOnly = true) @Override - public List getSearch(final String search, final int before) { - List mids = searchService.searchInAllMessages(search, before); - - if (!mids.isEmpty()) - return getNamedParameterJdbcTemplate().queryForList( - "SELECT message_id FROM messages WHERE message_id IN (:ids) AND privacy > 0 ORDER BY message_id DESC LIMIT 20", - new MapSqlParameterSource("ids", mids), - Integer.class); - return mids; + public List getSearch(final String search, final int page) { + return searchService.searchInAllMessages(search, page); } @Transactional(readOnly = true) @@ -755,17 +748,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Transactional(readOnly = true) @Override - public List getUserSearch(final int UID, final String search, final int privacy, final int before) { - List mids = searchService.searchByStringAndUser(search, UID, before); - - if (!mids.isEmpty()) { - return getNamedParameterJdbcTemplate().queryForList( - "SELECT message_id FROM messages WHERE message_id IN (:ids) AND privacy >= :privacy ORDER BY message_id DESC", - new MapSqlParameterSource("ids", mids) - .addValue("privacy", privacy), - Integer.class); - } - return mids; + public List getUserSearch(final int UID, final String search, final int privacy, final int page) { + return searchService.searchByStringAndUser(search, UID, page); } @Transactional(readOnly = true) diff --git a/juick-server/src/main/java/com/juick/service/SphinxSearchService.java b/juick-server/src/main/java/com/juick/service/SphinxSearchService.java index f2f31295..da27196f 100644 --- a/juick-server/src/main/java/com/juick/service/SphinxSearchService.java +++ b/juick-server/src/main/java/com/juick/service/SphinxSearchService.java @@ -39,7 +39,7 @@ public class SphinxSearchService extends BaseJdbcService implements SearchServic private int maxResult = DEFAULT_MAX_RESULT; @Override - public List searchInAllMessages(final String searchString, final int messageIdBefore) { + public List searchInAllMessages(final String searchString, final int page) { if (StringUtils.isBlank(searchString)) return Collections.emptyList(); @@ -47,8 +47,8 @@ public class SphinxSearchService extends BaseJdbcService implements SearchServic sphinxQuery.put("limit", String.valueOf(maxResult)); sphinxQuery.put("mode", "any"); sphinxQuery.put("sort", "extended:@weight desc, @id desc"); - if (messageIdBefore > 0) { - sphinxQuery.put("maxid", String.valueOf(messageIdBefore - 1)); + if (page > 0) { + sphinxQuery.put("offset", String.valueOf(page * maxResult)); } return getJdbcTemplate().queryForList( @@ -58,7 +58,7 @@ public class SphinxSearchService extends BaseJdbcService implements SearchServic } @Override - public List searchByStringAndUser(final String searchString, final int userId, int messageIdBefore) { + public List searchByStringAndUser(final String searchString, final int userId, int page) { if (StringUtils.isBlank(searchString)) return Collections.emptyList(); @@ -66,8 +66,8 @@ public class SphinxSearchService extends BaseJdbcService implements SearchServic sphinxQuery.put("limit", String.valueOf(maxResult)); sphinxQuery.put("mode", "any"); sphinxQuery.put("sort", "extended:@weight desc, @id desc"); - if (messageIdBefore > 0) { - sphinxQuery.put("maxid", String.valueOf(messageIdBefore - 1)); + if (page > 0) { + sphinxQuery.put("offset", String.valueOf(page * maxResult)); } return getJdbcTemplate().queryForList( String.format("SELECT id FROM search WHERE query = '%s;%s;filter=user_id,%d'", searchString, -- cgit v1.2.3