Uploaded image for project: 'PUBLIC - Liferay Portal Community Edition'
  1. PUBLIC - Liferay Portal Community Edition
  2. LPS-92045

SearchResponseBuilderImpl.SearchResponseImpl.getDocumentsStream() can generate NPE when there are no results

    Details

      Description

      1. Write a sample code (for example a new "search" method in EntryServiceImpl in the guestbook tutorial project) which uses SearchRequest, SearchResponse etc. new API
        	public List<Entry> search(long companyId) throws PortalException {
        		BooleanQuery booleanQuery = _queries.booleanQuery();
        
        		booleanQuery.addMustQueryClauses(
        			_queries.term(Field.COMPANY_ID, companyId));
        
        		SearchRequestBuilder searchRequestBuilder =
        			_searchRequestBuilderFactory.getSearchRequestBuilder(
        				new SearchContext());
        
        		SearchRequest searchRequest = searchRequestBuilder.entryClassNames(
        			Entry.class.getName()
        		).modelIndexerClasses(
        			Entry.class
        		).query(
        			booleanQuery
        		).withSearchContext(
        			searchContext -> searchContext.setCompanyId(companyId)
        		).build();
        
        		return _getEntries(searchResponse);
        	}
        
        	private List<Entry> _getEntries(SearchResponse searchResponse) {
        		Stream<Document> documentStream = searchResponse.getDocumentsStream();
        
        		return documentStream.map(
        			document -> document.getFieldValue(Field.ENTRY_CLASS_PK)
        		).map(
        			GetterUtil::getLong
        		).map(
        			entryLocalService::fetchEntry
        		).filter(
        			Objects::nonNull
        		).collect(
        			Collectors.toList()
        		);
        	}
        
        	@ServiceReference(type = Queries.class)
        	private Queries _queries;
        
        	@ServiceReference(type = Searcher.class)
        	private Searcher _searcher;
        
        	@ServiceReference(type = SearchRequestBuilderFactory.class)
                private SearchRequestBuilderFactory _searchRequestBuilderFactory;
        
      2. Deploy
      3. Invoke the API through http://localhost:8080/api/jsonws?contextName=gb&signature=%2Fgb.entry%2Fsearch-1-long#serviceResults
      4. Result: Empty response
      5. Remove
        		).modelIndexerClasses(
        			Entry.class
        
      6. Redeploy, invoke again
        Actual Result:
        2019-03-13 16:25:33.796 ERROR [http-nio-8080-exec-4][JSONWebServiceServiceAction:112] java.lang.NullPointerException
        

      due to the current impl of getDocumentsStream():

      		@Override
      		public Stream<Document> getDocumentsStream() {
      			SearchHits searchHits = getSearchHits();
      
      			List<SearchHit> list = searchHits.getSearchHits();
      
      			Stream<SearchHit> stream = list.stream();
      
      			return stream.map(SearchHit::getDocument);
      		}
      

      ==>> searchHits is null

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Days since last comment:
                  38 weeks, 1 day ago

                  Packages

                  Version Package
                  7.2.0 GA1