Search in sources :

Example 1 with SearchResult

use of org.eclipse.che.api.vfs.search.SearchResult in project che by eclipse.

the class ProjectService method search.

@GET
@Path("/search/{path:.*}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Search for resources", notes = "Search for resources applying a number of search filters as query parameters", response = ItemReference.class, responseContainer = "List")
@ApiResponses({ @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 403, message = "User not authorized to call this operation"), @ApiResponse(code = 404, message = "Not found"), @ApiResponse(code = 409, message = "Conflict error"), @ApiResponse(code = 500, message = "Internal Server Error") })
public List<ItemReference> search(@ApiParam(value = "Path to resource, i.e. where to search?", required = true) @PathParam("path") String path, @ApiParam(value = "Resource name") @QueryParam("name") String name, @ApiParam(value = "Search keywords") @QueryParam("text") String text, @ApiParam(value = "Maximum items to display. If this parameter is dropped, there are no limits") @QueryParam("maxItems") @DefaultValue("-1") int maxItems, @ApiParam(value = "Skip count") @QueryParam("skipCount") int skipCount) throws NotFoundException, ForbiddenException, ConflictException, ServerException {
    final Searcher searcher;
    try {
        searcher = projectManager.getSearcher();
    } catch (NotFoundException e) {
        LOG.warn(e.getLocalizedMessage());
        return Collections.emptyList();
    }
    if (skipCount < 0) {
        throw new ConflictException(String.format("Invalid 'skipCount' parameter: %d.", skipCount));
    }
    final QueryExpression expr = new QueryExpression().setPath(path.startsWith("/") ? path : ('/' + path)).setName(name).setText(text).setMaxItems(maxItems).setSkipCount(skipCount);
    final SearchResult result = searcher.search(expr);
    final List<SearchResultEntry> searchResultEntries = result.getResults();
    final List<ItemReference> items = new ArrayList<>(searchResultEntries.size());
    final FolderEntry root = projectManager.getProjectsRoot();
    for (SearchResultEntry searchResultEntry : searchResultEntries) {
        final VirtualFileEntry child = root.getChild(searchResultEntry.getFilePath());
        if (child != null && child.isFile()) {
            items.add(injectFileLinks(asDto((FileEntry) child)));
        }
    }
    return items;
}
Also used : ItemReference(org.eclipse.che.api.project.shared.dto.ItemReference) ConflictException(org.eclipse.che.api.core.ConflictException) Searcher(org.eclipse.che.api.vfs.search.Searcher) ArrayList(java.util.ArrayList) NotFoundException(org.eclipse.che.api.core.NotFoundException) SearchResult(org.eclipse.che.api.vfs.search.SearchResult) QueryExpression(org.eclipse.che.api.vfs.search.QueryExpression) SearchResultEntry(org.eclipse.che.api.vfs.search.SearchResultEntry) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with SearchResult

use of org.eclipse.che.api.vfs.search.SearchResult in project che by eclipse.

the class FSLuceneSearcherTest method retrievesSearchResultWithPages.

@Test
public void retrievesSearchResultWithPages() throws Exception {
    VirtualFileSystem virtualFileSystem = virtualFileSystem();
    for (int i = 0; i < 100; i++) {
        virtualFileSystem.getRoot().createFile(String.format("file%02d", i), TEST_CONTENT[i % TEST_CONTENT.length]);
    }
    searcher.init(virtualFileSystem);
    SearchResult firstPage = searcher.search(new QueryExpression().setText("spaceflight").setMaxItems(8));
    assertEquals(8, firstPage.getFilePaths().size());
    QueryExpression nextPageQueryExpression = firstPage.getNextPageQueryExpression().get();
    nextPageQueryExpression.setMaxItems(100);
    SearchResult lastPage = searcher.search(nextPageQueryExpression);
    assertEquals(17, lastPage.getFilePaths().size());
    assertTrue(Collections.disjoint(firstPage.getFilePaths(), lastPage.getFilePaths()));
}
Also used : VirtualFileSystem(org.eclipse.che.api.vfs.VirtualFileSystem) MemoryVirtualFileSystem(org.eclipse.che.api.vfs.impl.memory.MemoryVirtualFileSystem) SearchResult(org.eclipse.che.api.vfs.search.SearchResult) QueryExpression(org.eclipse.che.api.vfs.search.QueryExpression) Test(org.testng.annotations.Test)

Example 3 with SearchResult

use of org.eclipse.che.api.vfs.search.SearchResult in project che by eclipse.

the class MemoryLuceneSearcherTest method generatesQueryExpressionForRetrievingNextPageOfResults.

@Test
public void generatesQueryExpressionForRetrievingNextPageOfResults() throws Exception {
    VirtualFileSystem virtualFileSystem = virtualFileSystem();
    for (int i = 0; i < 100; i++) {
        virtualFileSystem.getRoot().createFile(String.format("file%02d", i), TEST_CONTENT[i % TEST_CONTENT.length]);
    }
    searcher.init(virtualFileSystem);
    SearchResult result = searcher.search(new QueryExpression().setText("spaceflight").setMaxItems(7));
    assertEquals(25, result.getTotalHits());
    Optional<QueryExpression> optionalNextPageQueryExpression = result.getNextPageQueryExpression();
    assertTrue(optionalNextPageQueryExpression.isPresent());
    QueryExpression nextPageQueryExpression = optionalNextPageQueryExpression.get();
    assertEquals("spaceflight", nextPageQueryExpression.getText());
    assertEquals(7, nextPageQueryExpression.getSkipCount());
    assertEquals(7, nextPageQueryExpression.getMaxItems());
}
Also used : MemoryVirtualFileSystem(org.eclipse.che.api.vfs.impl.memory.MemoryVirtualFileSystem) VirtualFileSystem(org.eclipse.che.api.vfs.VirtualFileSystem) SearchResult(org.eclipse.che.api.vfs.search.SearchResult) QueryExpression(org.eclipse.che.api.vfs.search.QueryExpression) Test(org.junit.Test)

Example 4 with SearchResult

use of org.eclipse.che.api.vfs.search.SearchResult in project che by eclipse.

the class MemoryLuceneSearcherTest method limitsNumberOfSearchResultsWhenMaxItemIsSet.

@Test
public void limitsNumberOfSearchResultsWhenMaxItemIsSet() throws Exception {
    VirtualFileSystem virtualFileSystem = virtualFileSystem();
    for (int i = 0; i < 100; i++) {
        virtualFileSystem.getRoot().createFile(String.format("file%02d", i), TEST_CONTENT[i % TEST_CONTENT.length]);
    }
    searcher.init(virtualFileSystem);
    SearchResult result = searcher.search(new QueryExpression().setText("mission").setMaxItems(5));
    assertEquals(25, result.getTotalHits());
    assertEquals(5, result.getFilePaths().size());
}
Also used : MemoryVirtualFileSystem(org.eclipse.che.api.vfs.impl.memory.MemoryVirtualFileSystem) VirtualFileSystem(org.eclipse.che.api.vfs.VirtualFileSystem) SearchResult(org.eclipse.che.api.vfs.search.SearchResult) QueryExpression(org.eclipse.che.api.vfs.search.QueryExpression) Test(org.junit.Test)

Example 5 with SearchResult

use of org.eclipse.che.api.vfs.search.SearchResult in project che by eclipse.

the class MemoryLuceneSearcherTest method retrievesSearchResultWithPages.

@Test
public void retrievesSearchResultWithPages() throws Exception {
    VirtualFileSystem virtualFileSystem = virtualFileSystem();
    for (int i = 0; i < 100; i++) {
        virtualFileSystem.getRoot().createFile(String.format("file%02d", i), TEST_CONTENT[i % TEST_CONTENT.length]);
    }
    searcher.init(virtualFileSystem);
    SearchResult firstPage = searcher.search(new QueryExpression().setText("spaceflight").setMaxItems(8));
    assertEquals(8, firstPage.getFilePaths().size());
    QueryExpression nextPageQueryExpression = firstPage.getNextPageQueryExpression().get();
    nextPageQueryExpression.setMaxItems(100);
    SearchResult lastPage = searcher.search(nextPageQueryExpression);
    assertEquals(17, lastPage.getFilePaths().size());
    assertTrue(Collections.disjoint(firstPage.getFilePaths(), lastPage.getFilePaths()));
}
Also used : MemoryVirtualFileSystem(org.eclipse.che.api.vfs.impl.memory.MemoryVirtualFileSystem) VirtualFileSystem(org.eclipse.che.api.vfs.VirtualFileSystem) SearchResult(org.eclipse.che.api.vfs.search.SearchResult) QueryExpression(org.eclipse.che.api.vfs.search.QueryExpression) Test(org.junit.Test)

Aggregations

QueryExpression (org.eclipse.che.api.vfs.search.QueryExpression)7 SearchResult (org.eclipse.che.api.vfs.search.SearchResult)7 VirtualFileSystem (org.eclipse.che.api.vfs.VirtualFileSystem)6 MemoryVirtualFileSystem (org.eclipse.che.api.vfs.impl.memory.MemoryVirtualFileSystem)6 Test (org.junit.Test)3 Test (org.testng.annotations.Test)3 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 ArrayList (java.util.ArrayList)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 ConflictException (org.eclipse.che.api.core.ConflictException)1 NotFoundException (org.eclipse.che.api.core.NotFoundException)1 ItemReference (org.eclipse.che.api.project.shared.dto.ItemReference)1 SearchResultEntry (org.eclipse.che.api.vfs.search.SearchResultEntry)1 Searcher (org.eclipse.che.api.vfs.search.Searcher)1