Closes #305: exclude tags from search

This commit is contained in:
Benjamin Gamard 2019-05-03 15:35:47 +02:00
parent d654564f6b
commit 8b1c41ae1e
5 changed files with 41 additions and 3 deletions

View File

@ -51,6 +51,12 @@ public class DocumentCriteria {
*/ */
private List<List<String>> tagIdList; private List<List<String>> tagIdList;
/**
* Tag IDs to excluded.
* The first and second level list will be excluded.
*/
private List<List<String>> excludedTagIdList;
/** /**
* Shared status. * Shared status.
*/ */
@ -119,6 +125,15 @@ public class DocumentCriteria {
this.tagIdList = tagIdList; this.tagIdList = tagIdList;
} }
public List<List<String>> getExcludedTagIdList() {
return excludedTagIdList;
}
public DocumentCriteria setExcludedTagIdList(List<List<String>> excludedTagIdList) {
this.excludedTagIdList = excludedTagIdList;
return this;
}
public Boolean getShared() { public Boolean getShared() {
return shared; return shared;
} }

View File

@ -309,6 +309,19 @@ public class LuceneIndexingHandler implements IndexingHandler {
criteriaList.add("(" + Joiner.on(" OR ").join(tagCriteriaList) + ")"); criteriaList.add("(" + Joiner.on(" OR ").join(tagCriteriaList) + ")");
} }
} }
if (criteria.getExcludedTagIdList() != null && !criteria.getExcludedTagIdList().isEmpty()) {
int index = 0;
for (List<String> tagIdList : criteria.getExcludedTagIdList()) {
List<String> tagCriteriaList = Lists.newArrayList();
for (String tagId : tagIdList) {
sb.append(String.format("left join T_DOCUMENT_TAG dtex%d on dtex%d.DOT_IDDOCUMENT_C = d.DOC_ID_C and dtex%d.DOT_IDTAG_C = :tagIdEx%d and dtex%d.DOT_DELETEDATE_D is null ", index, index, index, index, index));
parameterMap.put("tagIdEx" + index, tagId);
tagCriteriaList.add(String.format("dtex%d.DOT_ID_C is null", index));
index++;
}
criteriaList.add("(" + Joiner.on(" AND ").join(tagCriteriaList) + ")");
}
}
if (criteria.getShared() != null && criteria.getShared()) { if (criteria.getShared() != null && criteria.getShared()) {
criteriaList.add("s.count > 0"); criteriaList.add("s.count > 0");
} }

View File

@ -459,11 +459,15 @@ public class DocumentResource extends BaseResource {
switch (params[0]) { switch (params[0]) {
case "tag": case "tag":
case "!tag":
// New tag criteria // New tag criteria
List<TagDto> tagDtoList = TagUtil.findByName(params[1], allTagDtoList); List<TagDto> tagDtoList = TagUtil.findByName(params[1], allTagDtoList);
if (documentCriteria.getTagIdList() == null) { if (documentCriteria.getTagIdList() == null) {
documentCriteria.setTagIdList(new ArrayList<>()); documentCriteria.setTagIdList(new ArrayList<>());
} }
if (documentCriteria.getExcludedTagIdList() == null) {
documentCriteria.setExcludedTagIdList(new ArrayList<>());
}
if (tagDtoList.isEmpty()) { if (tagDtoList.isEmpty()) {
// No tag found, the request must returns nothing // No tag found, the request must returns nothing
documentCriteria.getTagIdList().add(Lists.newArrayList(UUID.randomUUID().toString())); documentCriteria.getTagIdList().add(Lists.newArrayList(UUID.randomUUID().toString()));
@ -476,8 +480,12 @@ public class DocumentResource extends BaseResource {
tagIdList.add(childrenTagDto.getId()); tagIdList.add(childrenTagDto.getId());
} }
} }
if (params[0].startsWith("!")) {
documentCriteria.getExcludedTagIdList().add(tagIdList);
} else {
documentCriteria.getTagIdList().add(tagIdList); documentCriteria.getTagIdList().add(tagIdList);
} }
}
break; break;
case "after": case "after":
case "before": case "before":

View File

@ -319,9 +319,9 @@ angular.module('docs').controller('Document', function ($scope, $rootScope, $tim
*/ */
$scope.extractNavigatedTag = function () { $scope.extractNavigatedTag = function () {
// Find the current tag in the search query // Find the current tag in the search query
var tagFound = /tag:([^ ]*)/.exec($scope.search); var tagFound = /(^| )tag:([^ ]*)/.exec($scope.search);
if (tagFound) { if (tagFound) {
tagFound = tagFound[1]; tagFound = tagFound[2];
// We search only for exact match // We search only for exact match
$scope.navigatedTag = _.findWhere($scope.tags, { name: tagFound }); $scope.navigatedTag = _.findWhere($scope.tags, { name: tagFound });
} else { } else {

View File

@ -209,7 +209,9 @@ public class TestDocumentResource extends BaseJerseyTest {
Assert.assertEquals(2, searchDocuments("uat:" + DateTimeFormat.forPattern("yyyy-MM-dd").print(new Date().getTime()), document1Token)); Assert.assertEquals(2, searchDocuments("uat:" + DateTimeFormat.forPattern("yyyy-MM-dd").print(new Date().getTime()), document1Token));
Assert.assertEquals(2, searchDocuments("uafter:2010 ubefore:2040-08", document1Token)); Assert.assertEquals(2, searchDocuments("uafter:2010 ubefore:2040-08", document1Token));
Assert.assertEquals(1, searchDocuments("tag:super", document1Token)); Assert.assertEquals(1, searchDocuments("tag:super", document1Token));
Assert.assertEquals(1, searchDocuments("!tag:super", document1Token));
Assert.assertEquals(1, searchDocuments("tag:super tag:hr", document1Token)); Assert.assertEquals(1, searchDocuments("tag:super tag:hr", document1Token));
Assert.assertEquals(0, searchDocuments("tag:super !tag:hr", document1Token));
Assert.assertEquals(1, searchDocuments("shared:yes", document1Token)); Assert.assertEquals(1, searchDocuments("shared:yes", document1Token));
Assert.assertEquals(2, searchDocuments("lang:eng", document1Token)); Assert.assertEquals(2, searchDocuments("lang:eng", document1Token));
Assert.assertEquals(1, searchDocuments("after:2010 before:2040-08 tag:super shared:yes lang:eng title description full:uranium", document1Token)); Assert.assertEquals(1, searchDocuments("after:2010 before:2040-08 tag:super shared:yes lang:eng title description full:uranium", document1Token));