Java Code Examples for org.apache.solr.common.SolrInputDocument#addField()
The following examples show how to use
org.apache.solr.common.SolrInputDocument#addField() .
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: Http2SolrClientTest.java From lucene-solr with Apache License 2.0 | 6 votes |
@Test public void testCollectionParameters() throws IOException, SolrServerException { try (Http2SolrClient client = getHttp2SolrClient(jetty.getBaseUrl().toString())) { SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", "collection"); client.add("collection1", doc); client.commit("collection1"); assertEquals(1, client.query("collection1", new SolrQuery("id:collection")).getResults().getNumFound()); } final String collection1Url = jetty.getBaseUrl().toString() + "/collection1"; try (Http2SolrClient client = getHttp2SolrClient(collection1Url)) { assertEquals(1, client.query(new SolrQuery("id:collection")).getResults().getNumFound()); } }
Example 2
Source File: TestLBHttpSolrClient.java From lucene-solr with Apache License 2.0 | 6 votes |
private void addDocs(SolrInstance solrInstance) throws IOException, SolrServerException { List<SolrInputDocument> docs = new ArrayList<>(); for (int i = 0; i < 10; i++) { SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", i); doc.addField("name", solrInstance.name); docs.add(doc); } SolrResponseBase resp; try (HttpSolrClient client = getHttpSolrClient(solrInstance.getUrl(), httpClient)) { resp = client.add(docs); assertEquals(0, resp.getStatus()); resp = client.commit(); assertEquals(0, resp.getStatus()); } }
Example 3
Source File: SolrjConverters.java From dubbox with Apache License 2.0 | 6 votes |
@Override public SolrInputDocument convert(Update source) { if (source == null) { return null; } Assert.notNull(source.getIdField()); Assert.hasText(source.getIdField().getName()); SolrInputDocument solrInputDocument = new SolrInputDocument(); solrInputDocument.addField(source.getIdField().getName(), source.getIdField().getValue()); if (source.getVersion() != null) { solrInputDocument.addField(VERSION_FIELD_ID, source.getVersion()); } for (UpdateField field : source.getUpdates()) { HashMap<String, Object> mapValue = new HashMap<String, Object>(1); mapValue.put(field.getAction().getSolrOperation(), getUpdateValue(field)); solrInputDocument.addField(field.getName(), mapValue); } return solrInputDocument; }
Example 4
Source File: FullSolrCloudDistribCmdsTest.java From lucene-solr with Apache License 2.0 | 6 votes |
/** * Recursive helper function for building out child and grandchild docs */ private long addChildren(String prefix, SolrInputDocument topDocument, int childIndex, boolean lastLevel, long docId) { SolrInputDocument childDocument = new SolrInputDocument(); childDocument.addField("id", docId++); childDocument.addField("type_s", prefix); for (int index = 0; index < childIndex; ++index) { childDocument.addField(childIndex + prefix + index + "_s", childIndex + "value"+ index); } if (!lastLevel) { for (int i = 0; i < childIndex * 2; ++i) { docId = addChildren("grand", childDocument, i, true, docId); } } topDocument.addChildDocument(childDocument); return docId; }
Example 5
Source File: ReplicationFactorTest.java From lucene-solr with Apache License 2.0 | 6 votes |
protected void addDocs(Set<Integer> docIds, int expectedRf, int retries) throws Exception { Integer[] idList = docIds.toArray(new Integer[docIds.size()]); if (idList.length == 1) { sendDoc(idList[0], expectedRf); return; } List<SolrInputDocument> batch = new ArrayList<SolrInputDocument>(10); for (int docId : idList) { SolrInputDocument doc = new SolrInputDocument(); doc.addField(id, docId); doc.addField("a_t", "hello" + docId); batch.add(doc); } sendDocsWithRetry(batch, expectedRf, retries, 1); }
Example 6
Source File: MetadataManager.java From ambari-logsearch with Apache License 2.0 | 6 votes |
private SolrInputDocument createMetadata(LogsearchMetaData metadata) { String name = metadata.getName(); SolrInputDocument solrInputDoc = new SolrInputDocument(); if (!isValid(metadata, false)) { throw new MalformedInputException("Name,type and value should be specified, input:" + ToStringBuilder.reflectionToString(metadata)); } final String userName = LogSearchContext.getCurrentUsername().toLowerCase(); solrInputDoc.addField(ID, generateUniqueId(metadata, userName)); solrInputDoc.addField(USER_NAME, userName); solrInputDoc.addField(VALUE, metadata.getValue()); solrInputDoc.addField(NAME, name); solrInputDoc.addField(TYPE, metadata.getType()); logger.info("Created metadata: name: {}, type: {}, user: {}", name, metadata.getType(), userName); return solrInputDoc; }
Example 7
Source File: DefaultSolrInputDocumentMapper.java From storm-solr with Apache License 2.0 | 6 votes |
protected void addDynField(SolrInputDocument doc, String fieldName, Object value, Class type, String dynamicFieldSuffix) { if (type.isArray()) return; // TODO: Array types not supported yet ... if (dynamicFieldSuffix == null) { dynamicFieldSuffix = getDefaultDynamicFieldMapping(type); // treat strings with multiple terms as text only if using the default! if ("_s".equals(dynamicFieldSuffix)) { String str = (String) value; if (str.indexOf(" ") != -1) dynamicFieldSuffix = "_t"; } } if (dynamicFieldSuffix != null) // don't auto-map if we don't have a type doc.addField(fieldName + dynamicFieldSuffix, value); }
Example 8
Source File: DocValuesNotIndexedTest.java From lucene-solr with Apache License 2.0 | 6 votes |
@Test public void testGroupingSorting() throws IOException, SolrServerException { CloudSolrClient client = cluster.getSolrClient(); // The point of these is to have at least one shard w/o the value. // While getting values for each of these fields starts _out_ random, each successive // _value_ increases. List<SolrInputDocument> docs = new ArrayList<>(3); docs.add(makeGSDoc(2, fieldsToTestGroupSortFirst, fieldsToTestGroupSortLast)); docs.add(makeGSDoc(1, fieldsToTestGroupSortFirst, fieldsToTestGroupSortLast)); docs.add(makeGSDoc(3, fieldsToTestGroupSortFirst, fieldsToTestGroupSortLast)); SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", 4); docs.add(doc); new UpdateRequest() .add(docs) .commit(client, COLLECTION); checkSortOrder(client, fieldsToTestGroupSortFirst, "asc", new String[]{"4", "2", "1", "3"}, new String[]{"4", "1", "2", "3"}); checkSortOrder(client, fieldsToTestGroupSortFirst, "desc", new String[]{"3", "1", "2", "4"}, new String[]{"2", "3", "1", "4"}); checkSortOrder(client, fieldsToTestGroupSortLast, "asc", new String[]{"4", "2", "1", "3"}, new String[]{"4", "1", "2", "3"}); checkSortOrder(client, fieldsToTestGroupSortLast, "desc", new String[]{"3", "1", "2", "4"}, new String[]{"2", "3", "1", "4"}); }
Example 9
Source File: TestRetrieveFieldsOptimizer.java From lucene-solr with Apache License 2.0 | 5 votes |
static void addDocWithAllFields(int idx) { // for each doc, add a doc with all the fields with values and store the expected return. Map<String, List<String>> fieldsExpectedVals = new HashMap<>(); SolrInputDocument sdoc = new SolrInputDocument(); String id = "str" + idx; sdoc.addField("str", id); sdoc.addField(idNotStoredDv, id); fieldsExpectedVals.put(idNotStoredDv, Collections.singletonList(id)); sdoc.addField(idStoredNotDv, id); fieldsExpectedVals.put(idStoredNotDv, Collections.singletonList(id)); for (RetrieveField field : fieldsHolder.fields.values()) { if (field.name.equals(idNotStoredDv) || field.name.equals(idStoredNotDv)) { continue; } List<String> valsAsStrings = field.getValsForField(); for (String val : valsAsStrings) { sdoc.addField(field.schemaField.getName(), val); } fieldsExpectedVals.put(field.name, valsAsStrings); } allFieldValuesInput.put(id, fieldsExpectedVals); assertU(adoc(sdoc)); }
Example 10
Source File: ReplicationFactorTest.java From lucene-solr with Apache License 2.0 | 5 votes |
protected int sendDoc(int docId, int minRf) throws Exception { UpdateRequest up = new UpdateRequest(); boolean minRfExplicit = maybeAddMinRfExplicitly(minRf, up); SolrInputDocument doc = new SolrInputDocument(); doc.addField(id, String.valueOf(docId)); doc.addField("a_t", "hello" + docId); up.add(doc); return runAndGetAchievedRf(up, minRfExplicit, minRf); }
Example 11
Source File: TestPointFields.java From lucene-solr with Apache License 2.0 | 5 votes |
public void doTestReturnNonStored(final String fieldName, boolean shouldReturnFieldIfRequested, final String... values) throws Exception { final String RETURN_FIELD = "count(//doc/*[@name='" + fieldName + "'])=10"; final String DONT_RETURN_FIELD = "count(//doc/*[@name='" + fieldName + "'])=0"; assertFalse(h.getCore().getLatestSchema().getField(fieldName).stored()); for (int i=0; i < 10; i++) { SolrInputDocument doc = sdoc("id", String.valueOf(i)); for (String value : values) { doc.addField(fieldName, value); } assertU(adoc(doc)); } assertU(commit()); assertQ(req("q", "*:*", "rows", "100", "fl", "id," + fieldName), "//*[@numFound='10']", "count(//doc)=10", // exactly 10 docs in response (shouldReturnFieldIfRequested?RETURN_FIELD:DONT_RETURN_FIELD)); // no field in any doc other then 'id' assertQ(req("q", "*:*", "rows", "100", "fl", "*"), "//*[@numFound='10']", "count(//doc)=10", // exactly 10 docs in response DONT_RETURN_FIELD); // no field in any doc other then 'id' assertQ(req("q", "*:*", "rows", "100"), "//*[@numFound='10']", "count(//doc)=10", // exactly 10 docs in response DONT_RETURN_FIELD); // no field in any doc other then 'id' clearIndex(); assertU(commit()); }
Example 12
Source File: ClusteringTest.java From solr-spatial-clustering with Apache License 2.0 | 5 votes |
@BeforeClass public static void setup() throws SolrServerException, IOException { infrastructureRule.getSolrClient().deleteByQuery("*:*"); for (int i = 0; i < TOTAL_DOC_COUNT; i++) { SolrInputDocument solrDocument = new SolrInputDocument(); solrDocument.addField(SOLR_FIELD_ID, String.valueOf(i + 1)); solrDocument.addField(SOLR_FIELD_LAT, String.valueOf(getRandomLat())); solrDocument.addField(SOLR_FIELD_LON, String.valueOf(getRandomLon())); infrastructureRule.getSolrClient().add(solrDocument); } infrastructureRule.getSolrClient().commit(); }
Example 13
Source File: OntologySolrLoader.java From owltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void collectClass(SolrInputDocument cls_doc, OWLGraphWrapper graph, OWLClass c) { // Single fields. cls_doc.addField("document_category", "ontology_class"); cls_doc.addField("source", graph.getNamespace(c)); cls_doc.addField("is_obsolete", graph.getIsObsolete(c)); cls_doc.addField("comment", graph.getComment(c)); // Term synonym gathering. java.util.List<ISynonym> syns = graph.getOBOSynonyms(c); if( syns != null && !syns.isEmpty() ){ for( ISynonym s : syns ){ String synLabel = s.getLabel(); // Standard neutral synonym. cls_doc.addField("synonym", synLabel); // can add multiples // // EXPERIMENTAL: scoped synonym label. // String synScope = s.getScope(); // String synScopeName = "synonym_label_with_scope_" + synScope.toLowerCase(); // cls_doc.addField(synScopeName, synLabel); } } // Add alternate ids, subsets, and definition xrefs. cramString(cls_doc, "alternate_id", graph.getAltIds(c)); cramString(cls_doc, "subset", graph.getSubsets(c)); cramString(cls_doc, "definition_xref", graph.getDefXref(c)); }
Example 14
Source File: QuerySolrIT.java From nifi with Apache License 2.0 | 5 votes |
@BeforeClass public static void setup() throws IOException, SolrServerException { CloudSolrClient solrClient = createSolrClient(); Path currentDir = Paths.get(ZK_CONFIG_PATH); ZkClientClusterStateProvider stateProvider = new ZkClientClusterStateProvider(SOLR_LOCATION); stateProvider.uploadConfig(currentDir, ZK_CONFIG_NAME); solrClient.setDefaultCollection(SOLR_COLLECTION); if (!solrClient.getZkStateReader().getClusterState().hasCollection(SOLR_COLLECTION)) { CollectionAdminRequest.Create createCollection = CollectionAdminRequest.createCollection(SOLR_COLLECTION, ZK_CONFIG_NAME, 1, 1); createCollection.process(solrClient); } else { solrClient.deleteByQuery("*:*"); } for (int i = 0; i < 10; i++) { SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", "doc" + i); Date date = new Date(); doc.addField("created", DATE_FORMAT.format(date)); doc.addField("string_single", "single" + i + ".1"); doc.addField("string_multi", "multi" + i + ".1"); doc.addField("string_multi", "multi" + i + ".2"); doc.addField("integer_single", i); doc.addField("integer_multi", 1); doc.addField("integer_multi", 2); doc.addField("integer_multi", 3); doc.addField("double_single", 0.5 + i); solrClient.add(doc); } solrClient.commit(); }
Example 15
Source File: TestSolrDocLevelOperations.java From incubator-sentry with Apache License 2.0 | 5 votes |
/** * Creates docs as follows and verifies queries work as expected: * - creates NUM_DOCS documents, where the document id equals the order * it was created in, starting at 0 * - when id % 4 == 0, documents get "role0" auth token * - when id % 4 == 1, documents get "role1" auth token * - when id % 4 == 2, documents get "role2" auth token * - when id % 4 == 3, documents get "role3" auth token * - all documents get a admin role */ private void createDocument(String collectionName) throws Exception { // ensure no current documents verifyDeletedocsPass(ADMIN_USER, collectionName, true); // create documents ArrayList<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); for (int i = 0; i < NUM_DOCS; ++i) { SolrInputDocument doc = new SolrInputDocument(); String iStr = Long.toString(i); doc.addField("id", iStr); doc.addField("description", "description" + iStr); if (i % 4 == 0) { doc.addField(AUTH_FIELD, "role0"); } else if (i % 4 ==1) { doc.addField(AUTH_FIELD, "role1"); } else if (i % 4 ==2) { doc.addField(AUTH_FIELD, "role2"); } else { doc.addField(AUTH_FIELD, "role3"); } doc.addField(AUTH_FIELD, ADMIN_ROLE); docs.add(doc); } setAuthenticationUser(ADMIN_USER); CloudSolrServer server = getCloudSolrServer(collectionName); try { server.add(docs); server.commit(true, true); } finally { server.shutdown(); } }
Example 16
Source File: TestOverriddenPrefixQueryForCustomFieldType.java From lucene-solr with Apache License 2.0 | 5 votes |
public void createIndex(int nDocs) { Random r = random(); for (int i=0; i<nDocs; i++) { SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", ""+i); int t = r.nextInt(1000); if(t%3 == 0) { doc.addField("swap_foo_bar_in_prefix_query", "foo" + i); counts[0]++; } else if(t%3 == 1) { doc.addField("swap_foo_bar_in_prefix_query", "foo" + i); doc.addField("swap_foo_bar_in_prefix_query", "spam" + i); otherCounts++; counts[0]++; } else { doc.addField("swap_foo_bar_in_prefix_query", "bar" + i); counts[1]++; } //Randomly add noise doc.addField("int_prefix_as_range", i); doc.addField("intfield", i); assertU(adoc(doc)); } assertU(commit()); }
Example 17
Source File: IndexMedlineSolr.java From lumongo with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws Exception { if (args.length != 2) { System.out.println("Usage: directoryWithXml solrServerUrl"); System.out.println(" ex. /tmp/medline http://127.0.0.1:8983/solr/medline-example"); System.exit(1); } String medlineDirectory = args[0]; String server = args[1]; if (!(new File(medlineDirectory)).exists()) { System.out.println("Directory <" + medlineDirectory + "> does not exist"); System.exit(2); } LogUtil.loadLogConfig(); SolrClient solrClient = new HttpSolrClient(server); final AtomicInteger counter = new AtomicInteger(); final long start = System.currentTimeMillis(); StaxJAXBReader<MedlineCitation> s = new MedlineJAXBReader(MedlineCitation.class, "MedlineCitation", 8) { @Override public void handleMedlineDocument(MedlineDocument medlineDocument) throws Exception { SolrInputDocument solrDoc = new SolrInputDocument(); solrDoc.addField("id", medlineDocument.getPmid()); solrDoc.addField("titleText", medlineDocument.getTitle()); solrDoc.addField("journalTitleText", medlineDocument.getJournalTitle()); solrDoc.addField("journalIsoText", medlineDocument.getJournalIso()); solrDoc.addField("abstractText", medlineDocument.getAbstractText()); solrDoc.addField("publicationDate", medlineDocument.getPublicationDate()); solrDoc.addField("publicationDateExact", medlineDocument.getPublicationDate()); solrDoc.addField("publicationYearText", medlineDocument.getPubYear()); solrDoc.addField("publicationYearExact", medlineDocument.getPubYear()); solrDoc.addField("journalVolumeText", medlineDocument.getJournalVolume()); solrDoc.addField("journalIssueText", medlineDocument.getJournalIssue()); solrDoc.addField("journalCountryText", medlineDocument.getJournalCountry()); solrDoc.addField("journalCountryExact", medlineDocument.getJournalCountry()); solrDoc.addField("issnText", medlineDocument.getIssn()); solrDoc.addField("issnExact", medlineDocument.getIssn()); solrDoc.addField("authorsTextMulti", medlineDocument.getAuthors()); solrDoc.addField("authorsExactMulti", medlineDocument.getAuthors()); solrDoc.addField("paginationText", medlineDocument.getPagination()); solrDoc.addField("citationText", medlineDocument.getCitation()); solrClient.add(solrDoc); int c = counter.incrementAndGet(); if (c % 50000 == 0) { long timeSinceStart = System.currentTimeMillis() - start; System.out.println(timeSinceStart + "\t" + c); } } }; Path medlineXmlDirectory = Paths.get(medlineDirectory); try (DirectoryStream<Path> directory = Files.newDirectoryStream(medlineXmlDirectory)) { for (Path file : directory) { System.out.println("Found <" + file.toAbsolutePath().toString() + ">"); if (file.toAbsolutePath().toString().endsWith("xml")) { try { s.handleFile(file.toAbsolutePath().toString()); } catch (Exception e) { System.err.println("Failed to process <" + file.toAbsolutePath().toString() + ">: " + e); } } } } }
Example 18
Source File: PANTHERSolrDocumentLoader.java From owltools with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public void load() throws SolrServerException, IOException { LOG.info("Loading PANTHER documents (" + pset.getTreeIDSet().size() + " total)..."); // Cycle through all of the trees in the forest. for( String tree_id : pset.getTreeIDSet() ){ PANTHERTree ptree = pset.getTreeByID(tree_id); if( ptree != null ){ //LOG.info("Loading PANTHER tree: " + ptree.getTreeID()); // Create the panther family document. SolrInputDocument family_doc = new SolrInputDocument(); // Base information. family_doc.addField("document_category", "family"); family_doc.addField("id", ptree.getPANTHERID()); family_doc.addField("panther_family", ptree.getPANTHERID()); family_doc.addField("panther_family_label", ptree.getTreeLabel()); // Add in the bioentities and maps. family_doc.addField("bioentity_list", ptree.getAssociatedGeneProductIDs()); family_doc.addField("bioentity_list_label", ptree.getAssociatedGeneProductLabels()); family_doc.addField("bioentity_list_map", ptree.getAssociatedGeneProductJSONMap()); // Okay, push into loader. add(family_doc); // Incremental commits. current_doc_number++; if( current_doc_number % doc_limit_trigger == 0 ){ LOG.info("Processed " + doc_limit_trigger + " trees at " + current_doc_number + " and committing..."); incrementalAddAndCommit(); } } } // Get the remainder of the docs in. LOG.info("Doing clean-up (final) commit at " + current_doc_number + " trees..."); addAllAndCommit(); LOG.info("Done."); }
Example 19
Source File: SolrExampleTestsBase.java From lucene-solr with Apache License 2.0 | 4 votes |
/** * query the example */ @Test public void testCommitWithinOnAdd() throws Exception { // make sure it is empty... SolrClient client = getSolrClient(); client.deleteByQuery("*:*");// delete everything! client.commit(); QueryResponse rsp = client.query(new SolrQuery("*:*")); Assert.assertEquals(0, rsp.getResults().getNumFound()); // Now try a timed commit... SolrInputDocument doc3 = new SolrInputDocument(); doc3.addField("id", "id3"); doc3.addField("name", "doc3"); doc3.addField("price", 10); UpdateRequest up = new UpdateRequest(); up.add(doc3); up.setCommitWithin(500); // a smaller commitWithin caused failures on the // following assert up.process(client); rsp = client.query(new SolrQuery("*:*")); Assert.assertEquals(0, rsp.getResults().getNumFound()); // TODO: not a great way to test this - timing is easily out // of whack due to parallel tests and various computer specs/load Thread.sleep(1000); // wait 1 sec // now check that it comes out... rsp = client.query(new SolrQuery("id:id3")); int cnt = 0; while (rsp.getResults().getNumFound() == 0) { // wait and try again for slower/busier machines // and/or parallel test effects. if (cnt++ == 10) { break; } Thread.sleep(2000); // wait 2 seconds... rsp = client.query(new SolrQuery("id:id3")); } Assert.assertEquals(1, rsp.getResults().getNumFound()); // Now test the new convenience parameter on the add() for commitWithin SolrInputDocument doc4 = new SolrInputDocument(); doc4.addField("id", "id4"); doc4.addField("name", "doc4"); doc4.addField("price", 10); client.add(doc4, 500); Thread.sleep(1000); // wait 1 sec // now check that it comes out... rsp = client.query(new SolrQuery("id:id4")); cnt = 0; while (rsp.getResults().getNumFound() == 0) { // wait and try again for slower/busier machines // and/or parallel test effects. if (cnt++ == 10) { break; } Thread.sleep(2000); // wait 2 seconds... rsp = client.query(new SolrQuery("id:id3")); } Assert.assertEquals(1, rsp.getResults().getNumFound()); }
Example 20
Source File: SolrInformationServer.java From SearchServices with GNU Lesser General Public License v3.0 | 4 votes |
@Override public void indexTransaction(Transaction info, boolean overwrite) throws IOException { canUpdate(); UpdateRequestProcessor processor = null; try (SolrQueryRequest request = newSolrQueryRequest()) { processor = this.core.getUpdateProcessingChain(null).createProcessor(request, newSolrQueryResponse()); AddUpdateCommand cmd = new AddUpdateCommand(request); cmd.overwrite = overwrite; SolrInputDocument input = new SolrInputDocument(); input.addField(FIELD_SOLR4_ID, AlfrescoSolrDataModel.getTransactionDocumentId(info.getId())); input.addField(FIELD_VERSION, 0); input.addField(FIELD_TXID, info.getId()); input.addField(FIELD_INTXID, info.getId()); input.addField(FIELD_TXCOMMITTIME, info.getCommitTimeMs()); input.addField(FIELD_DOC_TYPE, DOC_TYPE_TX); /* For backwards compat reasons adding 3 new stored fields. 2 of these fields are duplicate data but there are needed so that we can properly update the transaction record for ACE-4284. */ //This fields will be used to update the transaction record //They will only be on the record until the cascading updates for this transaction are processed input.addField(FIELD_S_TXID, info.getId()); input.addField(FIELD_S_TXCOMMITTIME, info.getCommitTimeMs()); if (cascadeTrackingEnabled()) { //Set the cascade flag to 1. This means cascading updates have not been done yet. input.addField(FIELD_CASCADE_FLAG, 1); } cmd.solrDoc = input; processor.processAdd(cmd); putTransactionState(processor, request, info); } finally { if (processor != null) { processor.finish(); } } }