-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Search elements are now returned paged #149
Changes from 10 commits
eaee743
b95be6b
5a7cb79
7976773
f6196ab
8f6101d
9ca3245
c19e536
334b3e4
d08161d
16e4d09
7a1755a
e290d18
ae2639f
57f70de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.gridsuite.directory.server.elasticsearch; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.elasticsearch.core.SearchHit; | ||
import org.springframework.data.elasticsearch.core.SearchHits; | ||
|
||
import java.util.List; | ||
|
||
public final class ESUtils { | ||
|
||
private ESUtils() { | ||
// This constructor is private to prevent instantiation of the utility class. | ||
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); | ||
} | ||
|
||
public static <T> Page<T> searchHitsToPage(SearchHits<T> searchHits, Pageable pageable) { | ||
List<T> content = searchHits.stream().map(SearchHit::getContent).toList(); | ||
return new PageImpl<>(content, pageable, searchHits.getTotalHits()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ | |
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import java.time.Instant; | ||
|
@@ -86,15 +88,27 @@ void searchElementInfos() { | |
List<DirectoryElementInfos> infos = List.of(directoryInfos, filterInfos, studyInfos, caseInfos, contingencyListInfos); | ||
repositoryService.saveElementsInfos(infos); | ||
|
||
Set<DirectoryElementInfos> hits = new HashSet<>(directoryElementInfosService.searchElements("a", "")); | ||
assertEquals(4, hits.size()); | ||
assertTrue(hits.contains(studyInfos)); | ||
assertTrue(hits.contains(caseInfos)); | ||
assertTrue(hits.contains(filterInfos)); | ||
assertTrue(hits.contains(contingencyListInfos)); | ||
Page<DirectoryElementInfos> pagedHits = directoryElementInfosService.searchElements("a", "", PageRequest.of(0, 10)); | ||
assertEquals(4, pagedHits.getTotalElements()); | ||
assertTrue(pagedHits.getContent().contains(studyInfos)); | ||
assertTrue(pagedHits.getContent().contains(caseInfos)); | ||
assertTrue(pagedHits.getContent().contains(filterInfos)); | ||
assertTrue(pagedHits.getContent().contains(contingencyListInfos)); | ||
|
||
hits = new HashSet<>(directoryElementInfosService.searchElements("aDirectory", "")); | ||
assertEquals(0, hits.size()); | ||
pagedHits = directoryElementInfosService.searchElements("aDirectory", "", PageRequest.of(0, 10)); | ||
assertEquals(0, pagedHits.getTotalElements()); | ||
} | ||
|
||
@Test | ||
void searchPagedElementInfos() { | ||
List<DirectoryElementInfos> elements = new ArrayList<>(20); | ||
for (int i = 0; i < 20; i++) { | ||
elements.add(createFilter("filter" + i)); | ||
} | ||
repositoryService.saveElementsInfos(elements); | ||
Page<DirectoryElementInfos> pagedHits = directoryElementInfosService.searchElements("filter", "", PageRequest.of(0, 10)); | ||
assertEquals(20, pagedHits.getTotalElements()); | ||
assertEquals(10, pagedHits.getContent().size()); | ||
} | ||
|
||
@Test | ||
|
@@ -124,7 +138,11 @@ void searchSpecialChars() { | |
} | ||
|
||
private void testNameFullAscii(String pat) { | ||
assertEquals(1, directoryElementInfosService.searchElements(pat, "").size()); | ||
assertEquals(1, directoryElementInfosService.searchElements(pat, "", PageRequest.of(0, 10)).getTotalElements()); | ||
} | ||
|
||
private DirectoryElementInfos createFilter(String name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we already discussed , the element type such as STUDY,FILTER... should not be specified on directory server. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i renamed the function 'createElement' |
||
return DirectoryElementInfos.builder().id(UUID.randomUUID()).name(name).type(FILTER).owner("admin").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(Instant.now().truncatedTo(ChronoUnit.SECONDS)).build(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You steel using "FILTER" and "STUDY" as equipment type |
||
} | ||
|
||
private DirectoryElementInfos makeElementDir(String name) { | ||
|
@@ -195,14 +213,14 @@ HashMap<String, DirectoryElementInfos> createFilesElements() { | |
void testExactMatchFromSubDirectory() { | ||
Map<String, DirectoryElementInfos> allDirs = createFilesElements(); | ||
UUID currentDirUuid = allDirs.get("sub_sub_directory1_2").getId(); | ||
List<DirectoryElementInfos> hitsCommunFile = directoryElementInfosService.searchElements("common_file", currentDirUuid.toString()); | ||
List<DirectoryElementInfos> hitsCommunFile = directoryElementInfosService.searchElements("common_file", currentDirUuid.toString(), PageRequest.of(0, 10)).stream().toList(); | ||
assertEquals(6, hitsCommunFile.size()); | ||
assertEquals(currentDirUuid, hitsCommunFile.get(0).getParentId()); // we get first the element in the current directory | ||
assertEquals("common_file", hitsCommunFile.get(0).getName()); | ||
|
||
//now using another current dir , we expect similar results | ||
currentDirUuid = allDirs.get("sub_sub_directory2_2").getId(); | ||
hitsCommunFile = directoryElementInfosService.searchElements("common_file", currentDirUuid.toString()); | ||
hitsCommunFile = directoryElementInfosService.searchElements("common_file", currentDirUuid.toString(), PageRequest.of(0, 10)).stream().toList(); | ||
assertEquals(6, hitsCommunFile.size()); | ||
assertEquals(currentDirUuid, hitsCommunFile.get(0).getParentId()); // we get first the element in the current directory | ||
assertEquals("common_file", hitsCommunFile.get(0).getName()); | ||
|
@@ -212,7 +230,7 @@ void testExactMatchFromSubDirectory() { | |
void testExactMatchFromOtherDirectory() { | ||
Map<String, DirectoryElementInfos> allDirs = createFilesElements(); | ||
UUID currentDirUuid = allDirs.get("sub_sub_directory1_2").getId(); | ||
List<DirectoryElementInfos> hits = directoryElementInfosService.searchElements("file3", currentDirUuid.toString()); | ||
List<DirectoryElementInfos> hits = directoryElementInfosService.searchElements("file3", currentDirUuid.toString(), PageRequest.of(0, 10)).stream().toList(); | ||
assertEquals(1, hits.size()); | ||
assertEquals(allDirs.get("sub_directory3").getId(), hits.get(0).getParentId()); | ||
assertEquals("file3", hits.get(0).getName()); | ||
|
@@ -244,7 +262,7 @@ void testExactMatchingParentDirectory() { // when a file is in a sub directory o | |
//we want to have the files in the current directory if any | ||
// then the files in the path of the current directory (sub directories and parent directories) | ||
// then the files in the other directories | ||
List<DirectoryElementInfos> hitsFile = directoryElementInfosService.searchElements("new-file", currentDirUuid.toString()); | ||
List<DirectoryElementInfos> hitsFile = directoryElementInfosService.searchElements("new-file", currentDirUuid.toString(), PageRequest.of(0, 10)).stream().toList(); | ||
assertEquals(3, hitsFile.size()); | ||
assertEquals(newFile1, hitsFile.get(0)); | ||
assertEquals(newFile2, hitsFile.get(1)); | ||
|
@@ -255,28 +273,43 @@ void testExactMatchingParentDirectory() { // when a file is in a sub directory o | |
void testPartialMatchFromSubDirectory() { | ||
HashMap<String, DirectoryElementInfos> allDirs = createFilesElements(); | ||
UUID currentDirUuid = allDirs.get("sub_sub_directory1_2").getId(); | ||
List<DirectoryElementInfos> hitsFile = directoryElementInfosService.searchElements("file", currentDirUuid.toString()); | ||
List<DirectoryElementInfos> hitsFile = directoryElementInfosService.searchElements("file", currentDirUuid.toString(), PageRequest.of(0, 10)).stream().toList(); | ||
assertEquals(9, hitsFile.size()); | ||
assertEquals(currentDirUuid, hitsFile.get(0).getParentId()); // we get first the elements in the current directory | ||
assertEquals("common_file", hitsFile.get(0).getName()); | ||
assertEquals("file1", hitsFile.get(1).getName()); // we get second the elements in the path | ||
} | ||
|
||
|
||
/* | ||
root_directory | ||
├── sub_directory1 | ||
.... | ||
├── sub_directory2 | ||
│ ├── bnew-filebbbb | ||
│ ├── anew-file | ||
│ ├── new-file | ||
│ ├── test-new-file | ||
... | ||
*/ | ||
@Test | ||
void testExactMatchInCurrentDir() { | ||
HashMap<String, DirectoryElementInfos> allDirs = createFilesElements(); | ||
UUID currentDirUuid = allDirs.get("sub_sub_directory1_2").getId(); | ||
String fileName = "new-file"; | ||
var newFile = makeElementFile(fileName, allDirs.get("sub_sub_directory1_2").getId()); | ||
var newFile1 = makeElementFile(fileName + "1", allDirs.get("sub_sub_directory1_2").getId()); | ||
var newFile2 = makeElementFile("1" + fileName + "2", allDirs.get("sub_sub_directory1_2").getId()); | ||
repositoryService.saveElementsInfos(List.of(newFile1, newFile, newFile2)); | ||
void testTermStartByUserInput() { // when a file start with search term | ||
Map<String, DirectoryElementInfos> allDirs = createFilesElements(); | ||
UUID currentDirUuid = allDirs.get("sub_directory2").getId(); | ||
var anewFile1 = makeElementFile("anew-file", allDirs.get("sub_directory2").getId()); | ||
var newFile2 = makeElementFile("new-file-Ok", allDirs.get("sub_directory2").getId()); | ||
var bNewFile = makeElementFile("bnew-filebbbb", allDirs.get("sub_directory2").getId()); | ||
var testNewFile = makeElementFile("test-new-file", allDirs.get("sub_directory2").getId()); | ||
repositoryService.saveElementsInfos(List.of(bNewFile, newFile2, anewFile1, testNewFile)); | ||
|
||
List<DirectoryElementInfos> hitsFile = directoryElementInfosService.searchElements(fileName, currentDirUuid.toString()); | ||
assertEquals(3, hitsFile.size()); | ||
assertEquals(fileName, hitsFile.get(0).getName()); | ||
assertEquals(fileName + "1", hitsFile.get(1).getName()); | ||
assertEquals("1" + fileName + "2", hitsFile.get(2).getName()); | ||
//we want to have the files in the current directory if any | ||
// then the files in the path of the current directory (sub directories and parent directories) | ||
// then the files in the other directories | ||
List<DirectoryElementInfos> hitsFile = directoryElementInfosService.searchElements("new-file", currentDirUuid.toString(), PageRequest.of(0, 10)).stream().toList(); | ||
assertEquals(4, hitsFile.size()); | ||
assertEquals(newFile2, hitsFile.get(0)); | ||
assertEquals(bNewFile, hitsFile.get(1)); | ||
assertEquals(anewFile1, hitsFile.get(2)); | ||
assertEquals(testNewFile, hitsFile.get(3)); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the same thing here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here "filter" is just used as a name , not a type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done