コード例 #1
0
ファイル: SearchExtensions.java プロジェクト: anthonyu/gaelyk
  /**
   * Get a document field raw value or list of raw values.
   *
   * <pre><code>
   *      assert document.title = "Big bad wolf"
   *      assert document.keyword[0] == "wolf"
   *      assert document.keyword[1] == "red hook"
   * </code></pre>
   *
   * @param document the document
   * @param fieldName the field name
   * @return a raw value or a list of raw values if the field is multivalued
   */
  public static Object get(Document document, String fieldName) {
    @SuppressWarnings("unchecked")
    List<Field> fields = (List<Field>) DefaultGroovyMethods.collect(document.getFields(fieldName));

    switch (fields.size()) {
      case 0:
        if (document instanceof ScoredDocument) {
          List<Object> exps = new ArrayList<Object>();
          for (Field f : ((ScoredDocument) document).getExpressions()) {
            if (f.getName().equals(fieldName)) {
              exps.add(getFieldRawValue(f));
            }
          }

          if (exps.size() == 0) {
            return null;
          }
          if (exps.size() == 1) {
            return exps.get(0);
          }
          return exps;
        }
        return null;
      case 1:
        return getFieldRawValue(fields.get(0));
      default:
        List<Object> exps = new ArrayList<Object>();
        for (Field f : fields) {
          exps.add(getFieldRawValue(f));
        }
        return exps;
    }
  }
コード例 #2
0
  protected Document buildDocument(K id, Map<String, Object> fields) {
    String stringId = convert(id, String.class);
    Builder documentBuilder = Document.newBuilder();
    documentBuilder.setId(stringId);
    for (Map.Entry<String, Object> fieldData : fields.entrySet()) {
      Object value = fieldData.getValue();
      String fieldName = fieldData.getKey();
      for (Object object : getCollectionValues(value)) {
        try {
          Field field = buildField(metadata, fieldName, object);
          documentBuilder.addField(field);
        } catch (Exception e) {
          throw new SearchException(
              e,
              "Failed to add field '%s' with value '%s' to document with id '%s': %s",
              fieldName,
              value.toString(),
              id,
              e.getMessage());
        }
      }
    }

    return documentBuilder.build();
  }
コード例 #3
0
  protected int removeAll() {
    int count = 0;
    Index index = getIndex();
    GetRequest request = GetRequest.newBuilder().setReturningIdsOnly(true).setLimit(200).build();
    GetResponse<Document> response = index.getRange(request);

    // can only delete documents in blocks of 200 so we need to iterate until they're all gone
    while (!response.getResults().isEmpty()) {
      List<String> ids = new ArrayList<String>();
      for (Document document : response) {
        ids.add(document.getId());
      }
      index.delete(ids);
      count += ids.size();
      response = index.getRange(request);
    }
    return count;
  }
コード例 #4
0
ファイル: Comentario.java プロジェクト: dextra/dextranet
 @Override
 public Document toDocument() {
   Document document =
       Document.newBuilder()
           .setId(id)
           .addField(Field.newBuilder().setName(ComentarioFields.id.name()).setText(id))
           .addField(
               Field.newBuilder().setName(ComentarioFields.conteudo.name()).setText(conteudo))
           .addField(Field.newBuilder().setName(ComentarioFields.usuario.name()).setText(usuario))
           .build();
   return document;
 }
コード例 #5
0
 // Index the geo location
 private void indexProduct(DProduct dProduct) {
   if (null != dProduct.getLocation()) {
     GeoPoint geoPoint =
         new GeoPoint(dProduct.getLocation().getLatitude(), dProduct.getLocation().getLongitude());
     int averageRating =
         (null != dProduct.getRatingAverage()) ? dProduct.getRatingAverage().getRating() : -1;
     Document.Builder locationBuilder =
         Document.newBuilder()
             .setId(dProduct.getProductId())
             .addField(Field.newBuilder().setName("location").setGeoPoint(geoPoint))
             .addField(Field.newBuilder().setName("averageRating").setNumber(averageRating))
             .addField(Field.newBuilder().setName("likeCount").setNumber(dProduct.getLikeCount()))
             .addField(Field.newBuilder().setName("thumbsUp").setNumber(dProduct.getThumbsUp()));
     getLocationIndex().add(locationBuilder.build());
   }
 }