From f0408a341bdefb9f4c17b2bfc8dff576f620d651 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 3 Aug 2018 14:04:18 +0300 Subject: search: ignore weight for one word query --- .../src/main/java/com/juick/service/SphinxSearchService.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'juick-server') 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 da27196f..40927bbb 100644 --- a/juick-server/src/main/java/com/juick/service/SphinxSearchService.java +++ b/juick-server/src/main/java/com/juick/service/SphinxSearchService.java @@ -38,6 +38,11 @@ public class SphinxSearchService extends BaseJdbcService implements SearchServic private int maxResult = DEFAULT_MAX_RESULT; + public String sortHint(String searchString) { + boolean isOneWord = searchString.split("[^\\S\\+]+").length == 1; + return isOneWord ? "extended:@id desc" : "extended:@weight desc, @id desc"; + } + @Override public List searchInAllMessages(final String searchString, final int page) { if (StringUtils.isBlank(searchString)) @@ -46,7 +51,7 @@ public class SphinxSearchService extends BaseJdbcService implements SearchServic Map sphinxQuery = new HashMap<>(); sphinxQuery.put("limit", String.valueOf(maxResult)); sphinxQuery.put("mode", "any"); - sphinxQuery.put("sort", "extended:@weight desc, @id desc"); + sphinxQuery.put("sort", sortHint(searchString)); if (page > 0) { sphinxQuery.put("offset", String.valueOf(page * maxResult)); } @@ -65,7 +70,7 @@ public class SphinxSearchService extends BaseJdbcService implements SearchServic Map sphinxQuery = new HashMap<>(); sphinxQuery.put("limit", String.valueOf(maxResult)); sphinxQuery.put("mode", "any"); - sphinxQuery.put("sort", "extended:@weight desc, @id desc"); + sphinxQuery.put("sort", sortHint(searchString)); if (page > 0) { sphinxQuery.put("offset", String.valueOf(page * maxResult)); } -- cgit v1.2.3