Package org.apache.solr.common

Examples of org.apache.solr.common.SolrInputDocument


    }
   
    @Override
    public void processAdd(AddUpdateCommand cmd) throws IOException {
      if(fields != null){
        SolrInputDocument solrInputDocument = cmd.getSolrInputDocument();
        List<Object> uniqList = new ArrayList<Object>();
        for (String field : fields) {
          uniqList.clear();
          Collection<Object> col = solrInputDocument.getFieldValues(field);
          if (col != null) {
            for (Object o : col) {
              if(!uniqList.contains(o))
                uniqList.add(o);
            }
            solrInputDocument.remove(field);
            for (Object o : uniqList) {
              solrInputDocument.addField(field, o);
            }
          }   
        }
      }
      super.processAdd(cmd);
View Full Code Here


    }

    @Override
    public void processAdd(AddUpdateCommand cmd) throws IOException {
      if (enabled) {
        SolrInputDocument doc = cmd.getSolrInputDocument();
        if (sigFields == null || sigFields.size() == 0) {
          Collection<String> docFields = doc.getFieldNames();
          sigFields = new ArrayList<String>(docFields.size());
          sigFields.addAll(docFields);
          Collections.sort(sigFields);
        }

        Signature sig = (Signature) req.getCore().getResourceLoader().newInstance(signatureClass);
        sig.init(params);

        for (String field : sigFields) {
          SolrInputField f = doc.getField(field);
          if (f != null) {
            sig.add(field);
            Object o = f.getValue();
            if (o instanceof String) {
              sig.add((String)o);
            } else if (o instanceof Collection) {
              for (Object oo : (Collection)o) {
                if (oo instanceof String) {
                  sig.add((String)oo);
                }
              }
            }
          }
        }

        byte[] signature = sig.getSignature();
        char[] arr = new char[signature.length<<1];
        for (int i=0; i<signature.length; i++) {
          int b = signature[i];
          int idx = i<<1;
          arr[idx]= StrUtils.HEX_DIGITS[(b >> 4) & 0xf];
          arr[idx+1]= StrUtils.HEX_DIGITS[b & 0xf];
        }
        String sigString = new String(arr);
        doc.addField(signatureField, sigString);

        if (overwriteDupes) {
          cmd.updateTerm = signatureTerm.createTerm(sigString);
        }
View Full Code Here

          }
        }
      }
      if (docIterator != null) {
        while (docIterator.hasNext()) {
          SolrInputDocument doc = docIterator.next();
          if (doc != null) {
            ClientUtils.writeXML(doc, writer);
          }
        }
      }
View Full Code Here

        case XMLStreamConstants.START_ELEMENT: {
          String currTag = parser.getLocalName();
          if ("doc".equals(currTag)) {
            log.trace("Tokenizing doc...");

            SolrInputDocument doc = readDoc(parser);
            SchemaField uniq = schema.getUniqueKeyField();
            NamedList<NamedList<NamedList<Object>>> theTokens = new SimpleOrderedMap<NamedList<NamedList<Object>>>();
            result.add(doc.getFieldValue(uniq.getName()).toString(), theTokens);
            for (String name : doc.getFieldNames()) {
              FieldType ft = schema.getFieldType(name);
              Analyzer analyzer = ft.getAnalyzer();
              Collection<Object> vals = doc.getFieldValues(name);
              for (Object val : vals) {
                Reader reader = new StringReader(val.toString());
                TokenStream tstream = analyzer.tokenStream(name, reader);
                NamedList<NamedList<Object>> tokens = getTokens(tstream);
                theTokens.add(name, tokens);
View Full Code Here

    }
    return tokens;
  }

  SolrInputDocument readDoc(XMLStreamReader parser) throws XMLStreamException {
    SolrInputDocument doc = new SolrInputDocument();

    StringBuilder text = new StringBuilder();
    String name = null;
    String attrName = "";
    float boost = 1.0f;
    boolean isNull = false;
    while (true) {
      int event = parser.next();
      switch (event) {
        // Add everything to the text
        case XMLStreamConstants.SPACE:
        case XMLStreamConstants.CDATA:
        case XMLStreamConstants.CHARACTERS:
          text.append(parser.getText());
          break;

        case XMLStreamConstants.END_ELEMENT:
          if ("doc".equals(parser.getLocalName())) {
            return doc;
          } else if ("field".equals(parser.getLocalName())) {
            if (!isNull) {
              doc.addField(name, text.toString(), boost);
              boost = 1.0f;
            }
          }
          break;
View Full Code Here

              else {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "invalid key: "+v + " ["+ parser.getPosition()+"]" );
              }
            }
            else if( obj instanceof SolrInputDocument ) {
              SolrInputDocument doc = (SolrInputDocument)obj;
              SolrInputField f = doc.get( v );
              if( f == null ) {
                f = new SolrInputField( v );
                doc.put( f.getName(), f );
              }
              stack.push( f );
            }
            else {
              throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "hymmm ["+ parser.getPosition()+"]" );
            }
          }
          else {
            addValToField(stack, parser.getString(), inArray, parser);
          }
          break;

        case JSONParser.LONG:
        case JSONParser.NUMBER:
        case JSONParser.BIGNUMBER:
          addValToField(stack, parser.getNumberChars().toString(), inArray, parser);
          break;
         
        case JSONParser.BOOLEAN:
          addValToField(stack, parser.getBoolean(),inArray, parser);
          break;

        case JSONParser.NULL:
          parser.getNull();
          /*** if we wanted to remove the field from the document now...
          if (!inArray) {
            Object o = stack.peek();
            // if null was only value in the field, then remove the field
            if (o instanceof SolrInputField) {
              SolrInputField sif = (SolrInputField)o;
              if (sif.getValueCount() == 0) {
                sdoc.remove(sif.getName());
              }
            }
          }
          ***/

          addValToField(stack, null, inArray, parser);
          break;

        case JSONParser.OBJECT_START:
          if( stack.isEmpty() ) {
            stack.push( new SolrInputDocument() );
          }
          else {
            obj = stack.peek();
            if( obj instanceof SolrInputField ) {
              // should alreay be pushed...
View Full Code Here

   * @return a SolrInputDocument with the same fields and values as the
   *   SolrDocument.  All boosts are 1.0f
   */
  public static SolrInputDocument toSolrInputDocument( SolrDocument d )
  {
    SolrInputDocument doc = new SolrInputDocument();
    for( String name : d.getFieldNames() ) {
      doc.addField( name, d.getFieldValue(name), 1.0f );
    }
    return doc;
  }
View Full Code Here

    List<DocField> fields = getDocFields( obj.getClass() );
    if( fields.isEmpty() ) {
      throw new RuntimeException( "class: "+obj.getClass()+" does not define any fields." );
    }
   
    SolrInputDocument doc = new SolrInputDocument();
  for (DocField field : fields) {
    if (field.dynamicFieldNamePatternMatcher != null
        && field.get(obj) != null && field.isContainedInMap) {
      Map<String, Object> mapValue = (HashMap<String, Object>) field
          .get(obj);

      for (Map.Entry<String, Object> e : mapValue.entrySet()) {
        doc.setField( e.getKey(), e.getValue(), 1.0f);
      }
    } else {
      doc.setField(field.name, field.get(obj), 1.0f);
    }
  }
    return doc;
  }
View Full Code Here

          }
          case XMLStreamConstants.START_ELEMENT: {
            String currTag = parser.getLocalName();
            if ("doc".equals(currTag)) {
              log.trace("Reading doc...");
              SolrInputDocument document = readDocument(parser, req.getSchema());
              request.addDocument(document);
            }
            break;
          }
        }
View Full Code Here

   * @return The read document.
   *
   * @throws XMLStreamException When reading of the document fails.
   */
  SolrInputDocument readDocument(XMLStreamReader reader, IndexSchema schema) throws XMLStreamException {
    SolrInputDocument doc = new SolrInputDocument();

    String uniqueKeyField = schema.getUniqueKeyField().getName();

    StringBuilder text = new StringBuilder();
    String fieldName = null;
    boolean hasId = false;

    while (true) {
      int event = reader.next();
      switch (event) {
        // Add everything to the text
        case XMLStreamConstants.SPACE:
        case XMLStreamConstants.CDATA:
        case XMLStreamConstants.CHARACTERS:
          text.append(reader.getText());
          break;

        case XMLStreamConstants.END_ELEMENT:
          if ("doc".equals(reader.getLocalName())) {
            if (!hasId) {
              throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                      "All documents must contain a unique key value: '" + doc.toString() + "'");
            }
            return doc;
          } else if ("field".equals(reader.getLocalName())) {
            doc.addField(fieldName, text.toString(), DEFAULT_BOOST);
            if (uniqueKeyField.equals(fieldName)) {
              hasId = true;
            }
          }
          break;
View Full Code Here

TOP

Related Classes of org.apache.solr.common.SolrInputDocument

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.