本文整理汇总了Java中org.apache.solr.common.SolrInputDocument.addField方法的典型用法代码示例。如果您正苦于以下问题:Java SolrInputDocument.addField方法的具体用法?Java SolrInputDocument.addField怎么用?Java SolrInputDocument.addField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.solr.common.SolrInputDocument
的用法示例。
在下文中一共展示了SolrInputDocument.addField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException, SolrServerException {
String zkHost = "localhost:2181";
String defaultCollection = "collection1";
CloudSolrServer server = new CloudSolrServer(zkHost);
server.setDefaultCollection(defaultCollection);
for (int i = 0; i < 1000; ++i) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("cat", "book");
doc.addField("id", "book-" + i);
doc.addField("name", "The Legend of Po part " + i);
server.add(doc);
if (i % 100 == 0)
server.commit(); // periodically flush
}
server.commit();
}
示例2: fetchExistingOrCreateNewSolrDoc
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
private SolrInputDocument fetchExistingOrCreateNewSolrDoc(String id) throws SolrServerException, IOException {
Map<String, String> p = new HashMap<String, String>();
p.put("q", PHRASE + ":\"" + ClientUtils.escapeQueryChars(id) + "\"");
SolrParams params = new MapSolrParams(p);
QueryResponse res = solrAC.query(params);
if (res.getResults().size() == 0) {
return new SolrInputDocument();
} else if (res.getResults().size() == 1) {
SolrDocument doc = res.getResults().get(0);
SolrInputDocument tmp = new SolrInputDocument();
for (String fieldName : doc.getFieldNames()) {
tmp.addField(fieldName, doc.getFieldValue(fieldName));
}
return tmp;
} else {
throw new IllegalStateException("Query with params : " + p + " returned more than 1 hit!");
}
}
示例3: indexMap
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
public void indexMap(String id, Map<String, List<String>> objectMap)
throws IOException, SolrServerException {
SolrInputDocument document = new SolrInputDocument();
document.addField("id", id);
for (String key : objectMap.keySet()) {
Object value = objectMap.get(key);
if (value != null) {
// System.err.printf("%s: class: %s\n", key, value.getClass());
document.addField(key + "_ss", value);
}
}
try {
UpdateResponse response = solr.add(document);
// solr.commit();
} catch (HttpSolrClient.RemoteSolrException ex) {
System.err.printf("document: %s", document);
System.err.printf("Commit exception: %s\n", ex.getMessage());
}
}
示例4: indexDuplumKey
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
public void indexDuplumKey(String id, Map<String, Object> objectMap)
throws IOException, SolrServerException {
SolrInputDocument document = new SolrInputDocument();
document.addField("id", id);
for (String key : objectMap.keySet()) {
Object value = objectMap.get(key);
if (value != null) {
// System.err.printf("%s: class: %s\n", key, value.getClass());
document.addField(key + "_ss", value);
}
}
try {
UpdateResponse response = solr.add(document);
// solr.commit();
} catch (HttpSolrClient.RemoteSolrException ex) {
System.err.printf("document: %s", document);
System.err.printf("Commit exception: %s\n", ex.getMessage());
}
}
示例5: createDocument
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
/**
* Creates a Solr input document from the given list of name:value pairs.
*
* @param luceneFields
* @return {@link SolrInputDocument}
*/
public static SolrInputDocument createDocument(List<LuceneField> luceneFields) {
SolrInputDocument doc = new SolrInputDocument();
if (luceneFields != null) {
for (LuceneField luceneField : luceneFields) {
if (luceneField.getValue() != null) {
// doc.addField(luceneField.getField(), luceneField.getValue(), 0);
// Do not pass a boost value because starting with Solr 3.6, adding an index-time boost to primitive field types will cause the commit to fail
doc.addField(luceneField.getField(), luceneField.getValue());
}
}
return doc;
}
return null;
}
示例6: updateDoc
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
/**
* Performs an atomic update of the given solr document. Updates defined in partialUpdates will be applied to the existing document without making
* any changes to other fields.
*
* @param doc
* @param partialUpdates Map of update operations (usage: Map<field, Map<operation, value>>)
* @return
* @throws FatalIndexerException
* @should update doc correctly
* @should add GROUPFIELD if original doc doesn't have it
*/
public boolean updateDoc(SolrDocument doc, Map<String, Map<String, Object>> partialUpdates) throws FatalIndexerException {
String iddoc = (String) doc.getFieldValue(SolrConstants.IDDOC);
SolrInputDocument newDoc = new SolrInputDocument();
newDoc.addField(SolrConstants.IDDOC, iddoc);
if (!doc.containsKey(SolrConstants.GROUPFIELD)) {
logger.warn("Document to update {} doesn't contain {} adding now.", iddoc, SolrConstants.GROUPFIELD);
Map<String, Object> update = new HashMap<>();
update.put("set", iddoc);
newDoc.addField(SolrConstants.GROUPFIELD, update);
}
for (String field : partialUpdates.keySet()) {
newDoc.addField(field, partialUpdates.get(field));
}
if (writeToIndex(newDoc)) {
commit(false);
return true;
}
return false;
}
示例7: addNamedEntitiesFields
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
/**
* Adds named entity fields from the given list to the given SolrInputDocument.
*
* @param altoData
* @param doc
*/
@SuppressWarnings("unchecked")
protected void addNamedEntitiesFields(Map<String, Object> altoData, SolrInputDocument doc) {
List<String> neList = (List<String>) altoData.get("NAMEDENTITIES");
if (neList != null && !neList.isEmpty()) {
for (String ne : neList) {
String[] splitString = ne.split("_", 2);
if (splitString[1] != null) {
splitString[1] = cleanUpNamedEntityValue(splitString[1]);
String fieldName = new StringBuilder("NE_").append(splitString[0]).toString();
doc.addField(fieldName, splitString[1]);
doc.addField(new StringBuilder(fieldName).append(SolrConstants._UNTOKENIZED).toString(), splitString[1]);
}
}
}
}
示例8: writePageDoc
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
/**
*
* @param order
* @param rootDoc
* @param aggregateRecords
* @throws FatalIndexerException
*/
private void writePageDoc(int order, SolrInputDocument rootDoc, boolean aggregateRecords) throws FatalIndexerException {
String iddoc = pageDocOrderIddocMap.get(order);
SolrInputDocument doc = load(iddoc);
if (doc != null) {
// doc.setField(SolrConstants.ORDER, newOrder); // make sure order starts at 1 in the end
{
Path xmlFile = Paths.get(tempFolder.toAbsolutePath().toString(), new StringBuilder().append(iddoc).append("_").append(
SolrConstants.FULLTEXT).toString());
if (Files.isRegularFile(xmlFile)) {
try {
String xml = FileUtils.readFileToString(xmlFile.toFile(), "UTF8");
doc.addField(SolrConstants.FULLTEXT, xml);
// Add the child doc's FULLTEXT values to the SUPERFULLTEXT value of the root doc
if (aggregateRecords) {
// sbSuperDefault.append('\n').append(doc.getFieldValue(SolrConstants.FULLTEXT));
rootDoc.addField(SolrConstants.SUPERFULLTEXT, (doc.getFieldValue(SolrConstants.FULLTEXT)));
}
logger.debug("Found FULLTEXT for: {}", iddoc);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
checkAndAddAccessCondition(doc);
if (!solrHelper.writeToIndex(doc)) {
logger.error(doc.toString());
}
// newOrder++;
} else {
logger.error("Could not find serialized document for IDDOC: {}", iddoc);
}
}
示例9: addField
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
private static void addField(SolrInputDocument doc, String name, String value) {
// find if such field already exists
if (doc.get(name) == null) {
// System.out.println("Adding field " + name + " without previous values");
doc.addField(name, value);
} else {
// for some fields we can't allow multiple values, like ID field phrase, so we have to perform this check
SolrInputField f = doc.get(name);
for (Object val : f.getValues()) {
// fix for boolean values
if ((value.equalsIgnoreCase("t") && val.toString().equalsIgnoreCase("true")) ||
(value.equalsIgnoreCase("f") && val.toString().equalsIgnoreCase("false"))) {
return;
}
if (value.equals(val.toString())) {
// if we find such value in the doc, we will not add it again
// System.out.println("Field " + name + " already contains value " + value);
return;
}
}
// System.out.println("Adding field " + name + " without new value " + value);
f.addValue(value);
}
}
示例10: indexDocumentsAndCommit
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
/**
* Indexiert die übergebene Liste von Dokumenten und führt am Ende ein Commit durch.
*
* Tritt während der Indexierung ein Fehler auf, so werden die Änderungen nicht
* zurückgenommen, die bereits erfolgreich in den Solr-Index geschrieben werden konnten.
*
* @param gutenbergDocs die Liste der zu indexierenden Dokumente
*/
public void indexDocumentsAndCommit(List<GutenbergDoc> gutenbergDocs) {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.setBasicAuthCredentials(USERNAME, PASSWORD);
for (GutenbergDoc gutenbergDoc : gutenbergDocs) {
SolrInputDocument solrInputDocument = buildSolrDoc(gutenbergDoc);
if (gutenbergDoc.getDocId().equals("9")) {
// füge zu Dokument 9 ein Feld hinzu, dass es nicht im Solr-Schema gibt
solrInputDocument.addField("foo", "bar");
}
updateRequest.add(solrInputDocument);
}
updateRequest.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, false);
try {
updateRequest.process(solrClient);
System.out.println(gutenbergDocs.size() + " Dokumente erfolgreich indexiert!");
}
catch (SolrServerException|IOException e) {
System.err.println("Fehler bei der Indexierung der Dokumente: " + e.getMessage());
updateRequest.rollback();
}
}
示例11: indexAndCommitDocuments
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
/**
* Indexiert die übergebene Liste von Dokumenten und führt nach jedem Dokument einen Commit durch.
*
* Tritt während der Indexierung eines Dokuments ein Fehler auf, so gehen die zuvor bereits
* erfolgreich indexierten Dokumente nicht verloren.
*
* @param gutenbergDocs die Liste der zu indexierenden Dokumente
*/
public void indexAndCommitDocuments(List<GutenbergDoc> gutenbergDocs) {
for (GutenbergDoc gutenbergDoc : gutenbergDocs) {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.setBasicAuthCredentials(USERNAME, PASSWORD);
SolrInputDocument solrInputDocument = buildSolrDoc(gutenbergDoc);
if (gutenbergDoc.getDocId().equals("9")) {
// füge zu Dokument 9 ein Feld hinzu, dass es nicht im Solr-Schema gibt
solrInputDocument.addField("foo", "bar");
}
updateRequest.add(solrInputDocument);
updateRequest.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, false);
try {
updateRequest.process(solrClient);
System.out.println("Dokument " + gutenbergDoc.getDocId() + " erfolgreich indexiert!");
}
catch (SolrServerException|IOException e) {
System.err.println("Fehler bei der Indexierung der Dokumente: " + e.getMessage());
updateRequest.rollback();
}
}
}
示例12: getSolrInputDocuments
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
List<SolrInputDocument> getSolrInputDocuments( String solrXMLData ) throws Exception {
ArrayList<SolrInputDocument> solrDocs = new ArrayList<SolrInputDocument>( );
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse( new ByteArrayInputStream( solrXMLData.getBytes() ) );
NodeList docEls = xmlDocument.getElementsByTagName( "doc" );
for (int i = 0; i < docEls.getLength( ); i++) {
Element docEl = (Element)docEls.item( i );
SolrInputDocument solrDoc = new SolrInputDocument( );
solrDocs.add( solrDoc );
NodeList fieldEls = docEl.getElementsByTagName( "field" );
for (int f = 0; f < fieldEls.getLength(); f++ ) {
Element fieldEl = (Element)fieldEls.item( f );
solrDoc.addField( fieldEl.getAttribute( "name" ), fieldEl.getTextContent() );
}
}
return solrDocs;
}
示例13: getSolrInputDocument
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
public static SolrInputDocument getSolrInputDocument(MCRSolrInputDocument jaxbDoc) {
SolrInputDocument doc = new SolrInputDocument();
HashSet<MCRSolrInputField> duplicateFilter = new HashSet<>();
for (Object o : jaxbDoc.getFieldOrDoc()) {
if (o instanceof MCRSolrInputField) {
MCRSolrInputField field = (MCRSolrInputField) o;
if (field.getValue().isEmpty() || duplicateFilter.contains(field)) {
continue;
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("adding {}={}", field.getName(), field.getValue());
}
duplicateFilter.add(field);
doc.addField(field.getName(), field.getValue());
} else if (o instanceof MCRSolrInputDocument) {
MCRSolrInputDocument child = (MCRSolrInputDocument) o;
SolrInputDocument solrChild = getSolrInputDocument(child);
doc.addChildDocument(solrChild);
}
}
return doc;
}
示例14: createDocument
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
public static SolrInputDocument createDocument(String dataStr)
throws JsonParseException, JsonMappingException, IOException {
Map<String, Object> dataMap =
new ObjectMapper().readValue(dataStr, new TypeReference<HashMap<String, Object>>() {
});
SolrInputDocument document = new SolrInputDocument();
for (Iterator<String> i = dataMap.keySet().iterator(); i.hasNext();) {
String fieldName = i.next();
Object fieldValue = dataMap.get(fieldName);
document.addField(fieldName, fieldValue);
}
return document;
}
示例15: main
import org.apache.solr.common.SolrInputDocument; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException, SolrServerException {
HttpSolrServer server = new HttpSolrServer("http://localhost:8983/solr");
for (int i = 0; i < 1000; ++i) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("cat", "book");
doc.addField("id", "book-" + i);
doc.addField("name", "The Legend of Po part " + i);
server.add(doc);
if (i % 100 == 0)
server.commit(); // periodically flush
}
server.commit();
}