aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2020-06-05 12:17:52 +0300
committerGravatar Vitaly Takmazov2020-06-05 12:17:52 +0300
commit85c5c69ffb5a14258eafb4c67c550f6018b3c621 (patch)
treee7f3f6ef8680aad33ed0802ca681821cbfe69cca /src/main
parent63a52408f430cb2109943c18c37adef5c3bd8da0 (diff)
Append * to keyword search
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/com/juick/service/MessagesService.java4
-rw-r--r--src/main/java/com/juick/service/MessagesServiceImpl.java9
-rw-r--r--src/main/java/com/juick/service/SearchService.java4
-rw-r--r--src/main/java/com/juick/service/SphinxSearchService.java17
-rw-r--r--src/main/java/com/juick/www/api/Messages.java4
-rw-r--r--src/main/java/com/juick/www/controllers/Site.java8
6 files changed, 29 insertions, 17 deletions
diff --git a/src/main/java/com/juick/service/MessagesService.java b/src/main/java/com/juick/service/MessagesService.java
index 3e3e027d..da478349 100644
--- a/src/main/java/com/juick/service/MessagesService.java
+++ b/src/main/java/com/juick/service/MessagesService.java
@@ -89,7 +89,7 @@ public interface MessagesService {
List<Integer> getPhotos(int visitorUid, int before);
- List<Integer> getSearch(User visitor, String search, int page);
+ List<Integer> getSearch(User visitor, String search, int page, boolean isTag);
List<Integer> getUserBlog(int uid, int privacy, int before);
@@ -103,7 +103,7 @@ public interface MessagesService {
List<Integer> getUserPhotos(int uid, int privacy, int before);
- List<Integer> getUserSearch(User visitor, int UID, String search, int privacy, int page);
+ List<Integer> getUserSearch(User visitor, int UID, String search, int privacy, int page, boolean isTag);
List<Message> getMessages(User visitor, List<Integer> mids);
diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java
index c97bf87e..eb7b155d 100644
--- a/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -724,8 +724,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
@Transactional(readOnly = true)
@Override
- public List<Integer> getSearch(final User visitor, final String search, final int page) {
- return searchService.searchInAllMessages(visitor, search, page);
+ public List<Integer> getSearch(final User visitor, final String search, final int page, final boolean isTag) {
+ return searchService.searchInAllMessages(visitor, search, page, isTag);
}
@Transactional(readOnly = true)
@@ -863,8 +863,9 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
@Transactional(readOnly = true)
@Override
- public List<Integer> getUserSearch(final User visitor, final int UID, final String search, final int privacy, final int page) {
- return searchService.searchByStringAndUser(visitor, search, UID, page);
+ public List<Integer> getUserSearch(final User visitor, final int UID, final String search, final int privacy,
+ final int page, final boolean isTag) {
+ return searchService.searchByStringAndUser(visitor, search, UID, page, isTag);
}
@Transactional(readOnly = true)
diff --git a/src/main/java/com/juick/service/SearchService.java b/src/main/java/com/juick/service/SearchService.java
index fbcb61a9..f9ce25cc 100644
--- a/src/main/java/com/juick/service/SearchService.java
+++ b/src/main/java/com/juick/service/SearchService.java
@@ -27,7 +27,7 @@ import java.util.List;
public interface SearchService {
void setMaxResult(int maxResult);
- List<Integer> searchInAllMessages(User visitor, String searchString, int messageIdBefore);
+ List<Integer> searchInAllMessages(User visitor, String searchString, int messageIdBefore, boolean exact);
- List<Integer> searchByStringAndUser(User visitor, String searchString, final int userId, int messageIdBefore);
+ List<Integer> searchByStringAndUser(User visitor, String searchString, final int userId, int messageIdBefore, boolean exact);
}
diff --git a/src/main/java/com/juick/service/SphinxSearchService.java b/src/main/java/com/juick/service/SphinxSearchService.java
index dd06c485..1b80cea0 100644
--- a/src/main/java/com/juick/service/SphinxSearchService.java
+++ b/src/main/java/com/juick/service/SphinxSearchService.java
@@ -43,13 +43,19 @@ public class SphinxSearchService extends BaseJdbcService implements SearchServic
@Inject
UserService userService;
- public String sortHint(String searchString) {
+ String sortHint(String searchString) {
boolean isOneWord = searchString.split("[^\\S\\+]+").length == 1;
return isOneWord ? "extended:@id desc" : "extended:@weight desc, @id desc";
}
+ String searchTerm(String searchString, boolean exact) {
+ String prefix = exact ? "*" : "";
+ return String.format("%s%s", prefix, searchString);
+ }
+
@Override
- public List<Integer> searchInAllMessages(User visitor, final String searchString, final int page) {
+ public List<Integer> searchInAllMessages(User visitor, final String searchString, final int page,
+ final boolean exact) {
if (StringUtils.isBlank(searchString))
return Collections.emptyList();
@@ -64,13 +70,14 @@ public class SphinxSearchService extends BaseJdbcService implements SearchServic
}
return getJdbcTemplate().queryForList(
- String.format("SELECT id FROM search WHERE query = '%s;%s'", searchString,
+ String.format("SELECT id FROM search WHERE query = '%s;%s'", searchTerm(searchString, exact),
sphinxQuery.entrySet().stream().map(Object::toString)
.collect(Collectors.joining(";"))), Integer.class);
}
@Override
- public List<Integer> searchByStringAndUser(User visitor, final String searchString, final int userId, int page) {
+ public List<Integer> searchByStringAndUser(User visitor, final String searchString, final int userId, int page,
+ final boolean exact) {
if (StringUtils.isBlank(searchString))
return Collections.emptyList();
@@ -82,7 +89,7 @@ public class SphinxSearchService extends BaseJdbcService implements SearchServic
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,
+ String.format("SELECT id FROM search WHERE query = '%s;%s;filter=user_id,%d'", searchTerm(searchString, exact),
sphinxQuery.entrySet().stream().map(Object::toString)
.collect(Collectors.joining(";")), userId), Integer.class);
}
diff --git a/src/main/java/com/juick/www/api/Messages.java b/src/main/java/com/juick/www/api/Messages.java
index de29c78c..29c4c0ba 100644
--- a/src/main/java/com/juick/www/api/Messages.java
+++ b/src/main/java/com/juick/www/api/Messages.java
@@ -119,7 +119,7 @@ public class Messages {
} else if (daysback > 0) {
mids = messagesService.getUserBlogAtDay(user.getUid(), 0, daysback);
} else if (!StringUtils.isEmpty(search)) {
- mids = messagesService.getUserSearch(visitor, user.getUid(), WebUtils.encodeSphinx(search), 0, page);
+ mids = messagesService.getUserSearch(visitor, user.getUid(), WebUtils.encodeSphinx(search), 0, page, false);
} else {
mids = messagesService.getUserBlog(user.getUid(), 0, before);
}
@@ -139,7 +139,7 @@ public class Messages {
return NOT_FOUND;
}
} else if (!StringUtils.isEmpty(search)) {
- mids = messagesService.getSearch(visitor, WebUtils.encodeSphinx(search), page);
+ mids = messagesService.getSearch(visitor, WebUtils.encodeSphinx(search), page, false);
} else {
mids = messagesService.getAll(visitor.getUid(), before);
}
diff --git a/src/main/java/com/juick/www/controllers/Site.java b/src/main/java/com/juick/www/controllers/Site.java
index 537b0a6e..00b47000 100644
--- a/src/main/java/com/juick/www/controllers/Site.java
+++ b/src/main/java/com/juick/www/controllers/Site.java
@@ -108,7 +108,7 @@ public class Site {
if (paramSearch != null) {
title = "Поиск: " + StringEscapeUtils.escapeHtml4(paramSearch);
- mids = messagesService.getSearch(visitor, WebUtils.encodeSphinx(paramSearch), page);
+ mids = messagesService.getSearch(visitor, WebUtils.encodeSphinx(paramSearch), page, false);
} else if (paramShow == null) {
title = "Обсуждения";
mids = messagesService.getDiscussions(visitor.getUid(), paramTo);
@@ -194,8 +194,11 @@ public class Site {
List<Integer> mids;
+ boolean isTagSearch = false;
+
if (paramTagStr != null) {
paramSearch = paramTagStr;
+ isTagSearch = true;
}
if (paramSearch != null && paramSearch.length() > 64) {
paramSearch = null;
@@ -214,7 +217,8 @@ public class Site {
if (paramShow == null) {
if (paramSearch != null) {
title = "Блог " + user.getName() + ": " + StringEscapeUtils.escapeHtml4(paramSearch);
- mids = messagesService.getUserSearch(visitor, user.getUid(), WebUtils.encodeSphinx(paramSearch), privacy, page);
+ mids = messagesService.getUserSearch(visitor, user.getUid(), WebUtils.encodeSphinx(paramSearch),
+ privacy, page, isTagSearch);
} else {
title = "Блог " + user.getName();
mids = messagesService.getUserBlog(user.getUid(), privacy, before);