@Test public void testGetTextPositions() throws IOException { String query = "comment"; String queryString = "http: boolean isW3c = true; String page = null; boolean isMixedSearch = false; long totalHits = 10; SearchHits searchHits = mock(SearchHits.class); when(searchHits.getTotalHits()).thenReturn(totalHits); SearchHit[] hits = new SearchHit[1]; SearchHit hit = mock(SearchHit.class); hits[0] = hit; when(hit.getId()).thenReturn("3"); Map <String, SearchHitField> hitMap = new HashMap<>(); List values = new ArrayList<>(); values.add("3"); SearchHitField shf = new SearchHitField("imageId", values); hitMap.put("imageId",shf); when(hit.getFields()).thenReturn(hitMap); when(searchHits.iterator()).thenReturn(Iterators.forArray(hits)); when(hit.getSourceAsString()).thenReturn(null); SearchResponse response = mock(SearchResponse.class); when(response.getHits()).thenReturn(searchHits); ListenableActionFuture<SearchResponse> action = mock(ListenableActionFuture.class); when(action.actionGet()).thenReturn(response); SearchRequestBuilder builder = mock(SearchRequestBuilder.class); when(builder.setQuery(anyObject())).thenReturn(builder); when(builder.setFrom(anyInt())).thenReturn(builder); when(builder.setSize(anyInt())).thenReturn(builder); when(builder.setFetchSource(anyBoolean())).thenReturn(builder); when(builder.execute()).thenReturn(action); when(client.prepareSearch(anyString())).thenReturn(builder); TermVectorsResponse tvResponse = new TermVectorsResponse("1","2","3"); tvResponse.setExists(true); writeStandardTermVector(tvResponse); MultiTermVectorsItemResponse mtviResponse = new MultiTermVectorsItemResponse(tvResponse, null); ActionFuture<MultiTermVectorsResponse> mtvAction = mock(ListenableActionFuture.class); when(client.multiTermVectors(anyObject())).thenReturn(mtvAction); MultiTermVectorsItemResponse[] itemResponseArray = new MultiTermVectorsItemResponse[]{mtviResponse}; MultiTermVectorsResponse mtvr = new MultiTermVectorsResponse(itemResponseArray); when(mtvAction.actionGet()).thenReturn(mtvr); String coordinates = getFileContents("coordinates.json"); when(coordinateService.getJsonPayload(anyString(), anyString())).thenReturn(coordinates); Cache.ValueWrapper obj = mock(Cache.ValueWrapper.class); Cache mockCache = mock(Cache.class); when(cacheManager.getCache("textSearchCache")).thenReturn(mockCache); when(mockCache.get(anyString())).thenReturn(null); ServiceResponse<Map<String, Object>> serviceResponse = textSearchServiceImpl.getTextPositions(query, queryString, isW3c, page, isMixedSearch, null, null); assertEquals(serviceResponse.getStatus(), Status.OK); when(coordinateService.getJsonPayload(anyString(), anyString())).thenReturn(null); serviceResponse = textSearchServiceImpl.getTextPositions(query, queryString, isW3c, page, isMixedSearch, null, null); assertEquals(serviceResponse.getStatus(), Status.NOT_FOUND); }
@Override @Transactional(readOnly = true, isolation = Isolation.SERIALIZABLE) public ServiceResponse<Map<String, Object>> getTextPositions(String query, String queryString, boolean isW3c, String page, boolean isMixedSearch, String within, String widthHeight) { totalHits = 0; String pageTest; int pageNumber = 1; Cache cache = cacheManager.getCache("textSearchCache"); if ("1".equals(page) || null == page) { pageTest = ""; } else { pageTest = page; pageNumber = Integer.parseInt(page); } String queryWithNoPageParamter = textUtils.removeParametersAutocompleteQuery(queryString, new String[] { "page" }); String queryWithAmendedPageParamter = queryWithNoPageParamter + pageTest; String queryWithPageParamter; Cache.ValueWrapper obj = cache.get(queryWithAmendedPageParamter); if (null != obj) { Map<String, Object> textMap = (Map) obj.get(); return new ServiceResponse<>(Status.OK, textMap); } else { if (pageNumber > 1) { Cache.ValueWrapper firstObj = cache.get(queryWithNoPageParamter); LOG.info("getting " + queryWithNoPageParamter + "from the cache"); boolean isInitialSearch = false; if (null == firstObj) { Map<String, Object> firstTextMap = getTextMap(query, queryWithNoPageParamter, isW3c, null, isMixedSearch, within, widthHeight); if (null != firstTextMap) { cache.put(queryWithNoPageParamter, firstTextMap); firstObj = cache.get(queryWithNoPageParamter); isInitialSearch = true; } } if (null != firstObj) { Map<String, Object> textMap = (Map) firstObj.get(); int[] totalElements = textUtils.tallyPagingParameters(textMap, isW3c, 0, 0); for (int y = 2; y <= pageNumber; y++) { Map<String, Object> otherTextMaps = getTextMap(query, queryString, isW3c, Integer.toString(y), isMixedSearch, within, widthHeight); if (null != otherTextMaps) { totalElements = textUtils.tallyPagingParameters(otherTextMaps, isW3c, totalElements[0], totalElements[1]); queryWithPageParamter = queryWithNoPageParamter + (y); cache.put(queryWithPageParamter, otherTextMaps); } else { LOG.error("No results for page " + y + " need to get back map with no resources plus numbers "); textUtils.removeResources(textMap, isW3c); queryWithPageParamter = queryWithNoPageParamter + (y); cache.put(queryWithPageParamter, textMap); return new ServiceResponse<>(Status.OK, textMap); } } obj = cache.get(queryWithAmendedPageParamter); if (null != obj) { Map<String, Object> requestedTextMap = (Map) obj.get(); return new ServiceResponse<>(Status.OK, requestedTextMap); } } else { LOG.error("Error with the cache - cannot create the first non paged search results"); } } else { Map<String, Object> textMap = getTextMap(query, queryString, isW3c, page, isMixedSearch, within, widthHeight); if (null != textMap) { cache.put(queryWithNoPageParamter, textMap); return new ServiceResponse<>(Status.OK, textMap); } else { return new ServiceResponse<>(Status.NOT_FOUND, null); } } } return new ServiceResponse<>(Status.NOT_FOUND, null); }
TextSearchServiceImpl implements TextSearchService { @Override @Transactional(readOnly = true, isolation = Isolation.SERIALIZABLE) public ServiceResponse<Map<String, Object>> getTextPositions(String query, String queryString, boolean isW3c, String page, boolean isMixedSearch, String within, String widthHeight) { totalHits = 0; String pageTest; int pageNumber = 1; Cache cache = cacheManager.getCache("textSearchCache"); if ("1".equals(page) || null == page) { pageTest = ""; } else { pageTest = page; pageNumber = Integer.parseInt(page); } String queryWithNoPageParamter = textUtils.removeParametersAutocompleteQuery(queryString, new String[] { "page" }); String queryWithAmendedPageParamter = queryWithNoPageParamter + pageTest; String queryWithPageParamter; Cache.ValueWrapper obj = cache.get(queryWithAmendedPageParamter); if (null != obj) { Map<String, Object> textMap = (Map) obj.get(); return new ServiceResponse<>(Status.OK, textMap); } else { if (pageNumber > 1) { Cache.ValueWrapper firstObj = cache.get(queryWithNoPageParamter); LOG.info("getting " + queryWithNoPageParamter + "from the cache"); boolean isInitialSearch = false; if (null == firstObj) { Map<String, Object> firstTextMap = getTextMap(query, queryWithNoPageParamter, isW3c, null, isMixedSearch, within, widthHeight); if (null != firstTextMap) { cache.put(queryWithNoPageParamter, firstTextMap); firstObj = cache.get(queryWithNoPageParamter); isInitialSearch = true; } } if (null != firstObj) { Map<String, Object> textMap = (Map) firstObj.get(); int[] totalElements = textUtils.tallyPagingParameters(textMap, isW3c, 0, 0); for (int y = 2; y <= pageNumber; y++) { Map<String, Object> otherTextMaps = getTextMap(query, queryString, isW3c, Integer.toString(y), isMixedSearch, within, widthHeight); if (null != otherTextMaps) { totalElements = textUtils.tallyPagingParameters(otherTextMaps, isW3c, totalElements[0], totalElements[1]); queryWithPageParamter = queryWithNoPageParamter + (y); cache.put(queryWithPageParamter, otherTextMaps); } else { LOG.error("No results for page " + y + " need to get back map with no resources plus numbers "); textUtils.removeResources(textMap, isW3c); queryWithPageParamter = queryWithNoPageParamter + (y); cache.put(queryWithPageParamter, textMap); return new ServiceResponse<>(Status.OK, textMap); } } obj = cache.get(queryWithAmendedPageParamter); if (null != obj) { Map<String, Object> requestedTextMap = (Map) obj.get(); return new ServiceResponse<>(Status.OK, requestedTextMap); } } else { LOG.error("Error with the cache - cannot create the first non paged search results"); } } else { Map<String, Object> textMap = getTextMap(query, queryString, isW3c, page, isMixedSearch, within, widthHeight); if (null != textMap) { cache.put(queryWithNoPageParamter, textMap); return new ServiceResponse<>(Status.OK, textMap); } else { return new ServiceResponse<>(Status.NOT_FOUND, null); } } } return new ServiceResponse<>(Status.NOT_FOUND, null); } }
TextSearchServiceImpl implements TextSearchService { @Override @Transactional(readOnly = true, isolation = Isolation.SERIALIZABLE) public ServiceResponse<Map<String, Object>> getTextPositions(String query, String queryString, boolean isW3c, String page, boolean isMixedSearch, String within, String widthHeight) { totalHits = 0; String pageTest; int pageNumber = 1; Cache cache = cacheManager.getCache("textSearchCache"); if ("1".equals(page) || null == page) { pageTest = ""; } else { pageTest = page; pageNumber = Integer.parseInt(page); } String queryWithNoPageParamter = textUtils.removeParametersAutocompleteQuery(queryString, new String[] { "page" }); String queryWithAmendedPageParamter = queryWithNoPageParamter + pageTest; String queryWithPageParamter; Cache.ValueWrapper obj = cache.get(queryWithAmendedPageParamter); if (null != obj) { Map<String, Object> textMap = (Map) obj.get(); return new ServiceResponse<>(Status.OK, textMap); } else { if (pageNumber > 1) { Cache.ValueWrapper firstObj = cache.get(queryWithNoPageParamter); LOG.info("getting " + queryWithNoPageParamter + "from the cache"); boolean isInitialSearch = false; if (null == firstObj) { Map<String, Object> firstTextMap = getTextMap(query, queryWithNoPageParamter, isW3c, null, isMixedSearch, within, widthHeight); if (null != firstTextMap) { cache.put(queryWithNoPageParamter, firstTextMap); firstObj = cache.get(queryWithNoPageParamter); isInitialSearch = true; } } if (null != firstObj) { Map<String, Object> textMap = (Map) firstObj.get(); int[] totalElements = textUtils.tallyPagingParameters(textMap, isW3c, 0, 0); for (int y = 2; y <= pageNumber; y++) { Map<String, Object> otherTextMaps = getTextMap(query, queryString, isW3c, Integer.toString(y), isMixedSearch, within, widthHeight); if (null != otherTextMaps) { totalElements = textUtils.tallyPagingParameters(otherTextMaps, isW3c, totalElements[0], totalElements[1]); queryWithPageParamter = queryWithNoPageParamter + (y); cache.put(queryWithPageParamter, otherTextMaps); } else { LOG.error("No results for page " + y + " need to get back map with no resources plus numbers "); textUtils.removeResources(textMap, isW3c); queryWithPageParamter = queryWithNoPageParamter + (y); cache.put(queryWithPageParamter, textMap); return new ServiceResponse<>(Status.OK, textMap); } } obj = cache.get(queryWithAmendedPageParamter); if (null != obj) { Map<String, Object> requestedTextMap = (Map) obj.get(); return new ServiceResponse<>(Status.OK, requestedTextMap); } } else { LOG.error("Error with the cache - cannot create the first non paged search results"); } } else { Map<String, Object> textMap = getTextMap(query, queryString, isW3c, page, isMixedSearch, within, widthHeight); if (null != textMap) { cache.put(queryWithNoPageParamter, textMap); return new ServiceResponse<>(Status.OK, textMap); } else { return new ServiceResponse<>(Status.NOT_FOUND, null); } } } return new ServiceResponse<>(Status.NOT_FOUND, null); } @Autowired  TextSearchServiceImpl(TextUtils textUtils, ElasticsearchTemplate template,
	    @Value("${text.server.coordinate.url}") String coordinateServerUrl, GetPayloadService coordinateService,
	    CacheManager cacheManager); }
TextSearchServiceImpl implements TextSearchService { @Override @Transactional(readOnly = true, isolation = Isolation.SERIALIZABLE) public ServiceResponse<Map<String, Object>> getTextPositions(String query, String queryString, boolean isW3c, String page, boolean isMixedSearch, String within, String widthHeight) { totalHits = 0; String pageTest; int pageNumber = 1; Cache cache = cacheManager.getCache("textSearchCache"); if ("1".equals(page) || null == page) { pageTest = ""; } else { pageTest = page; pageNumber = Integer.parseInt(page); } String queryWithNoPageParamter = textUtils.removeParametersAutocompleteQuery(queryString, new String[] { "page" }); String queryWithAmendedPageParamter = queryWithNoPageParamter + pageTest; String queryWithPageParamter; Cache.ValueWrapper obj = cache.get(queryWithAmendedPageParamter); if (null != obj) { Map<String, Object> textMap = (Map) obj.get(); return new ServiceResponse<>(Status.OK, textMap); } else { if (pageNumber > 1) { Cache.ValueWrapper firstObj = cache.get(queryWithNoPageParamter); LOG.info("getting " + queryWithNoPageParamter + "from the cache"); boolean isInitialSearch = false; if (null == firstObj) { Map<String, Object> firstTextMap = getTextMap(query, queryWithNoPageParamter, isW3c, null, isMixedSearch, within, widthHeight); if (null != firstTextMap) { cache.put(queryWithNoPageParamter, firstTextMap); firstObj = cache.get(queryWithNoPageParamter); isInitialSearch = true; } } if (null != firstObj) { Map<String, Object> textMap = (Map) firstObj.get(); int[] totalElements = textUtils.tallyPagingParameters(textMap, isW3c, 0, 0); for (int y = 2; y <= pageNumber; y++) { Map<String, Object> otherTextMaps = getTextMap(query, queryString, isW3c, Integer.toString(y), isMixedSearch, within, widthHeight); if (null != otherTextMaps) { totalElements = textUtils.tallyPagingParameters(otherTextMaps, isW3c, totalElements[0], totalElements[1]); queryWithPageParamter = queryWithNoPageParamter + (y); cache.put(queryWithPageParamter, otherTextMaps); } else { LOG.error("No results for page " + y + " need to get back map with no resources plus numbers "); textUtils.removeResources(textMap, isW3c); queryWithPageParamter = queryWithNoPageParamter + (y); cache.put(queryWithPageParamter, textMap); return new ServiceResponse<>(Status.OK, textMap); } } obj = cache.get(queryWithAmendedPageParamter); if (null != obj) { Map<String, Object> requestedTextMap = (Map) obj.get(); return new ServiceResponse<>(Status.OK, requestedTextMap); } } else { LOG.error("Error with the cache - cannot create the first non paged search results"); } } else { Map<String, Object> textMap = getTextMap(query, queryString, isW3c, page, isMixedSearch, within, widthHeight); if (null != textMap) { cache.put(queryWithNoPageParamter, textMap); return new ServiceResponse<>(Status.OK, textMap); } else { return new ServiceResponse<>(Status.NOT_FOUND, null); } } } return new ServiceResponse<>(Status.NOT_FOUND, null); } @Autowired  TextSearchServiceImpl(TextUtils textUtils, ElasticsearchTemplate template,
	    @Value("${text.server.coordinate.url}") String coordinateServerUrl, GetPayloadService coordinateService,
	    CacheManager cacheManager); @Override PageParameters getPageParameters(); @Override long getTotalHits(); @Override TextUtils getTextUtils(); @Override @Transactional(readOnly = true, isolation = Isolation.SERIALIZABLE) ServiceResponse<Map<String, Object>> getTextPositions(String query, String queryString, boolean isW3c,
	    String page, boolean isMixedSearch, String within, String widthHeight); }
TextSearchServiceImpl implements TextSearchService { @Override @Transactional(readOnly = true, isolation = Isolation.SERIALIZABLE) public ServiceResponse<Map<String, Object>> getTextPositions(String query, String queryString, boolean isW3c, String page, boolean isMixedSearch, String within, String widthHeight) { totalHits = 0; String pageTest; int pageNumber = 1; Cache cache = cacheManager.getCache("textSearchCache"); if ("1".equals(page) || null == page) { pageTest = ""; } else { pageTest = page; pageNumber = Integer.parseInt(page); } String queryWithNoPageParamter = textUtils.removeParametersAutocompleteQuery(queryString, new String[] { "page" }); String queryWithAmendedPageParamter = queryWithNoPageParamter + pageTest; String queryWithPageParamter; Cache.ValueWrapper obj = cache.get(queryWithAmendedPageParamter); if (null != obj) { Map<String, Object> textMap = (Map) obj.get(); return new ServiceResponse<>(Status.OK, textMap); } else { if (pageNumber > 1) { Cache.ValueWrapper firstObj = cache.get(queryWithNoPageParamter); LOG.info("getting " + queryWithNoPageParamter + "from the cache"); boolean isInitialSearch = false; if (null == firstObj) { Map<String, Object> firstTextMap = getTextMap(query, queryWithNoPageParamter, isW3c, null, isMixedSearch, within, widthHeight); if (null != firstTextMap) { cache.put(queryWithNoPageParamter, firstTextMap); firstObj = cache.get(queryWithNoPageParamter); isInitialSearch = true; } } if (null != firstObj) { Map<String, Object> textMap = (Map) firstObj.get(); int[] totalElements = textUtils.tallyPagingParameters(textMap, isW3c, 0, 0); for (int y = 2; y <= pageNumber; y++) { Map<String, Object> otherTextMaps = getTextMap(query, queryString, isW3c, Integer.toString(y), isMixedSearch, within, widthHeight); if (null != otherTextMaps) { totalElements = textUtils.tallyPagingParameters(otherTextMaps, isW3c, totalElements[0], totalElements[1]); queryWithPageParamter = queryWithNoPageParamter + (y); cache.put(queryWithPageParamter, otherTextMaps); } else { LOG.error("No results for page " + y + " need to get back map with no resources plus numbers "); textUtils.removeResources(textMap, isW3c); queryWithPageParamter = queryWithNoPageParamter + (y); cache.put(queryWithPageParamter, textMap); return new ServiceResponse<>(Status.OK, textMap); } } obj = cache.get(queryWithAmendedPageParamter); if (null != obj) { Map<String, Object> requestedTextMap = (Map) obj.get(); return new ServiceResponse<>(Status.OK, requestedTextMap); } } else { LOG.error("Error with the cache - cannot create the first non paged search results"); } } else { Map<String, Object> textMap = getTextMap(query, queryString, isW3c, page, isMixedSearch, within, widthHeight); if (null != textMap) { cache.put(queryWithNoPageParamter, textMap); return new ServiceResponse<>(Status.OK, textMap); } else { return new ServiceResponse<>(Status.NOT_FOUND, null); } } } return new ServiceResponse<>(Status.NOT_FOUND, null); } @Autowired  TextSearchServiceImpl(TextUtils textUtils, ElasticsearchTemplate template,
	    @Value("${text.server.coordinate.url}") String coordinateServerUrl, GetPayloadService coordinateService,
	    CacheManager cacheManager); @Override PageParameters getPageParameters(); @Override long getTotalHits(); @Override TextUtils getTextUtils(); @Override @Transactional(readOnly = true, isolation = Isolation.SERIALIZABLE) ServiceResponse<Map<String, Object>> getTextPositions(String query, String queryString, boolean isW3c,
	    String page, boolean isMixedSearch, String within, String widthHeight); static final String SERVICE_NAME; }