Search in sources :

Example 1 with Hit

use of org.sagebionetworks.repo.model.search.Hit in project Synapse-Repository-Services by Sage-Bionetworks.

the class SearchServiceImpl method addReturnDataToHits.

/**
 * Add extra return results to the hit list.
 * @param hits
 * @param includePath
 */
public void addReturnDataToHits(List<Hit> hits, boolean includePath) {
    List<Hit> toRemove = new LinkedList<Hit>();
    if (hits != null) {
        // For each hit we need to add the path
        for (Hit hit : hits) {
            if (includePath) {
                try {
                    EntityPath path = searchDocumentDriver.getEntityPath(hit.getId());
                    hit.setPath(path);
                } catch (NotFoundException e) {
                    // Add a warning and remove it from the hits
                    log.warn("Found a search document that did not exist in the reposiroty: " + hit, e);
                    // We need to remove this from the hits
                    toRemove.add(hit);
                }
            }
        }
    }
    if (!toRemove.isEmpty()) {
        hits.removeAll(toRemove);
    }
}
Also used : Hit(org.sagebionetworks.repo.model.search.Hit) EntityPath(org.sagebionetworks.repo.model.EntityPath) NotFoundException(org.sagebionetworks.repo.web.NotFoundException) LinkedList(java.util.LinkedList)

Example 2 with Hit

use of org.sagebionetworks.repo.model.search.Hit in project Synapse-Repository-Services by Sage-Bionetworks.

the class SearchControllerTest method testSearch.

@Test
public void testSearch() throws Exception {
    // Build a simple query.
    SearchQuery query = new SearchQuery();
    query.setBooleanQuery(new LinkedList<KeyValue>());
    KeyValue kv = new KeyValue();
    kv.setKey(SearchConstants.FIELD_ID);
    kv.setValue(project.getId());
    query.getBooleanQuery().add(kv);
    // the mock request
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setMethod("POST");
    request.addHeader("Accept", "application/json");
    request.addHeader("Content-Type", "application/json");
    request.setRequestURI("/search");
    request.setParameter(AuthorizationConstants.USER_ID_PARAM, TestUserDAO.TEST_USER_NAME);
    request.setContent(EntityFactory.createJSONStringForEntity(query).getBytes("UTF-8"));
    DispatchServletSingleton.getInstance().service(request, response);
    if (response.getStatus() != HttpStatus.CREATED.value()) {
        throw new RuntimeException(response.getContentAsString());
    }
    SearchResults results = EntityFactory.createEntityFromJSONString(response.getContentAsString(), SearchResults.class);
    assertNotNull(results);
    assertNotNull(results.getHits());
    assertEquals(1l, results.getHits().size());
    Hit hit = results.getHits().get(0);
    assertNotNull(hit);
    assertEquals(project.getId(), hit.getId());
}
Also used : SearchQuery(org.sagebionetworks.repo.model.search.query.SearchQuery) Hit(org.sagebionetworks.repo.model.search.Hit) KeyValue(org.sagebionetworks.repo.model.search.query.KeyValue) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SearchResults(org.sagebionetworks.repo.model.search.SearchResults) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 3 with Hit

use of org.sagebionetworks.repo.model.search.Hit in project Synapse-Repository-Services by Sage-Bionetworks.

the class IT510SynapseJavaClientSearchTest method testAllReturnFields.

/**
 * @throws Exception
 */
@Test
public void testAllReturnFields() throws Exception {
    SearchQuery searchQuery = new SearchQuery();
    List<String> queryTerms = new ArrayList<String>();
    // Lookup the first data object by its id.
    queryTerms.add(dataList.get(0).getId());
    searchQuery.setQueryTerm(queryTerms);
    List<String> returnFields = new ArrayList<String>();
    returnFields.add("id");
    returnFields.add("name");
    returnFields.add("path");
    returnFields.add("description");
    returnFields.add("etag");
    returnFields.add("modified_on");
    returnFields.add("created_on");
    returnFields.add("num_samples");
    returnFields.add("created_by_r");
    returnFields.add("modified_by_r");
    returnFields.add("node_type_r");
    returnFields.add("disease_r");
    returnFields.add("tissue_r");
    searchQuery.setReturnFields(returnFields);
    SearchResults results = synapse.search(searchQuery);
    assertTrue(new Long(0) < results.getFound());
    Hit hit = results.getHits().get(0);
    assertNotNull(hit.getId());
    assertNotNull(hit.getName());
    assertNotNull(hit.getPath());
    assertNotNull(hit.getDescription());
    assertNotNull(hit.getEtag());
    assertNotNull(hit.getModified_on());
    assertNotNull(hit.getCreated_on());
    assertNotNull(hit.getNum_samples());
    assertNotNull(hit.getCreated_by());
    assertNotNull(hit.getModified_by());
    assertNotNull(hit.getNode_type());
    assertNotNull(hit.getDisease());
    assertNotNull(hit.getTissue());
}
Also used : SearchQuery(org.sagebionetworks.repo.model.search.query.SearchQuery) Hit(org.sagebionetworks.repo.model.search.Hit) ArrayList(java.util.ArrayList) SearchResults(org.sagebionetworks.repo.model.search.SearchResults) Test(org.junit.Test)

Example 4 with Hit

use of org.sagebionetworks.repo.model.search.Hit in project Synapse-Repository-Services by Sage-Bionetworks.

the class IT510SynapseJavaClientSearchTest method testDescription.

/**
 * @throws Exception
 */
@Test
public void testDescription() throws Exception {
    // When descriptions are not null, make sure they are not just an empty
    // json array (this was an old bug)
    SearchQuery searchQuery = new SearchQuery();
    List<String> returnFields = new ArrayList<String>();
    returnFields.add("description");
    searchQuery.setReturnFields(returnFields);
    KeyValue booleanQueryClause = new KeyValue();
    booleanQueryClause.setKey("node_type");
    booleanQueryClause.setValue("*");
    List<KeyValue> booleanQuery = new ArrayList<KeyValue>();
    booleanQuery.add(booleanQueryClause);
    searchQuery.setBooleanQuery(booleanQuery);
    SearchResults results = synapse.search(searchQuery);
    assertTrue(1 <= results.getFound());
    for (Hit hit : results.getHits()) {
        String description = hit.getDescription();
        if (null != description) {
            assertFalse("[]".equals(description));
        }
    }
}
Also used : SearchQuery(org.sagebionetworks.repo.model.search.query.SearchQuery) Hit(org.sagebionetworks.repo.model.search.Hit) KeyValue(org.sagebionetworks.repo.model.search.query.KeyValue) ArrayList(java.util.ArrayList) SearchResults(org.sagebionetworks.repo.model.search.SearchResults) Test(org.junit.Test)

Example 5 with Hit

use of org.sagebionetworks.repo.model.search.Hit in project Synapse-Repository-Services by Sage-Bionetworks.

the class SearchDaoImpl method deleteAllDocuments.

@Override
public void deleteAllDocuments() throws ClientProtocolException, IOException, HttpClientHelperException, InterruptedException {
    // Keep deleting as long as there are documents
    SearchResults sr = null;
    do {
        sr = listSearchDocuments(1000, 0);
        HashSet<String> idSet = new HashSet<String>();
        for (Hit hit : sr.getHits()) {
            idSet.add(hit.getId());
        }
        // Delete the whole set
        if (!idSet.isEmpty()) {
            log.warn("Deleting the following documents from the search index:" + idSet.toString());
            deleteDocuments(idSet);
            Thread.sleep(5000);
        }
    } while (sr.getFound() > 0);
}
Also used : Hit(org.sagebionetworks.repo.model.search.Hit) SearchResults(org.sagebionetworks.repo.model.search.SearchResults) HashSet(java.util.HashSet)

Aggregations

Hit (org.sagebionetworks.repo.model.search.Hit)8 SearchResults (org.sagebionetworks.repo.model.search.SearchResults)7 Test (org.junit.Test)6 SearchQuery (org.sagebionetworks.repo.model.search.query.SearchQuery)5 KeyValue (org.sagebionetworks.repo.model.search.query.KeyValue)4 ArrayList (java.util.ArrayList)2 EntityPath (org.sagebionetworks.repo.model.EntityPath)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Document (org.sagebionetworks.repo.model.search.Document)1 DocumentFields (org.sagebionetworks.repo.model.search.DocumentFields)1 NotFoundException (org.sagebionetworks.repo.web.NotFoundException)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1