Example usage for org.apache.solr.common SolrInputDocument setField

List of usage examples for org.apache.solr.common SolrInputDocument setField

Introduction

In this page you can find the example usage for org.apache.solr.common SolrInputDocument setField.

Prototype

public void setField(String name, Object value) 

Source Link

Document

Set a field value; replacing the existing value if present.

Usage

From source file:brooklyn.entity.nosql.solr.SolrJSupport.java

License:Apache License

public void addDocument(Map<String, Object> fields) throws Exception {
    SolrInputDocument doc = new SolrInputDocument();
    for (String field : fields.keySet()) {
        doc.setField(field, fields.get(field));
    }/*from w  w w  . ja va 2 s.  c  o  m*/
    server.add(doc, 100);
}

From source file:com.buddycloud.channeldirectory.crawler.node.MetaDataCrawler.java

License:Apache License

private static void insertOrUpate(ChannelData channelData, Properties configuration) throws Exception {

    SolrInputDocument object = new SolrInputDocument();

    if (channelData.getGeolocation() != null) {
        Double lat = channelData.getGeolocation().getLat();
        Double lng = channelData.getGeolocation().getLng();

        if (lat != null && lng != null) {
            object.setField("geoloc", LATLNG_FORMAT.format(lat) + "," + LATLNG_FORMAT.format(lng));
        }/*from   ww  w  . j a va2  s.  c  o m*/
        String geoText = channelData.getGeolocation().getText();
        if (geoText != null) {
            object.setField("geoloc-text", geoText);
        }
    }

    object.setField("jid", channelData.getId());
    object.setField("title", channelData.getTitle());
    object.setField("description", channelData.getDescription());
    object.setField("creation-date", channelData.getCreationDate());
    object.setField("default-affiliation", channelData.getDefaultAffiliation());
    object.setField("channel-type", channelData.getChannelType()); //topic or personal

    SolrServer solrServer = SolrServerFactory.createChannelCore(configuration);
    solrServer.add(object);
    solrServer.commit();
}

From source file:com.buddycloud.channeldirectory.crawler.node.PostCrawler.java

License:Apache License

private void insert(PostData postData) throws SolrServerException, IOException {

    SolrInputDocument postDocument = new SolrInputDocument();

    postDocument.addField("id", postData.getId());
    postDocument.addField("parent_simpleid", postData.getParentSimpleId());
    postDocument.addField("parent_fullid", postData.getParentFullId());
    postDocument.addField("inreplyto", postData.getInReplyTo());
    postDocument.addField("author", postData.getAuthor());
    postDocument.addField("author-uri", postData.getAuthorUri());
    postDocument.addField("content", postData.getContent());

    postDocument.addField("updated", postData.getUpdated());
    postDocument.addField("published", postData.getPublished());

    Geolocation geolocation = postData.getGeolocation();

    if (geolocation != null) {

        Double lat = geolocation.getLat();
        Double lng = geolocation.getLng();

        if (lat != null && lng != null) {
            postDocument.setField("geoloc", LATLNG_FORMAT.format(lat) + "," + LATLNG_FORMAT.format(lng));
        }//w w  w  .j  a  v a  2s  . c om

        if (geolocation.getText() != null) {
            postDocument.addField("geoloc_text", geolocation.getText());
        }
    }

    SolrServer solrServer = SolrServerFactory.createPostCore(configuration);
    solrServer.add(postDocument);
    solrServer.commit();
}

From source file:com.cominvent.solr.update.processor.MappingUpdateProcessor.java

License:Apache License

@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
    if (isEnabled()) {

        SolrInputDocument doc = cmd.getSolrInputDocument();

        // Fetch document id
        String docId = "";
        if (doc.containsKey(docIdField))
            docId = (String) doc.getFieldValue(docIdField);

        String inValue = (String) doc.getFieldValue(inputField);
        String outValue;//from   w ww  . j  av a 2s. c om
        if (map.containsKey(inValue)) {
            outValue = map.get(inValue);
        } else {
            outValue = fallbackValue;
        }

        if (outValue != null && outValue.length() > 0) {
            log.debug("Mapping done for document " + docId + ": " + inValue + " => " + outValue);
            doc.setField(outputField, outValue);
        }

    } else {
        log.debug("MappingUpdateProcessor is not enabled. Skipping");
    }

    super.processAdd(cmd);
}

From source file:com.datasalt.pangool.solr.DefaultTupleDocumentConverter.java

License:Apache License

public SolrInputDocument convert(ITuple key, NullWritable value) throws IOException {
    SolrInputDocument document = new SolrInputDocument();
    for (Field field : key.getSchema().getFields()) {
        checkFieldType(field);//from   ww  w  .  j a  v  a  2  s  .c o m
        if (field.getType().equals(Type.STRING)) {
            // deep copy string
            document.setField(field.getName(), key.get(field.getName()).toString());
        } else { // primitive type
            document.setField(field.getName(), key.get(field.getName()));
        }
    }
    return document;
}

From source file:com.digitalpebble.behemoth.solr.LucidWorksWriter.java

License:Apache License

protected SolrInputDocument convertToSOLR(BehemothDocument doc) {
    final SolrInputDocument inputDoc = new SolrInputDocument();
    // map from a Behemoth document to a SOLR one
    // the field names below should be modified
    // to match the SOLR schema
    inputDoc.setField("id", doc.getUrl());
    inputDoc.setField("text", doc.getText());

    LOG.debug("Adding field : id\t" + doc.getUrl());
    //LOG.debug("Adding field : text\t" + doc.getText());

    //Rely on LucidWorks field mapping to handle this, or the dynamic fields
    MapWritable metadata = doc.getMetadata();
    if (includeMetadata && metadata != null) {
        for (Entry<Writable, Writable> entry : metadata.entrySet()) {
            inputDoc.addField(entry.getKey().toString(), entry.getValue().toString());
        }// www  .jav  a2 s  . com
    }
    // iterate on the annotations of interest and
    // create a new field for each one
    // it is advised NOT to set frequent annotation types
    // such as token as this would generate a stupidly large
    // number of fields which won't be used by SOLR for
    // tokenizing anyway.
    // what you can do though is to concatenate the token values
    // to form a new content string separated by spaces

    // iterate on the annotations
    if (includeAnnotations) {
        Iterator<Annotation> iterator = doc.getAnnotations().iterator();
        while (iterator.hasNext()) {
            Annotation current = iterator.next();
            // check whether it belongs to a type we'd like to send to SOLR
            Map<String, String> featureField = fieldMapping.get(current.getType());
            if (featureField == null)
                continue;
            // iterate on the expected features
            for (String targetFeature : featureField.keySet()) {
                String SOLRFieldName = featureField.get(targetFeature);
                String value = null;
                // special case for covering text
                if ("*".equals(targetFeature)) {
                    value = doc.getText().substring((int) current.getStart(), (int) current.getEnd());
                }
                // get the value for the feature
                else {
                    value = current.getFeatures().get(targetFeature);
                }
                LOG.debug("Adding field : " + SOLRFieldName + "\t" + value);
                // skip if no value has been found
                if (value != null)
                    inputDoc.setField(SOLRFieldName, value);
            }
        }
    }

    float boost = 1.0f;
    inputDoc.setDocumentBoost(boost);
    return inputDoc;
}

From source file:com.digitalpebble.behemoth.solr.SOLRWriter.java

License:Apache License

protected SolrInputDocument convertToSOLR(BehemothDocument doc) {
    final SolrInputDocument inputDoc = new SolrInputDocument();
    // map from a Behemoth document to a SOLR one
    // the field names below should be modified
    // to match the SOLR schema
    inputDoc.setField("id", doc.getUrl());
    inputDoc.setField("text", doc.getText());

    LOG.debug("Adding field : id\t" + doc.getUrl());

    // Rely on the field mapping to handle this, or the dynamic
    // fields/*ww  w .j av a2s .  co  m*/
    MapWritable metadata = doc.getMetadata();
    if (includeMetadata && metadata != null) {
        for (Entry<Writable, Writable> entry : metadata.entrySet()) {
            if (useMetadataPrefix) {
                String key = metadataPrefix + entry.getKey().toString();
                inputDoc.addField(key, entry.getValue().toString());
            } else {
                inputDoc.addField(entry.getKey().toString(), entry.getValue().toString());
            }
        }
    }
    // iterate on the annotations of interest and
    // create a new field for each one
    // it is advised NOT to set frequent annotation types
    // such as token as this would generate a stupidly large
    // number of fields which won't be used by SOLR for
    // tokenizing anyway.
    // what you can do though is to concatenate the token values
    // to form a new content string separated by spaces

    // iterate on the annotations
    if (includeAnnotations) {
        Iterator<Annotation> iterator = doc.getAnnotations().iterator();
        while (iterator.hasNext()) {
            Annotation current = iterator.next();
            // check whether it belongs to a type we'd like to send to SOLR
            Map<String, String> featureField = fieldMapping.get(current.getType());
            // special case of all annotations
            if (featureField == null && !includeAllAnnotations) {
                continue;
            }
            if (!includeAllAnnotations) {
                // iterate on the expected features
                for (String targetFeature : featureField.keySet()) {
                    String SOLRFieldName = featureField.get(targetFeature);
                    String value = null;
                    // special case for covering text
                    if ("*".equals(targetFeature)) {
                        value = doc.getText().substring((int) current.getStart(), (int) current.getEnd());
                    }
                    // get the value for the feature
                    else {
                        value = current.getFeatures().get(targetFeature);
                    }
                    LOG.debug("Adding field : " + SOLRFieldName + "\t" + value);
                    // skip if no value has been found
                    if (value != null)
                        inputDoc.addField(SOLRFieldName, value);
                }
            } else {
                for (Entry<String, String> e : current.getFeatures().entrySet()) {
                    inputDoc.addField(annotationPrefix + current.getType() + "." + e.getKey(), e.getValue());
                }
            }
        }
    }

    float boost = 1.0f;
    inputDoc.setDocumentBoost(boost);
    return inputDoc;
}

From source file:com.digitalpebble.storm.crawler.solr.persistence.StatusUpdaterBolt.java

License:Apache License

@Override
public void store(String url, Status status, Metadata metadata, Date nextFetch) throws Exception {

    SolrInputDocument doc = new SolrInputDocument();

    doc.setField("url", url);

    doc.setField("host", URLUtil.getHost(url));

    doc.setField("status", status);

    doc.setField("metadata", metadata.toString());
    doc.setField("nextFetchDate", nextFetch);

    connection.getClient().add(doc);//from  w w w .j  a  va  2 s  .  c om
}

From source file:com.digitalpebble.stormcrawler.solr.persistence.StatusUpdaterBolt.java

License:Apache License

@Override
public void store(String url, Status status, Metadata metadata, Date nextFetch) throws Exception {

    SolrInputDocument doc = new SolrInputDocument();

    doc.setField("url", url);

    doc.setField("host", URLUtil.getHost(url));

    doc.setField("status", status);

    Iterator<String> keyIterator = metadata.keySet().iterator();
    while (keyIterator.hasNext()) {
        String key = keyIterator.next();
        String[] values = metadata.getValues(key);
        doc.setField(String.format("%s.%s", mdPrefix, key), values);
    }/*from   ww  w.j  av a 2  s  .  co m*/

    doc.setField("nextFetchDate", nextFetch);

    connection.getClient().add(doc);
}

From source file:com.doculibre.constellio.services.SolrServicesImpl.java

License:Open Source License

public static void main(String[] args) {

    try {/*from  www  .j  av  a 2s  .  co m*/
        SolrCoreContext.init();
        // RecordCollection collection=new RecordCollection();
        // collection.setName("test");
        // SolrServer solrServer=SolrCoreContext.getSolrServer(collection);
        // SolrQuery query=new SolrQuery();
        // query.setQuery("doculibre");
        // // query.set("enableElevation", "true");
        // query.setFields("doc_url,[elevated]");
        // query.set("shards.qt", "/elevate");
        // query.setRequestHandler("/elevate");
        // QueryResponse queryResponse=solrServer.query(query);
        // long numFound = queryResponse.getResults().getNumFound();
        // System.out.println(numFound);

        // byte[] data =
        // SolrCoreContext.getSolrZkClient().getData(ZkController.CONFIGS_ZKNODE
        // + "/" +
        // "test" + "/" + "schema.xml", null, null, true);
        // File myTempFile = Files.createTempFile("config_",
        // ".tmp").toFile();
        // FileOutputStream outputStream = new FileOutputStream(myTempFile);
        // outputStream.write(data);
        // outputStream.close();
        // System.out.println(myTempFile.getAbsolutePath());
        // CloudSolrServer mainSolrServer = new
        // CloudSolrServer("localhost:9983");
        // mainSolrServer.setZkClientTimeout(3000);
        // mainSolrServer.setZkConnectTimeout(3000);
        // SolrCoreContext.init();
        // RecordCollection collection = new RecordCollection();
        // collection.setName("test");
        // SolrServer solrServer =
        // SolrCoreContext.getSolrServer(collection);
        // SolrQuery query = new SolrQuery();
        // // query.setRequestHandler("/admin/luke");
        // // query.setParam("show", "schema");
        // query.setParam(CommonParams.Q, "*:*");
        // query.setParam(CommonParams.ROWS, "0");
        // try {
        // QueryResponse queryResponse = solrServer.query(query);
        // long numFound = queryResponse.getResults().getNumFound();
        // System.out.println(numFound);
        // } catch (SolrServerException e) {
        // throw new RuntimeException(e);
        // }

        // ModifiableSolrParams params = new ModifiableSolrParams();
        // params.set(CommonParams.QT, "/admin/collections");
        // params.set(CommonParams.ACTION, "RELOAD");
        // params.set("name", "bbbb");
        // QueryResponse response=mainSolrServer.query(params);
        // System.out.println(response.getResponseHeader());
        // HttpSolrServer solrServer=new
        // HttpSolrServer("http://192.168.88.1:8983/solr/bbbb");
        // ModifiableSolrParams solrParams = new ModifiableSolrParams();
        // solrParams.set("collectionName", "bbbb");
        // solrParams.set("q", "doc_url:*");
        // solrParams.set("start", 0);
        // solrParams.set("rows", 10);
        // QueryResponse response = solrServer.query(solrParams);
        // System.out.println(response.getResults().size());

        // CloudSolrServer
        // cloudSolrServer=(CloudSolrServer)SolrCoreContext.getMainSolrServer();
        // cloudSolrServer.setDefaultCollection("tttt");
        // ModifiableSolrParams params = new ModifiableSolrParams();
        // params.set(CommonParams.QT, "/admin/collections");
        // params.set(CommonParams.ACTION, "RELOAD");
        // params.set("name", "bbbb");
        // QueryResponse response=cloudSolrServer.query(params);
        // System.out.println(response.getResponseHeader().toString());
        // // Thread.sleep(1000);
        // ModifiableSolrParams solrParams = new ModifiableSolrParams();
        // solrParams.set("collectionName", "bbbb");
        // solrParams.set("q", "doc_url:*");
        // solrParams.set("start", 0);
        // solrParams.set("rows", 10);
        // response = cloudSolrServer.query(solrParams);
        // System.out.println(response.getResults().size());

        HttpSolrServer solrServer = new HttpSolrServer(
                "http://ec2-54-204-186-243.compute-1.amazonaws.com:8983/solr/gouv");
        SolrInputDocument doc = new SolrInputDocument();
        doc.setField("doc_uniqueKey", "rouleroule");
        doc.setField("ministereOrganisme", "testministereOrganisme");
        doc.setField("sousCollection", "testsousCollection");
        solrServer.add(doc);

        solrServer.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // try {
    // SolrZkClient zkClient = SolrCoreContext.getSolrZkClient();
    // byte[] data = zkClient.getData(ZkController.CONFIGS_ZKNODE + "/" +
    // "abc" + "/" + "schema.xml", null, null, true);
    // File myTempFile = Files.createTempFile("config_", ".tmp").toFile();
    // FileOutputStream outputStream = new FileOutputStream(myTempFile);
    // outputStream.write(data);
    // outputStream.close();
    // System.out.println(myTempFile.getAbsolutePath());
    // } catch (KeeperException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } catch (InterruptedException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } catch (IOException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }

    // ModifiableSolrParams params = new ModifiableSolrParams();
    // params.set(CommonParams.QT, "/replication");
    // params.set("command", "details");
    //
    //
    // CloudSolrServer cloudSolrServer =
    // (CloudSolrServer)SolrCoreContext.getMainSolrServer();
    // ClusterState
    // clusterState=cloudSolrServer.getZkStateReader().getClusterState();
    // StringBuffer finalSize=new StringBuffer();
    // for(Slice slice:clusterState.getActiveSlices("abc")){
    // Replica replica=clusterState.getLeader("abc", slice.getName());
    // System.out.println(slice.getName()+"!!!"+replica);
    // StringUtils.substringBefore(replica.getNodeName(), "_");
    // HttpSolrServer solrServer= new
    // HttpSolrServer("http://"+StringUtils.substringBefore(replica.getNodeName(),
    // "_")+"/solr"+"/abc");
    // try {
    // QueryResponse response = solrServer.query(params);
    // finalSize.append(((NamedList)response.getResponse().get("details")).get("indexSize")+",");
    // } catch (SolrServerException e) {
    // e.printStackTrace();
    // }
    // }
    // System.out.println(finalSize);

    // SolrCoreContext.init();
    // SolrServicesImpl impl = new SolrServicesImpl();
    // RecordCollection rc = new RecordCollection();
    // rc.setName("abc");
    // String collectionName = rc.getName();
    // try {
    // SolrZkClient zkClient = SolrCoreContext.getSolrZkClient();
    // byte[] data = zkClient.getData(ZkController.CONFIGS_ZKNODE + "/" +
    // collectionName + "/schema.xml", null, null, true);
    // Document solrconfigXmlDocument = new SAXReader().read(new
    // ByteArrayInputStream(data));
    //
    // Element rootElement = solrconfigXmlDocument.getRootElement();
    // rootElement.addAttribute("name", collectionName);
    //
    // OutputFormat format = OutputFormat.createPrettyPrint();
    // ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    // XMLWriter writer = new XMLWriter(outputStream, format);
    // writer.write("");
    // writer.close();
    //
    // zkClient.setData(ZkController.CONFIGS_ZKNODE + "/" + collectionName +
    // "/schema.xml", outputStream.toByteArray(), true);
    //
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    // impl.ensureCore(rc);

    // try {
    // SolrZkClient zkClient = SolrCoreContext.getSolrZkClient();
    // File myTempDir = Files.createTempDirectory("constellio_").toFile();
    // ZkController.downloadConfigDir(zkClient,"adsf", myTempDir);
    // System.out.println(myTempDir.getAbsolutePath());

    // CloudSolrServer solrServer = new
    // CloudSolrServer("localhost:9983");
    // solrServer.setDefaultCollection("abc");

    // SolrServicesImpl impl = new SolrServicesImpl();
    // RecordCollection collection = new RecordCollection();
    // collection.setName("def");
    // SolrZkClient zkClient = SolrCoreContext.getSolrZkClient();
    // File myTempDir =
    // Files.createTempDirectory("constellio_").toFile();
    // ZkController.downloadConfigDir(zkClient, "abc", myTempDir);
    // // we do not change the "name" value in schema.xml, it is not
    // // very important
    // ZkController.uploadConfigDir(zkClient, myTempDir, "def");
    // FileUtils.deleteDirectory(myTempDir);
    //
    //
    // ModifiableSolrParams params = new ModifiableSolrParams();
    // params.set(CommonParams.QT, "/admin/collections");
    // params.set(CommonParams.ACTION, "CREATE");
    // params.set("name", "abc");
    // params.set("numShards", 2);
    // SolrCoreContext.getMainSolrServer().query(params);
    // Thread.sleep(10000);
    // SolrCoreContext.initCores();

    // HttpSolrServer solrServer = (HttpSolrServer)
    // SolrCoreContext.getSolrServer(collection);
    // ModifiableSolrParams solrParams = new ModifiableSolrParams();
    // solrParams.set("collectionName", "def");
    // solrParams.set("q", "*:*");
    // solrParams.set("start", 0);
    // solrParams.set("rows", 10);
    // QueryResponse response = solrServer.query(solrParams);
    // System.out.println(response.getResults().size());

    // SolrInputDocument doc = new SolrInputDocument();
    // int count = 20000;
    // while (count < 25000) {
    // doc.setField("id", count++);
    // solrServer.add(doc);
    // if (count % 10 == 0)
    // solrServer.commit();
    // }
    // } catch (Exception e) {
    // e.printStackTrace();
    // }

    // HttpSolrServer solrServer =new
    // HttpSolrServer("http://localhost:8900/solr");
    // CoreAdminRequest adminRequest = new CoreAdminRequest();
    // adminRequest.setAction(CoreAdminAction.STATUS);
    // CoreAdminResponse adminResponse = adminRequest.process(solrServer);
    // NamedList<NamedList<Object>> coreStatus = adminResponse
    // .getCoreStatus();
    // for (Object core : coreStatus) {
    // System.out.println(core);
    // }
    // }catch(Exception e){
    // e.printStackTrace();
    // }
    // try {
    // CloudSolrServer solrServer=new CloudSolrServer("localhost:9983");
    // solrServer.setDefaultCollection("myCollection4");

    // SolrZkClient zkClient=SolrCoreContext.getSolrZkClient();
    // try {
    // File myTempDir = Files.createTempDirectory("constellio_").toFile();
    // ZkController.downloadConfigDir(zkClient,
    // SolrCoreContext.getDefaultCoreName(), myTempDir);
    // ZkController.uploadConfigDir(zkClient, myTempDir, "myCollection");
    // } catch (Exception e) {
    // e.printStackTrace();
    // }

}