List of usage examples for org.apache.solr.client.solrj.impl HttpSolrClient add
public UpdateResponse add(String collection, Collection<SolrInputDocument> docs) throws SolrServerException, IOException
From source file:com.ibm.watson.apis.conversation_enhanced.listener.SetupThread.java
License:Open Source License
/** * Reads documents from the file4.json file, uploads them to the previously created collection and * commits them.// ww w . j a va 2 s.c o m * * @param solrClient */ private void indexDocuments(HttpSolrClient solrClient) { URL url = this.getClass().getClassLoader().getResource("file4.json"); //$NON-NLS-1$ File dataFile = null; try { dataFile = new File(url.toURI()); } catch (Exception e) { logger.error(e.getMessage()); ; } JsonArray a = null; try { a = (JsonArray) new JsonParser().parse(new FileReader(dataFile)).getAsJsonArray(); } catch (Exception e) { logger.error(Messages.getString("SetupThread.INGESTION_ERROR_PARSING") + e.getMessage()); //$NON-NLS-1$ } Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); for (int i = 0, size = a.size(); i < size; i++) { SolrInputDocument document = new SolrInputDocument(); JsonObject car = a.get(i).getAsJsonObject(); int id = car.get(Constants.SCHEMA_FIELD_ID).getAsInt(); String title = (String) car.get(Constants.SCHEMA_FIELD_TITLE).getAsString(); String body = (String) car.get(Constants.SCHEMA_FIELD_BODY).getAsString(); String sourceUrl = (String) car.get(Constants.SCHEMA_FIELD_SOURCE_URL).getAsString(); String contentHtml = (String) car.get(Constants.SCHEMA_FIELD_CONTENT_HTML).getAsString(); document.addField(Constants.SCHEMA_FIELD_ID, id); document.addField(Constants.SCHEMA_FIELD_TITLE, title); document.addField(Constants.SCHEMA_FIELD_BODY, body); document.addField(Constants.SCHEMA_FIELD_SOURCE_URL, sourceUrl); document.addField(Constants.SCHEMA_FIELD_CONTENT_HTML, contentHtml); docs.add(document); } logger.info(Messages.getString("SetupThread.INDEXING_DOCUMENT")); //$NON-NLS-1$ UpdateResponse addResponse; try { addResponse = solrClient.add(Constants.COLLECTION_NAME, docs); logger.info(addResponse); // Commit the document to the index so that it will be available for searching. solrClient.commit(Constants.COLLECTION_NAME); logger.info(Messages.getString("SetupThread.INDEX_DOC_COMMITTED")); //$NON-NLS-1$ } catch (SolrServerException e) { logger.error(Messages.getString("SetupThread.SOLR_INDEXING_ERROR") + e.getMessage()); //$NON-NLS-1$ } catch (IOException e) { logger.error(Messages.getString("SetupThread.SOLR_IO_ERROR") + e.getMessage()); //$NON-NLS-1$ } }