Java Code Examples for org.apache.solr.client.solrj.SolrQuery#addTermsField()

The following examples show how to use org.apache.solr.client.solrj.SolrQuery#addTermsField() . 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: TermsResponseTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testTermsResponse() throws Exception {
  SolrInputDocument doc = new SolrInputDocument();
  doc.setField("id", 1);
  doc.setField("terms_s", "samsung");
  getSolrClient().add(doc);
  getSolrClient().commit(true, true);

  SolrQuery query = new SolrQuery();
  query.setRequestHandler("/terms");
  query.setTerms(true);
  query.setTermsLimit(5);
  query.setTermsLower("s");
  query.setTermsPrefix("s");
  query.addTermsField("terms_s");
  query.setTermsMinCount(1);

  QueryRequest request = new QueryRequest(query);
  List<Term> terms = request.process(getSolrClient()).getTermsResponse().getTerms("terms_s");

  Assert.assertNotNull(terms);
  Assert.assertEquals(terms.size(), 1);

  Term term = terms.get(0);
  Assert.assertEquals(term.getTerm(), "samsung");
  Assert.assertEquals(term.getFrequency(), 1);
}
 
Example 2
Source File: TermsQueryParser.java    From dubbox with Apache License 2.0 4 votes vote down vote up
protected void appendTermsFieldToSolrQuery(Field field, SolrQuery solrQuery) {
	if (field != null && StringUtils.hasText(field.getName())) {
		solrQuery.addTermsField(field.getName());
	}
}
 
Example 3
Source File: CustomTermsComponentTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static String findAndCheckTerms(final MiniSolrCloudCluster cluster, final String collection,
  String requestHandlerName, String in_shards,
  String field1, String value1,
  String field2, String value2,
  String solrServerExceptionMessagePrefix) throws IOException {

  // compose the query ...
  final SolrQuery solrQuery =  new SolrQuery("*:*");
  solrQuery.setRequestHandler(requestHandlerName);
  solrQuery.add("shards.qt", requestHandlerName);
  // ... asking for terms ...
  solrQuery.setTerms(true);
  if (field1 != null) {
    solrQuery.addTermsField(field1);
  }
  if (field2 != null) {
    solrQuery.addTermsField(field2);
  }
  // ... and shards info ...
  solrQuery.add("shards.info", "true");
  // ... passing shards to use (if we have a preference)
  if (in_shards != null) {
    solrQuery.add("shards", in_shards);
  }

  // make the query
  final QueryResponse queryResponse;
  try {
    queryResponse = new QueryRequest(solrQuery)
        .process(cluster.getSolrClient(), collection);
    assertNull("expected exception ("+solrServerExceptionMessagePrefix+") not encountered", solrServerExceptionMessagePrefix);
  } catch (SolrServerException sse) {
    assertNotNull("unexpectedly caught exception "+sse, solrServerExceptionMessagePrefix);
    assertTrue(sse.getMessage().startsWith(solrServerExceptionMessagePrefix));
    assertThat(sse.getCause().getMessage(), containsString("not on the shards whitelist"));
    return null;
  }

  // analyse the response ...
  final TermsResponse termsResponse = queryResponse.getTermsResponse();
  // ... checking the terms returned ...
  checkTermsResponse(termsResponse, field1, value1);
  checkTermsResponse(termsResponse, field2, value2);
  // ... and assemble info about the shards ...
  final String out_shards = extractShardAddresses(queryResponse, ",");
  // ... to return to the caller
  return out_shards;
}