Пример #1
0
  /**
   * Connect to searchd server and update given attributes on given documents in given indexes.
   * Sample code that will set group_id=123 where id=1 and group_id=456 where id=3:
   *
   * <pre>
   * String[] attrs = new String[1];
   *
   * attrs[0] = "group_id";
   * long[][] values = new long[2][2];
   *
   * values[0] = new long[2]; values[0][0] = 1; values[0][1] = 123;
   * values[1] = new long[2]; values[1][0] = 3; values[1][1] = 456;
   *
   * int res = cl.UpdateAttributes ( "test1", attrs, values );
   * </pre>
   *
   * @param index index name(s) to update; might be distributed
   * @param attrs array with the names of the attributes to update
   * @param values array of updates; each long[] entry must contains document ID in the first
   *     element, and all new attribute values in the following ones
   * @param ignorenonexistent the flag whether to silently ignore non existent columns up update
   *     request
   * @return -1 on failure, amount of actually found and updated documents (might be 0) on success
   * @throws SphinxException on invalid parameters
   */
  public int UpdateAttributes(
      String index, String[] attrs, long[][] values, boolean ignorenonexistent)
      throws SphinxException {
    /* check args */
    myAssert(index != null && index.length() > 0, "no index name provided");
    myAssert(attrs != null && attrs.length > 0, "no attribute names provided");
    myAssert(values != null && values.length > 0, "no update entries provided");
    for (int i = 0; i < values.length; i++) {
      myAssert(values[i] != null, "update entry #" + i + " is null");
      myAssert(values[i].length == 1 + attrs.length, "update entry #" + i + " has wrong length");
    }

    /* build and send request */
    ByteArrayOutputStream reqBuf = new ByteArrayOutputStream();
    DataOutputStream req = new DataOutputStream(reqBuf);
    try {
      writeNetUTF8(req, index);

      req.writeInt(attrs.length);
      req.writeInt(ignorenonexistent ? 1 : 0);
      for (int i = 0; i < attrs.length; i++) {
        writeNetUTF8(req, attrs[i]);
        req.writeInt(0); // not MVA attr
      }

      req.writeInt(values.length);
      for (int i = 0; i < values.length; i++) {
        req.writeLong(values[i][0]); /* send docid as 64bit value */
        for (int j = 1; j < values[i].length; j++)
          req.writeInt(
              (int)
                  values[i][
                      j]); /* send values as 32bit values; FIXME! what happens when they are over 2^31? */
      }

      req.flush();

    } catch (Exception e) {
      _error = "internal error: failed to build request: " + e;
      return -1;
    }

    /* get and parse response */
    DataInputStream in = _DoRequest(SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, reqBuf);
    if (in == null) return -1;

    try {
      return in.readInt();
    } catch (Exception e) {
      _error = "incomplete reply";
      return -1;
    }
  }
Пример #2
0
  /**
   * Set integer range filter. Only match records if attribute value is beetwen min and max
   * (inclusive).
   */
  public void SetFilterRange(String attribute, long min, long max, boolean exclude)
      throws SphinxException {
    myAssert(min <= max, "min must be less or equal to max");
    try {
      writeNetUTF8(_filters, attribute);
      _filters.writeInt(SPH_FILTER_RANGE);
      _filters.writeLong(min);
      _filters.writeLong(max);
      _filters.writeInt(exclude ? 1 : 0);

    } catch (Exception e) {
      myAssert(false, "IOException: " + e.getMessage());
    }
    _filterCount++;
  }
Пример #3
0
  /** Set values filter. Only match records where attribute value is in given set. */
  public void SetFilter(String attribute, long[] values, boolean exclude) throws SphinxException {
    myAssert(values != null && values.length > 0, "values array must not be null or empty");
    myAssert(
        attribute != null && attribute.length() > 0, "attribute name must not be null or empty");

    try {
      writeNetUTF8(_filters, attribute);
      _filters.writeInt(SPH_FILTER_VALUES);
      _filters.writeInt(values.length);
      for (int i = 0; i < values.length; i++) _filters.writeLong(values[i]);
      _filters.writeInt(exclude ? 1 : 0);

    } catch (Exception e) {
      myAssert(false, "IOException: " + e.getMessage());
    }
    _filterCount++;
  }
Пример #4
0
  /** Add new query with current settings to current search request. */
  public int AddQuery(String query, String index, String comment) throws SphinxException {
    ByteArrayOutputStream req = new ByteArrayOutputStream();

    /* build request */
    try {
      DataOutputStream out = new DataOutputStream(req);
      out.writeInt(_offset);
      out.writeInt(_limit);
      out.writeInt(_mode);
      out.writeInt(_ranker);
      if (_ranker == SPH_RANK_EXPR) {
        writeNetUTF8(out, _rankexpr);
      }
      out.writeInt(_sort);
      writeNetUTF8(out, _sortby);
      writeNetUTF8(out, query);
      int weightLen = _weights != null ? _weights.length : 0;

      out.writeInt(weightLen);
      if (_weights != null) {
        for (int i = 0; i < _weights.length; i++) out.writeInt(_weights[i]);
      }

      writeNetUTF8(out, index);
      out.writeInt(0);
      out.writeInt(_minId);
      out.writeInt(_maxId);

      /* filters */
      out.writeInt(_filterCount);
      out.write(_rawFilters.toByteArray());

      /* group-by, max matches, sort-by-group flag */
      out.writeInt(_groupFunc);
      writeNetUTF8(out, _groupBy);
      out.writeInt(_maxMatches);
      writeNetUTF8(out, _groupSort);

      out.writeInt(_cutoff);
      out.writeInt(_retrycount);
      out.writeInt(_retrydelay);

      writeNetUTF8(out, _groupDistinct);

      /* anchor point */
      if (_latitudeAttr == null
          || _latitudeAttr.length() == 0
          || _longitudeAttr == null
          || _longitudeAttr.length() == 0) {
        out.writeInt(0);
      } else {
        out.writeInt(1);
        writeNetUTF8(out, _latitudeAttr);
        writeNetUTF8(out, _longitudeAttr);
        out.writeFloat(_latitude);
        out.writeFloat(_longitude);
      }

      /* per-index weights */
      out.writeInt(_indexWeights.size());
      for (Iterator e = _indexWeights.keySet().iterator(); e.hasNext(); ) {
        String indexName = (String) e.next();
        Integer weight = (Integer) _indexWeights.get(indexName);
        writeNetUTF8(out, indexName);
        out.writeInt(weight.intValue());
      }

      /* max query time */
      out.writeInt(_maxQueryTime);

      /* per-field weights */
      out.writeInt(_fieldWeights.size());
      for (Iterator e = _fieldWeights.keySet().iterator(); e.hasNext(); ) {
        String field = (String) e.next();
        Integer weight = (Integer) _fieldWeights.get(field);
        writeNetUTF8(out, field);
        out.writeInt(weight.intValue());
      }

      /* comment */
      writeNetUTF8(out, comment);

      /* overrides */
      out.writeInt(_overrideTypes.size());
      for (Iterator e = _overrideTypes.keySet().iterator(); e.hasNext(); ) {
        String attr = (String) e.next();
        Integer type = (Integer) _overrideTypes.get(attr);
        Map values = (Map) _overrideValues.get(attr);

        writeNetUTF8(out, attr);
        out.writeInt(type.intValue());
        out.writeInt(values.size());

        for (Iterator e2 = values.keySet().iterator(); e2.hasNext(); ) {
          Long id = (Long) e2.next();
          out.writeLong(id.longValue());
          switch (type.intValue()) {
            case SPH_ATTR_FLOAT:
              out.writeFloat(((Float) values.get(id)).floatValue());
              break;
            case SPH_ATTR_BIGINT:
              out.writeLong(((Long) values.get(id)).longValue());
              break;
            default:
              out.writeInt(((Integer) values.get(id)).intValue());
              break;
          }
        }
      }

      /* select-list */
      writeNetUTF8(out, _select);

      /* done! */
      out.flush();
      int qIndex = _reqs.size();
      _reqs.add(qIndex, req.toByteArray());
      return qIndex;

    } catch (Exception e) {
      myAssert(false, "error in AddQuery(): " + e + ": " + e.getMessage());

    } finally {
      try {
        _filters.close();
        _rawFilters.close();
      } catch (IOException e) {
        myAssert(false, "error in AddQuery(): " + e + ": " + e.getMessage());
      }
    }
    return -1;
  }