Java Code Examples for org.apache.solr.client.solrj.SolrServer#query()

The following examples show how to use org.apache.solr.client.solrj.SolrServer#query() . 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: AbstractSolrInputOperator.java    From attic-apex-malhar with Apache License 2.0 9 votes vote down vote up
@Override
public void emitTuples()
{
  SolrParams solrQueryParams = getQueryParams();
  try {
    SolrServer solrServer = solrServerConnector.getSolrServer();
    QueryResponse response = solrServer.query(solrQueryParams);
    SolrDocumentList queriedDocuments = response.getResults();
    for (SolrDocument solrDocument : queriedDocuments) {
      emitTuple(solrDocument);
      lastEmittedTuple = solrDocument;
      lastEmittedTimeStamp = System.currentTimeMillis();

      logger.debug("Emiting document: " + solrDocument.getFieldValue("name"));
    }
  } catch (SolrServerException ex) {
    throw new RuntimeException("Unable to fetch documents from Solr server", ex);
  }
}
 
Example 2
Source File: ThothServers.java    From thoth with BSD 3-Clause Clear License 6 votes vote down vote up
public ArrayList<ServerDetail> getList(SolrServer realTimeThoth) throws SolrServerException {
  ArrayList<ServerDetail> serverDetails = new ArrayList<ServerDetail>();
  // Using HierarchicalFaceting to fetch server details .http://wiki.apache.org/solr/HierarchicalFaceting
  QueryResponse qr = realTimeThoth.query(new SolrQuery("*:*").addFacetPivotField(FACET_PIVOT_FIELDS).setRows(0).setFacetLimit(FACET_LIMIT));
  NamedList<List<PivotField>> pivots = qr.getFacetPivot();
  System.out.println("Found " + pivots.get(FACET_PIVOT_FIELDS).size()+" servers to monitor. Fetching information for these servers. Please wait");
  for (PivotField pivot: pivots.get(FACET_PIVOT_FIELDS)){
    String hostname = (String) pivot.getValue();
    for (PivotField pf: pivot.getPivot()){
      String coreName = (String) pf.getValue();
      ServerDetail detail = fetchServerDetails(hostname,coreName, realTimeThoth);
      if (detail != null) serverDetails.add(detail);
    }
  }
  return serverDetails;
}
 
Example 3
Source File: SolrLookingBlurServerTest.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@Test
public void fieldsRequestsShouldTurnIntoSelectors() throws Exception,
    TException {

  String table = "fieldsRequestsShouldTurnIntoSelectors";
  SolrServer server = TestTableCreator.newTable(table)
      .withRowCount(1).withRecordsPerRow(2)
      .withRecordColumns("fam.value", "fam.mvf").create();

  SolrQuery query = new SolrQuery("value0-0");
  query.setFields("fam.value");
  QueryResponse response = server.query(query);

  assertEquals("We should get our doc back for a valid test.", 1l, response.getResults().getNumFound());

  SolrDocument docResult = response.getResults().get(0);

  assertEquals("value0-0", docResult.getFieldValue("fam.value"));
  assertNull("We shouldn't get this one back since it wasnt in our fields.", docResult.getFieldValues("fam.mvf"));

  removeTable(table);
}
 
Example 4
Source File: SolrLookingBlurServerTest.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@Test
public void weShouldBeAbleToPageResults() throws SolrServerException, IOException, BlurException, TException {
  String table = "weShouldBeAbleToPageResults";
  SolrServer server = createServerAndTableWithSimpleTestDoc(table);

  SolrQuery query = new SolrQuery("123");
  query.setFields("fam.value");
  QueryResponse response = server.query(query);

  assertEquals("We should get our doc back for a valid test.", 1l, response.getResults().getNumFound());

  SolrDocument docResult = response.getResults().get(0);

  assertEquals("123", docResult.getFieldValue("fam.value"));
  assertNull("We shouldn't get this one back since it wasnt in our fields.", docResult.getFieldValues("fam.mvf"));

  removeTable(table);
}
 
Example 5
Source File: Searcher.java    From entando-components with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected List<String> executeQuery(SolrQuery solrQuery) throws ApsSystemException {
  	List<String> contentsId = new ArrayList<String>();
  	try {
	HttpClient httpClient = new DefaultHttpClient();
	SolrServer solr = new HttpSolrServer(this.getSolrUrl(), httpClient, new XMLResponseParser());
	QueryResponse rsp = solr.query(solrQuery);
          SolrDocumentList solrDocumentList = rsp.getResults();
          for (SolrDocument doc : solrDocumentList) {
		String id = (String) doc.getFieldValue(CmsSearchEngineManager.CONTENT_ID_FIELD_NAME); //id is the uniqueKey field
              contentsId.add(id);
          }
  	} catch (SolrServerException e) {
	throw new ApsSystemException("Error on solr server", e);
} catch (Throwable t) {
  		throw new ApsSystemException("Error extracting response", t);
  	}
  	return contentsId;
  }
 
Example 6
Source File: TestSolr.java    From DistributedCrawler with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearch() throws SolrServerException{
	String url = "http://localhost:8888/solr";
	  SolrServer server = new HttpSolrServer(url);
	  SolrQuery query = new SolrQuery("火箭");
	  query.setStart(0);
	  query.setRows(100);
	   QueryResponse response = server.query(query);
	   SolrDocumentList docs = response.getResults();
	   System.out.println("文档个数:" + docs.getNumFound());
	   System.out.println("查询时间:" + response.getQTime());
	   for (SolrDocument doc : docs) {
	    System.out.println("id: " + doc.getFieldValue("id"));
	    System.out.println("title: " + doc.getFieldValue("title"));
	    System.out.println("contect: "+doc.getFieldValue("content"));
	   }
}
 
Example 7
Source File: SolrDeleteDuplicates.java    From anthelion with Apache License 2.0 5 votes vote down vote up
/** Return each index as a split. */
public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException {
  SolrServer solr = SolrUtils.getCommonsHttpSolrServer(job);

  final SolrQuery solrQuery = new SolrQuery(SOLR_GET_ALL_QUERY);
  solrQuery.setFields(SolrConstants.ID_FIELD);
  solrQuery.setRows(1);

  QueryResponse response;
  try {
    response = solr.query(solrQuery);
  } catch (final SolrServerException e) {
    throw new IOException(e);
  }

  int numResults = (int)response.getResults().getNumFound();
  int numDocsPerSplit = (numResults / numSplits); 
  int currentDoc = 0;
  SolrInputSplit[] splits = new SolrInputSplit[numSplits];
  for (int i = 0; i < numSplits - 1; i++) {
    splits[i] = new SolrInputSplit(currentDoc, numDocsPerSplit);
    currentDoc += numDocsPerSplit;
  }
  splits[splits.length - 1] = new SolrInputSplit(currentDoc, numResults - currentDoc);

  return splits;
}
 
Example 8
Source File: SolrEntityQueryMixin.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
public SolrDocumentList search( String queryString ) throws SolrServerException
{
    SolrServer server = solr.solrServer();

    NamedList<Object> list = new NamedList<>();

    list.add( "q", queryString );

    QueryResponse query = server.query( SolrParams.toSolrParams( list ) );
    return query.getResults();
}
 
Example 9
Source File: ThothServers.java    From thoth with BSD 3-Clause Clear License 5 votes vote down vote up
private ServerDetail fetchServerDetails(String hostname, String coreName, SolrServer realTimeThoth) throws SolrServerException {
  System.out.println("Fetching server details for hostname("+hostname+") coreName("+coreName+")");
  QueryResponse qr = realTimeThoth.query(new SolrQuery("hostname_s:\"" + hostname + "\"" +" AND " + "coreName_s:\"" +coreName + "\" AND NOT exception_b:true AND NOT slowQuery_b:true").setRows(1));
  SolrDocumentList solrDocumentList = qr.getResults();
  ServerDetail sd = null;
  if (qr.getResults().getNumFound() > 0) {
    String pool = (String)solrDocumentList.get(0).getFieldValue(POOL);
    String port = solrDocumentList.get(0).getFieldValue(PORT).toString();
    sd = new ServerDetail(hostname, pool, port, coreName);
  }
  return sd;
}
 
Example 10
Source File: SolrDeleteDuplicates.java    From anthelion with Apache License 2.0 4 votes vote down vote up
public RecordReader<Text, SolrRecord> getRecordReader(final InputSplit split,
    final JobConf job, 
    Reporter reporter)
    throws IOException {

  SolrServer solr = SolrUtils.getCommonsHttpSolrServer(job);
  SolrInputSplit solrSplit = (SolrInputSplit) split;
  final int numDocs = solrSplit.getNumDocs();
  
  SolrQuery solrQuery = new SolrQuery(SOLR_GET_ALL_QUERY);
  solrQuery.setFields(SolrConstants.ID_FIELD, SolrConstants.BOOST_FIELD,
                      SolrConstants.TIMESTAMP_FIELD,
                      SolrConstants.DIGEST_FIELD);
  solrQuery.setStart(solrSplit.getDocBegin());
  solrQuery.setRows(numDocs);

  QueryResponse response;
  try {
    response = solr.query(solrQuery);
  } catch (final SolrServerException e) {
    throw new IOException(e);
  }

  final SolrDocumentList solrDocs = response.getResults();

  return new RecordReader<Text, SolrRecord>() {

    private int currentDoc = 0;

    public void close() throws IOException { }

    public Text createKey() {
      return new Text();
    }

    public SolrRecord createValue() {
      return new SolrRecord();
    }

    public long getPos() throws IOException {
      return currentDoc;
    }

    public float getProgress() throws IOException {
      return currentDoc / (float) numDocs;
    }

    public boolean next(Text key, SolrRecord value) throws IOException {
      if (currentDoc >= numDocs) {
        return false;
      }

      SolrDocument doc = solrDocs.get(currentDoc);
      String digest = (String) doc.getFieldValue(SolrConstants.DIGEST_FIELD);
      key.set(digest);
      value.readSolrDocument(doc);

      currentDoc++;
      return true;
    }    
  };
}
 
Example 11
Source File: SolrLookingBlurServerTest.java    From incubator-retired-blur with Apache License 2.0 4 votes vote down vote up
@Test
public void basicFullTextQuery() throws Exception {
  String table = "basicFullTextQuery";
  SolrServer server = TestTableCreator.newTable(table)
      .withRowCount(1).withRecordsPerRow(2)
      .withRecordColumns("fam.value", "fam.mvf", "fam.mvf").create();

  SolrQuery query = new SolrQuery("value0-0");

  QueryResponse response = server.query(query);

  assertEquals("We should get our doc back.", 1l, response.getResults().getNumFound());

  SolrDocument docResult = response.getResults().get(0);

  assertEquals("0", docResult.getFieldValue("recordid"));
  assertEquals("value0-0", docResult.getFieldValue("fam.value"));

  Collection<Object> mvfVals = docResult.getFieldValues("fam.mvf");

  assertTrue("We should get all our values back[" + mvfVals + "]",
      CollectionUtils.isEqualCollection(mvfVals, Lists.newArrayList("value0-0", "value0-0")));

  removeTable(table);
}