Search in sources :

Example 21 with BatchSearch

use of org.icij.datashare.batch.BatchSearch in project datashare by ICIJ.

the class BatchSearchResource method getResultAsCsv.

/**
 * Retrieve the results of a batch search as a CSV file.
 *
 * The search request is by default all results of the batch search.
 *
 * @param batchId
 * @return 200 and the CSV file as attached file
 *
 * Example :
 * $(curl -i localhost:8080/api/batch/search/result/csv/f74432db-9ae8-401d-977c-5c44a124f2c8)
 */
@Get("/search/result/csv/:batchid")
public Payload getResultAsCsv(String batchId, Context context) {
    StringBuilder builder = new StringBuilder("\"query\", \"documentUrl\", \"documentId\",\"rootId\",\"contentType\",\"contentLength\",\"documentPath\",\"creationDate\",\"documentNumber\"\n");
    BatchSearch batchSearch = batchSearchRepository.get((User) context.currentUser(), batchId);
    String url = propertiesProvider.get("rootHost").orElse(context.header("Host"));
    getResultsOrThrowUnauthorized(batchId, (User) context.currentUser(), new BatchSearchRepository.WebQuery()).forEach(result -> builder.append("\"").append(result.query).append("\"").append(",").append("\"").append(docUrl(url, batchSearch.project, result.documentId, result.rootId)).append("\"").append(",").append("\"").append(result.documentId).append("\"").append(",").append("\"").append(result.rootId).append("\"").append(",").append("\"").append(result.contentType).append("\"").append(",").append("\"").append(result.contentLength).append("\"").append(",").append("\"").append(result.documentPath).append("\"").append(",").append("\"").append(result.creationDate).append("\"").append(",").append("\"").append(result.documentNumber).append("\"").append("\n"));
    return new Payload("text/csv", builder.toString()).withHeader("Content-Disposition", "attachment;filename=\"" + batchId + ".csv\"");
}
Also used : User(org.icij.datashare.user.User) DatashareUser(org.icij.datashare.session.DatashareUser) BatchSearch(org.icij.datashare.batch.BatchSearch) Payload(net.codestory.http.payload.Payload)

Example 22 with BatchSearch

use of org.icij.datashare.batch.BatchSearch in project datashare by ICIJ.

the class BatchSearchResourceTest method test_upload_batch_search_csv_with_all_parameters.

@Test
public void test_upload_batch_search_csv_with_all_parameters() {
    when(batchSearchRepository.save(any())).thenReturn(true);
    Response response = postRaw("/api/batch/search/prj", "multipart/form-data;boundary=AaB03x", new MultipartContentBuilder("AaB03x").addField("name", "my batch search").addField("description", "search description").addFile(new FileUpload("csvFile").withFilename("search.csv").withContentType("text/csv").withContent("query one\n" + "query two\r\n" + "query three\r\n")).addField("published", String.valueOf(true)).addField("fileTypes", "application/pdf").addField("fileTypes", "image/jpeg").addField("paths", "/path/to/document").addField("paths", "/other/path/").addField("fuzziness", String.valueOf(4)).addField("phrase_matches", String.valueOf(true)).build()).response();
    assertThat(response.code()).isEqualTo(200);
    ArgumentCaptor<BatchSearch> argument = ArgumentCaptor.forClass(BatchSearch.class);
    verify(batchSearchRepository).save(argument.capture());
    assertThat(argument.getValue().published).isTrue();
    assertThat(argument.getValue().fileTypes).containsExactly("application/pdf", "image/jpeg");
    assertThat(argument.getValue().paths).containsExactly("/path/to/document", "/other/path/");
    assertThat(argument.getValue().fuzziness).isEqualTo(4);
    assertThat(argument.getValue().phraseMatches).isTrue();
    assertThat(argument.getValue().user).isEqualTo(User.local());
    assertThat(argument.getValue().description).isEqualTo("search description");
    Iterator<String> iterator = argument.getValue().queries.keySet().iterator();
    assertThat(iterator.next()).isEqualTo("query one");
    assertThat(iterator.next()).isEqualTo("query two");
    assertThat(iterator.next()).isEqualTo("query three");
    assertThat(iterator.hasNext()).isFalse();
}
Also used : Response(net.codestory.rest.Response) BatchSearch(org.icij.datashare.batch.BatchSearch) AbstractProdWebServerTest(org.icij.datashare.web.testhelpers.AbstractProdWebServerTest) Test(org.junit.Test)

Example 23 with BatchSearch

use of org.icij.datashare.batch.BatchSearch in project datashare by ICIJ.

the class BatchSearchResourceTest method test_get_search_results_csv_with_url_prefix_parameter.

@Test
public void test_get_search_results_csv_with_url_prefix_parameter() {
    server.configure(routes -> {
        PropertiesProvider propertiesProvider = new PropertiesProvider(new HashMap<String, String>() {

            {
                put("rootHost", "http://foo.com:12345");
            }
        });
        routes.add(new BatchSearchResource(batchSearchRepository, batchSearchQueue, propertiesProvider)).filter(new LocalUserFilter(propertiesProvider));
    });
    when(batchSearchRepository.get(User.local(), "batchSearchId")).thenReturn(new BatchSearch(project("prj"), "name", "desc", asSet("q"), User.local()));
    when(batchSearchRepository.getResults(User.local(), "batchSearchId", new BatchSearchRepository.WebQuery())).thenReturn(singletonList(new SearchResult("q", "docId", "rootId", Paths.get("/path/to/doc"), new Date(), "content/type", 123L, 1)));
    get("/api/batch/search/result/csv/batchSearchId").should().respond(200).haveType("text/csv").haveHeader("Content-Disposition", "attachment;filename=\"batchSearchId.csv\"").contain("\"http://foo.com:12345/#/d/prj/docId/rootId\",\"docId\",\"rootId\"");
}
Also used : PropertiesProvider(org.icij.datashare.PropertiesProvider) LocalUserFilter(org.icij.datashare.session.LocalUserFilter) BatchSearch(org.icij.datashare.batch.BatchSearch) JooqBatchSearchRepository(org.icij.datashare.db.JooqBatchSearchRepository) BatchSearchRepository(org.icij.datashare.batch.BatchSearchRepository) SearchResult(org.icij.datashare.batch.SearchResult) AbstractProdWebServerTest(org.icij.datashare.web.testhelpers.AbstractProdWebServerTest) Test(org.junit.Test)

Example 24 with BatchSearch

use of org.icij.datashare.batch.BatchSearch in project datashare by ICIJ.

the class BatchSearchResourceTest method test_get_batch_search.

@Test
public void test_get_batch_search() {
    BatchSearch search1 = new BatchSearch(project("prj"), "name1", "description1", asSet("query 1", "query 2"), User.local());
    BatchSearch search2 = new BatchSearch(project("prj"), "name2", "description2", asSet("query 3", "query 4"), User.local());
    when(batchSearchRepository.get(User.local(), search1.uuid)).thenReturn(search1);
    when(batchSearchRepository.get(User.local(), search2.uuid)).thenReturn(search2);
    get("/api/batch/search/" + search1.uuid).should().respond(200).haveType("application/json").contain("\"name\":\"name1\"");
    get("/api/batch/search/" + search2.uuid).should().respond(200).haveType("application/json").contain("\"name\":\"name2\"");
}
Also used : BatchSearch(org.icij.datashare.batch.BatchSearch) AbstractProdWebServerTest(org.icij.datashare.web.testhelpers.AbstractProdWebServerTest) Test(org.junit.Test)

Example 25 with BatchSearch

use of org.icij.datashare.batch.BatchSearch in project datashare by ICIJ.

the class BatchSearchRunnerIntTest method test_search_with_paths_ok.

@Test
public void test_search_with_paths_ok() throws Exception {
    Document mydoc = createDoc("mydoc").build();
    indexer.add(TEST_INDEX, mydoc);
    BatchSearch searchOk = new BatchSearch(project(TEST_INDEX), "name", "desc", asSet("mydoc"), User.local(), false, null, singletonList("/path/to"), 0);
    new BatchSearchRunner(indexer, new PropertiesProvider(), searchOk, resultConsumer).call();
    verify(resultConsumer).apply(searchOk.uuid, "mydoc", singletonList(mydoc));
}
Also used : PropertiesProvider(org.icij.datashare.PropertiesProvider) BatchSearch(org.icij.datashare.batch.BatchSearch) Document(org.icij.datashare.text.Document)

Aggregations

BatchSearch (org.icij.datashare.batch.BatchSearch)32 PropertiesProvider (org.icij.datashare.PropertiesProvider)18 Test (org.junit.Test)17 Document (org.icij.datashare.text.Document)15 AbstractProdWebServerTest (org.icij.datashare.web.testhelpers.AbstractProdWebServerTest)9 Date (java.util.Date)6 Response (net.codestory.rest.Response)4 BatchSearchRepository (org.icij.datashare.batch.BatchSearchRepository)4 SearchException (org.icij.datashare.batch.SearchException)4 JooqBatchSearchRepository (org.icij.datashare.db.JooqBatchSearchRepository)4 User (org.icij.datashare.user.User)4 CollectionUtils.asSet (org.icij.datashare.CollectionUtils.asSet)3 SearchResult (org.icij.datashare.batch.SearchResult)3 Project.project (org.icij.datashare.text.Project.project)3 HashMap (java.util.HashMap)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 NotFoundException (net.codestory.http.errors.NotFoundException)2