Java Code Examples for org.apache.solr.client.solrj.io.Tuple#get()

The following examples show how to use org.apache.solr.client.solrj.io.Tuple#get() . 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: UpdateStream.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked"})
private SolrInputDocument convertTupleToSolrDocument(Tuple tuple) {
  SolrInputDocument doc = new SolrInputDocument();
  for (Object field : tuple.getFields().keySet()) {

    if (! (field.equals(CommonParams.VERSION_FIELD) && pruneVersionField)) {
      Object value = tuple.get(field);
      if (value instanceof List) {
        addMultivaluedField(doc, (String)field, (List<Object>)value);
      } else {
        doc.addField((String)field, tuple.get(field));
      }
    }
  }
  log.debug("Tuple [{}] was converted into SolrInputDocument [{}].", tuple, doc);
  
  return doc;
}
 
Example 2
Source File: MeanMetric.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void update(Tuple tuple) {
  ++count;
  Object o = tuple.get(columnName);
  if(o instanceof Double) {
    Double d = (Double) o;
    doubleSum += d;
  } else if(o instanceof Float) {
    Float f = (Float) o;
    doubleSum += f.doubleValue();
  } else if(o instanceof Integer) {
    Integer i = (Integer)o;
    longSum += i.longValue();
  } else {
    Long l = (Long)o;
    longSum += l;
  }
}
 
Example 3
Source File: JDBCStreamTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected boolean assertOrderOf(List<Tuple> tuples, String fieldName, String... values) throws Exception {
  int i = 0;
  for(String val : values) {
    Tuple t = tuples.get(i);
    
    if(null == val){
      if(null != t.get(fieldName)){
        throw new Exception("Found value:"+(String)t.get(fieldName)+" expecting:null");
      }
    }
    else{
      String tip = (String)t.get(fieldName);
      if(!tip.equals(val)) {
        throw new Exception("Found value:"+tip+" expecting:"+val);
      }
    }
    ++i;
  }
  return true;
}
 
Example 4
Source File: JDBCStreamTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public boolean assertString(Tuple tuple, String fieldName, String expected) throws Exception {
  String actual = (String)tuple.get(fieldName);
  
  if( (null == expected && null != actual) ||
      (null != expected && null == actual) ||
      (null != expected && !expected.equals(actual))){
    throw new Exception("Longs not equal:"+expected+" : "+actual);
  }

  return true;
}
 
Example 5
Source File: Bucket.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public Object getBucketValue(Tuple tuple)
{
  Object o = tuple.get(bucketKey);
  if(o == null) {
    return NULL_VALUE;
  } else {
    return o;
  }
}
 
Example 6
Source File: JDBCStreamTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public boolean assertLong(Tuple tuple, String fieldName, long l) throws Exception {
  long lv = (long)tuple.get(fieldName);
  if(lv != l) {
    throw new Exception("Longs not equal:"+l+" : "+lv);
  }

  return true;
}
 
Example 7
Source File: JDBCStreamTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected boolean assertOrderOf(List<Tuple> tuples, String fieldName, int... values) throws Exception {
  int i = 0;
  for(int val : values) {
    Tuple t = tuples.get(i);
    Long tip = (Long)t.get(fieldName);
    if(tip.intValue() != val) {
      throw new Exception("Found value:"+tip.intValue()+" expecting:"+val);
    }
    ++i;
  }
  return true;
}
 
Example 8
Source File: StreamExpressionTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public boolean assertLong(Tuple tuple, String fieldName, long l) throws Exception {
  long lv = (long)tuple.get(fieldName);
  if(lv != l) {
    throw new Exception("Longs not equal:"+l+" : "+lv);
  }

  return true;
}
 
Example 9
Source File: ReplaceWithValueOperation.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private boolean matchesOriginal(Tuple tuple){
  Object value = tuple.get(fieldName);
  
  if(null == value){
    return null == original;
  }
  else if(null != original){
    return original.equals(value);
  }
  
  return false;    
}
 
Example 10
Source File: StreamExpressionTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public boolean assertString(Tuple tuple, String fieldName, String expected) throws Exception {
  String actual = (String)tuple.get(fieldName);

  if( (null == expected && null != actual) ||
      (null != expected && null == actual) ||
      (null != expected && !expected.equals(actual))){
    throw new Exception("Longs not equal:"+expected+" : "+actual);
  }

  return true;
}
 
Example 11
Source File: ConcatOperation.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void operate(Tuple tuple) {
  StringBuilder buf = new StringBuilder();
  for(String field : fields) {
    if(buf.length() > 0) {
      buf.append(delim);
    }
    Object value = tuple.get(field);
    if(null == value){ value = "null"; }
    buf.append(value);
  }

  tuple.put(as, buf.toString());
}
 
Example 12
Source File: GetValueEvaluator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Object doWork(Object value1, Object value2) throws IOException {

  if(value1 instanceof Tuple) {
    Tuple tuple = (Tuple)value1;
    String key = (String)value2;
    key = key.replace("\"", "");
    return tuple.get(key);
  } else {
    throw new IOException("The getValue function expects a Tuple as the first parameter");
  }
}
 
Example 13
Source File: TestSQLHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public boolean assertLong(Tuple tuple, String fieldName, long l) throws Exception {
  long lv = (long)tuple.get(fieldName);
  if(lv != l) {
    throw new Exception("Longs not equal:"+l+" : "+lv);
  }

  return true;
}
 
Example 14
Source File: SelectWithEvaluatorsTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected boolean assertGroupOrder(Tuple tuple, int... ids) throws Exception {
  List<?> group = (List<?>)tuple.get("tuples");
  int i=0;
  for(int val : ids) {
    Map<?,?> t = (Map<?,?>)group.get(i);
    Long tip = (Long)t.get("id");
    if(tip.intValue() != val) {
      throw new Exception("Found value:"+tip.intValue()+" expecting:"+val);
    }
    ++i;
  }
  return true;
}
 
Example 15
Source File: GraphExpressionTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public boolean assertLong(Tuple tuple, String fieldName, long l) throws Exception {
  long lv = (long)tuple.get(fieldName);
  if(lv != l) {
    throw new Exception("Longs not equal:"+l+" : "+lv);
  }

  return true;
}
 
Example 16
Source File: CrossCollectionJoinQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private DocSet getDocSet() throws IOException {
  SolrClientCache solrClientCache = searcher.getCore().getCoreContainer().getSolrClientCache();
  TupleStream solrStream;
  if (zkHost != null || solrUrl == null) {
    solrStream = createCloudSolrStream(solrClientCache);
  } else {
    solrStream = createSolrStream();
  }

  FieldType fieldType = searcher.getSchema().getFieldType(toField);
  JoinKeyCollector collector;
  if (fieldType.isPointField()) {
    collector = new PointJoinKeyCollector(searcher);
  } else {
    Terms terms = searcher.getSlowAtomicReader().terms(toField);
    if (terms == null) {
      return DocSet.empty();
    }
    collector = new TermsJoinKeyCollector(fieldType, terms, searcher);
  }

  try {
    solrStream.open();
    while (true) {
      Tuple tuple = solrStream.read();
      if (tuple.EXCEPTION) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, tuple.getException());
      }
      if (tuple.EOF) {
        break;
      }

      Object value = tuple.get(fromField);
      collector.collect(value);
    }
  } catch (IOException e) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
  } finally {
    solrStream.close();
  }

  return collector.getDocSet();
}
 
Example 17
Source File: LatLonVectorsEvaluator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public Object doWork(Object... objects) throws IOException {

  if (objects.length == 1) {
    //Just docs
    if(!(objects[0] instanceof List)) {
      throw new IOException("The latlonVectors function expects a list of Tuples as a parameter.");
    } else {
      @SuppressWarnings({"rawtypes"})
      List list = (List)objects[0];
      if(list.size() > 0) {
        Object o = list.get(0);
        if(!(o instanceof Tuple)) {
          throw new IOException("The latlonVectors function expects a list of Tuples as a parameter.");
        }
      } else {
        throw new IOException("Empty list was passed as a parameter to termVectors function.");
      }
    }

    @SuppressWarnings({"unchecked"})
    List<Tuple> tuples = (List<Tuple>) objects[0];

    double[][] locationVectors = new double[tuples.size()][2];
    List<String> features = new ArrayList<>();
    features.add("lat");
    features.add("lon");

    List<String> rowLabels = new ArrayList<>();

    for(int i=0; i< tuples.size(); i++) {
      Tuple tuple = tuples.get(i);
      String value = tuple.getString(field);
      String[] latLong = null;
      if(value.contains(",")) {
        latLong = value.split(",");
      } else {
        latLong = value.split(" ");
      }

      locationVectors[i][0] = Double.parseDouble(latLong[0].trim());
      locationVectors[i][1] = Double.parseDouble(latLong[1].trim());
      if(tuple.get("id") != null) {
        rowLabels.add(tuple.get("id").toString());
      }
    }

    Matrix matrix = new Matrix(locationVectors);
    matrix.setColumnLabels(features);
    matrix.setRowLabels(rowLabels);
    return matrix;
  } else {
    throw new IOException("The latlonVectors function takes a single positional parameter.");
  }
}
 
Example 18
Source File: GraphMLResponseWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void write(Writer writer, SolrQueryRequest req, SolrQueryResponse res) throws IOException {

    Exception e1 = res.getException();
    if(e1 != null) {
      e1.printStackTrace(new PrintWriter(writer));
      return;
    }

    TupleStream stream =  (TupleStream)req.getContext().get("stream");

    if(stream instanceof GraphHandler.DummyErrorStream) {
      GraphHandler.DummyErrorStream d = (GraphHandler.DummyErrorStream)stream;
      Exception e = d.getException();
      e.printStackTrace(new PrintWriter(writer));
      return;
    }


    Traversal traversal = (Traversal)req.getContext().get("traversal");
    PrintWriter printWriter = new PrintWriter(writer);

    try {

      stream.open();

      Tuple tuple = null;

      int edgeCount = 0;

      printWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
      printWriter.println("<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" ");
      printWriter.println("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
      printWriter.print("xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns ");
      printWriter.println("http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">");

      printWriter.println("<graph id=\"G\" edgedefault=\"directed\">");

      while (true) {
        //Output the graph
        tuple = stream.read();
        if (tuple.EOF) {
          break;
        }

        String id = tuple.getString("node");

        if (traversal.isMultiCollection()) {
          id = tuple.getString("collection") + "." + id;
        }

        printWriter.write("<node id=\""+ xmlEscape(id)+"\"");

        List<String> outfields = new ArrayList<>();
        Iterator<Object> keys = tuple.getFields().keySet().iterator();
        while(keys.hasNext()) {
          String key = String.valueOf(keys.next());
          if(key.equals("node") || key.equals("ancestors") || key.equals("collection")) {
            continue;
          } else {
            outfields.add(key);
          }
        }

        if (outfields.size() > 0) {
          printWriter.println(">");
          for (String nodeAttribute : outfields) {
            Object o = tuple.get(nodeAttribute);
            if (o != null) {
              printWriter.println("<data key=\"" + xmlEscape(nodeAttribute) + "\">" + xmlEscape(o.toString()) + "</data>");
            }
          }
          printWriter.println("</node>");
        } else {
          printWriter.println("/>");
        }

        List<String> ancestors = tuple.getStrings("ancestors");

        if(ancestors != null) {
          for (String ancestor : ancestors) {
            ++edgeCount;
            printWriter.write("<edge id=\"" + edgeCount + "\" ");
            printWriter.write(" source=\"" + xmlEscape(ancestor) + "\" ");
            printWriter.println(" target=\"" + xmlEscape(id) + "\"/>");
          }
        }
      }

      printWriter.write("</graph></graphml>");
    } finally {
      stream.close();
    }
  }
 
Example 19
Source File: CountMetric.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void update(Tuple tuple) {
  if(isAllColumns() || tuple.get(columnName) != null) {
    ++count;
  }
}
 
Example 20
Source File: TextLogitStream.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked"})
public Tuple read() throws IOException {
  try {

    if(++iteration > maxIterations) {
      return Tuple.EOF();
    } else {

      if (this.idfs == null) {
        loadTerms();

        if (weights != null && terms.size() + 1 != weights.size()) {
          throw new IOException(String.format(Locale.ROOT,"invalid expression %s - the number of weights must be %d, found %d", terms.size()+1, weights.size()));
        }
      }

      List<List<Double>> allWeights = new ArrayList<>();
      this.evaluation = new ClassificationEvaluation();

      this.error = 0;
      for (Future<Tuple> logitCall : callShards(getShardUrls())) {

        Tuple tuple = logitCall.get();
        List<Double> shardWeights = (List<Double>) tuple.get("weights");
        allWeights.add(shardWeights);
        this.error += tuple.getDouble("error");
        @SuppressWarnings({"rawtypes"})
        Map shardEvaluation = (Map) tuple.get("evaluation");
        this.evaluation.addEvaluation(shardEvaluation);
      }

      this.weights = averageWeights(allWeights);
      @SuppressWarnings({"rawtypes"})
      Map map = new HashMap();
      map.put(ID, name+"_"+iteration);
      map.put("name_s", name);
      map.put("field_s", field);
      map.put("terms_ss", terms);
      map.put("iteration_i", iteration);

      if(weights != null) {
        map.put("weights_ds", weights);
      }

      map.put("error_d", error);
      evaluation.putToMap(map);
      map.put("alpha_d", this.learningRate);
      map.put("idfs_ds", this.idfs);

      if (iteration != 1) {
        if (lastError <= error) {
          this.learningRate *= 0.5;
        } else {
          this.learningRate *= 1.05;
        }
      }

      lastError = error;

      return new Tuple(map);
    }

  } catch(Exception e) {
    throw new IOException(e);
  }
}