Java Code Examples for org.apache.solr.client.solrj.response.QueryResponse#getBeans()

The following examples show how to use org.apache.solr.client.solrj.response.QueryResponse#getBeans() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UsingSolrJRefGuideExamplesTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void queryBeanValueTypeExample() throws Exception {
  expectLine("Found 3 documents");
  expectLine("id: 1; name: Fitbit Alta");
  expectLine("id: 2; name: Sony Walkman");
  expectLine("id: 3; name: Garmin GPS");
  
  // tag::solrj-query-bean-value-type[]
  final SolrClient client = getSolrClient();

  final SolrQuery query = new SolrQuery("*:*");
  query.addField("id");
  query.addField("name");
  query.setSort("id", ORDER.asc);

  final QueryResponse response = client.query("techproducts", query);
  final List<TechProduct> products = response.getBeans(TechProduct.class);
  // end::solrj-query-bean-value-type[]

  print("Found " + products.size() + " documents");
  for (TechProduct product : products) {
    print("id: " + product.id + "; name: " + product.name);
  }
}
 
Example 2
Source File: SolrDocumentSearch.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@Override
public ResultsList<Document> searchByEfoUri(int start, int rows, String term, String... uris) throws SearchEngineException {
	ResultsList<Document> results;

	try {
		SolrQuery query = new SolrQuery(term + " OR " + EFO_URI_FIELD + ":" + buildUriFilter(uris));
		// query.addFilterQuery(EFO_URI_FIELD + ":" + buildUriFilter(uris));
		query.setStart(start);
		query.setRows(rows);
		query.setRequestHandler(config.getDocumentUriRequestHandler());

		LOGGER.debug("Solr query: {}", query);

		QueryResponse response = server.query(query);
		List<Document> docs = response.getBeans(Document.class);
		results = new ResultsList<>(docs, start, (start / rows), response.getResults().getNumFound());
	} catch (SolrServerException | IOException e) {
		throw new SearchEngineException(e);
	}

	return results;
}
 
Example 3
Source File: SolrOntologySearch.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@Override
public OntologyEntryBean findOntologyEntryByUri(String uri) throws SearchEngineException {
	OntologyEntryBean retVal = null;
	
	try {
		SolrQuery query = new SolrQuery(uri);
		query.setRequestHandler(config.getOntologyNodeRequestHandler());
		
		QueryResponse response = server.query(query);
		List<OntologyEntryBean> annotations = response.getBeans(OntologyEntryBean.class);
		if (annotations.size() > 0) {
			retVal = annotations.get(0);
		}
	} catch (SolrServerException e) {
		throw new SearchEngineException(e);
	}
	
	return retVal;
}
 
Example 4
Source File: ItemSearchServiceLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenSearchingWithFilterQuery_thenAllMatchingItemsShouldAvialble() throws Exception {
    itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f);
    itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f);
    itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "Home Appliances", 200f);
    itemSearchService.index("hm0004", "Brand2 Dishwasher", "Washing tools and equipment ", 250f);

    SolrQuery query = new SolrQuery();
    query.setQuery("price:[100 TO 300]");
    query.addFilterQuery("description:Brand1", "category:Home Appliances");

    QueryResponse response = solrClient.query(query);
    List<Item> items = response.getBeans(Item.class);

    assertEquals(2, items.size());
}
 
Example 5
Source File: ItemSearchServiceLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenSearchingByBasicQuery_thenAllMatchingItemsShouldAvialble() throws Exception {
    itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f);
    itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f);
    itemSearchService.index("hm0003", "LED TV 32", "Brand1 Home Appliances", 450f);

    SolrQuery query = new SolrQuery();
    query.setQuery("brand1");
    query.setStart(0);
    query.setRows(10);

    QueryResponse response = solrClient.query(query);
    List<Item> items = response.getBeans(Item.class);

    assertEquals(3, items.size());

}
 
Example 6
Source File: ItemSearchServiceLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenSearchingWithPriceRangeInclusiveExclusive_thenAllMatchingItemsShouldAvialble() throws Exception {
    itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f);
    itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f);
    itemSearchService.index("hm0003", "Brand2 Dishwasher", "Home Appliances", 200f);
    itemSearchService.index("hm0004", "Brand2 Dishwasher", "Washing tools and equipment ", 450f);

    SolrQuery query = new SolrQuery();
    query.setQuery("price:{100 TO 300]");

    QueryResponse response = solrClient.query(query);
    List<Item> items = response.getBeans(Item.class);

    assertEquals(2, items.size());
}
 
Example 7
Source File: MavenCentralSearchApiServiceImpl.java    From artifact-listener with Apache License 2.0 5 votes vote down vote up
@Override
public List<ArtifactBean> getArtifacts(String global, String groupId, String artifactId, int offset, int maxRows) throws ServiceException {
	String query = getSearchArtifactsQuery(global, groupId, artifactId);
	if (!StringUtils.hasText(query)) {
		return Lists.newArrayList();
	}
	SolrQuery solrQuery = new SolrQuery(query);

	QueryResponse response = query(solrQuery, offset, maxRows);
	return response.getBeans(ArtifactBean.class);
}
 
Example 8
Source File: ItemSearchServiceLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenSearchingPhraseWithProximity_thenAllMatchingItemsShouldAvialble() throws Exception {
    itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f);
    itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f);
    itemSearchService.index("hm0003", "Brand2 Dishwasher", "Washing tools and equipment ", 450f);

    SolrQuery query = new SolrQuery();
    query.setQuery("\"Washing equipment\"~2");

    QueryResponse response = solrClient.query(query);
    List<Item> items = response.getBeans(Item.class);

    assertEquals(1, items.size());
}
 
Example 9
Source File: ItemSearchServiceLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenSearchingWithRealPhrase_thenAllMatchingItemsShouldAvialble() throws Exception {
    itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f);
    itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f);
    itemSearchService.index("hm0003", "Brand2 Dishwasher", "Washing tools and equipment ", 450f);

    SolrQuery query = new SolrQuery();
    query.setQuery("\"washing machine\"");

    QueryResponse response = solrClient.query(query);
    List<Item> items = response.getBeans(Item.class);

    assertEquals(1, items.size());
}
 
Example 10
Source File: ItemSearchServiceLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenSearchingWithPhrase_thenAllMatchingItemsShouldAvialble() throws Exception {
    itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f);
    itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f);
    itemSearchService.index("hm0003", "Brand2 Dishwasher", "Washing tools and equipment ", 450f);

    SolrQuery query = new SolrQuery();
    query.setQuery("washing MachIne");

    QueryResponse response = solrClient.query(query);
    List<Item> items = response.getBeans(Item.class);

    assertEquals(2, items.size());
}
 
Example 11
Source File: ItemSearchServiceLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenSearchingWithFields_thenAllMatchingItemsShouldAvialble() throws Exception {
    itemSearchService.index("0001", "Brand1 Washing Machine", "Home Appliances", 450f);
    itemSearchService.index("0002", "Brand1 Refrigerator", "Home Appliances", 450f);
    itemSearchService.index("0003", "Brand2 LED TV 32", "Brand1 Washing Home Appliances", 450f);

    SolrQuery query = new SolrQuery();
    query.setQuery("description:Brand* AND category:*Washing*");

    QueryResponse response = solrClient.query(query);
    List<Item> items = response.getBeans(Item.class);

    assertEquals(1, items.size());
}
 
Example 12
Source File: ItemSearchServiceLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenSearchingWithLogicalOperators_thenAllMatchingItemsShouldAvialble() throws Exception {
    itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f);
    itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f);
    itemSearchService.index("hm0003", "Brand2 LED TV 32", "Washing Appliances", 450f);

    SolrQuery query = new SolrQuery();
    query.setQuery("brand1 AND (Washing OR Refrigerator)");

    QueryResponse response = solrClient.query(query);
    List<Item> items = response.getBeans(Item.class);

    assertEquals(2, items.size());
}
 
Example 13
Source File: ItemSearchServiceLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenSearchingWithWildCard_thenAllMatchingItemsShouldAvialble() throws Exception {
    itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f);
    itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f);
    itemSearchService.index("hm0003", "LED TV 32", "Brand1 Home Appliances", 450f);

    SolrQuery query = new SolrQuery();
    query.setQuery("*rand?");

    QueryResponse response = solrClient.query(query);
    List<Item> items = response.getBeans(Item.class);

    assertEquals(3, items.size());
}
 
Example 14
Source File: ServiceLogsManager.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
private SolrServiceLogData getNextHitForKeyword(ServiceLogRequest request, String keyword, boolean isNext, String event, boolean timeAscending, String nextOrPreviousPageDate) {
  if (hasNextOrAscOrder(isNext, timeAscending)) {
    request.setTo(nextOrPreviousPageDate);
  } else {
    request.setFrom(nextOrPreviousPageDate);
  }
  SimpleQuery keywordNextQuery = conversionService.convert(request, SimpleQuery.class);
  keywordNextQuery.addFilterQuery(new SimpleFilterQuery(new Criteria(KEY_LOG_MESSAGE).contains(keyword)));
  keywordNextQuery.setRows(1);
  SolrQuery kewordNextSolrQuery = new DefaultQueryParser().doConstructSolrQuery(keywordNextQuery);
  kewordNextSolrQuery.setStart(0);
  if (hasNextOrAscOrder(isNext, timeAscending)) {
    kewordNextSolrQuery.setSort(LOGTIME, SolrQuery.ORDER.desc);
  } else {
    kewordNextSolrQuery.setSort(LOGTIME, SolrQuery.ORDER.asc);
  }
  kewordNextSolrQuery.addSort(SEQUENCE_ID, SolrQuery.ORDER.desc);
  QueryResponse queryResponse = serviceLogsSolrDao.process(kewordNextSolrQuery, event);
  if (queryResponse == null) {
    throw new NotFoundException(String.format("The keyword \"%s\" was not found", keyword));
  }
  List<SolrServiceLogData> solrServiceLogDataList = queryResponse.getBeans(SolrServiceLogData.class);
  if (!CollectionUtils.isNotEmpty(solrServiceLogDataList)) {
    throw new NotFoundException(String.format("The keyword \"%s\" was not found", keyword));
  }
  return solrServiceLogDataList.get(0);
}
 
Example 15
Source File: MavenCentralSearchApiServiceImpl.java    From artifact-listener with Apache License 2.0 5 votes vote down vote up
private List<ArtifactVersionBean> getArtifactVersions(String groupId, String artifactId, int offset, int maxRows) throws ServiceException {
	if (!StringUtils.hasText(groupId) || ! StringUtils.hasText(artifactId)) {
		return Lists.newArrayListWithCapacity(0);
	}
	
	BooleanQuery bq = new BooleanQuery();
	bq.add(new TermQuery(new Term(MavenCentralSearchApiConstants.GROUP_FIELD, groupId)), Occur.MUST);
	bq.add(new TermQuery(new Term(MavenCentralSearchApiConstants.ARTIFACT_FIELD, artifactId)), Occur.MUST);
	
	SolrQuery solrQuery = new SolrQuery(LuceneUtils.queryToString(bq));
	solrQuery.set(CoreAdminParams.CORE, MavenCentralSearchApiConstants.CORE_GAV);

	QueryResponse response = query(solrQuery, offset, maxRows);
	return response.getBeans(ArtifactVersionBean.class);
}
 
Example 16
Source File: CustomSolrRepositoryImpl.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public PageProductResponse findProductsByCustom(Product product) {
	SolrQuery sq = new SolrQuery();

	sq.set("q", "goods_name:黄瓜花");
	sq.set("fl", "stores_name", "id", "goods_name");

	QueryResponse queryResponse = null;// solrTemplate.querySolrByCustomDefine(sq);
	List<Product> productList = queryResponse.getBeans(Product.class);
	PageProductResponse pageModel = new PageProductResponse();
	pageModel.setTotalPages(queryResponse.getResults().getNumFound());
	return pageModel;
}
 
Example 17
Source File: ServiceLogsManager.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
private <T extends LogData> List<T> getComponentBeans(Class<T> clazz, QueryResponse response) {
  if (clazz.isAssignableFrom(SolrHostLogData.class) || clazz.isAssignableFrom(SolrComponentTypeLogData.class)) {
    return response.getBeans(clazz);
  } else {
    throw new UnsupportedOperationException();
  }
}
 
Example 18
Source File: SolrDocumentSearch.java    From BioSolr with Apache License 2.0 4 votes vote down vote up
@Override
public ResultsList<Document> searchDocuments(String term, int start, int rows, List<String> additionalFields,
		List<String> filters) throws SearchEngineException {
	ResultsList<Document> results;

	try {
		SolrQuery query = new SolrQuery(term);
		query.setStart(start);
		query.setRows(rows);
		query.setRequestHandler(config.getDocumentRequestHandler());
		List<String> queryFields = new ArrayList<>(DEFAULT_SEARCH_FIELDS);
		if (additionalFields != null) {
			queryFields.addAll(additionalFields);
		}
		if (filters != null) {
			query.addFilterQuery(filters.toArray(new String[filters.size()]));
		}
		query.setParam(DisMaxParams.QF, queryFields.toArray(new String[queryFields.size()]));
		
		LOGGER.debug("Query: {}", query);

		QueryResponse response = server.query(query);
		List<Document> docs;
		long total = 0;
		
		if (response.getGroupResponse() != null) {
			docs = new ArrayList<>(rows);
			GroupResponse gResponse = response.getGroupResponse();
			for (GroupCommand gCommand : gResponse.getValues()) {
				total += gCommand.getNGroups();
				for (Group group : gCommand.getValues()) {
					docs.addAll(server.getBinder().getBeans(Document.class, group.getResult()));
				}
			}
		} else if (response.getResults().getNumFound() == 0) {
			docs = new ArrayList<>();
		} else {
			docs = response.getBeans(Document.class);
			total = response.getResults().getNumFound();
		}
		
		results = new ResultsList<>(docs, start, (start / rows), total);
	} catch (SolrServerException | IOException e) {
		throw new SearchEngineException(e);
	}

	return results;
}
 
Example 19
Source File: AuditLogsManager.java    From ambari-logsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected List<AuditLogData> convertToSolrBeans(QueryResponse response) {
  return new ArrayList<>(response.getBeans(SolrAuditLogData.class));
}
 
Example 20
Source File: ServiceLogsManager.java    From ambari-logsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected List<ServiceLogData> convertToSolrBeans(QueryResponse response) {
  return new ArrayList<>(response.getBeans(SolrServiceLogData.class));
}