All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.tair.tairsearch.TairSearchCluster Maven / Gradle / Ivy

Go to download

Aliyun Tair Redis client for Java Copyright (C) Alibaba Cloud Computing All rights reserved. 版权所有 (C)阿里云计算有限公司 http://www.aliyun.com

There is a newer version: 3.0.9
Show newest version
package com.aliyun.tair.tairsearch;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.aliyun.tair.ModuleCommand;
import com.aliyun.tair.tairsearch.params.TFTAddDocParams;
import com.aliyun.tair.tairsearch.params.TFTDelDocParams;
import com.aliyun.tair.tairsearch.params.TFTScanParams;
import com.aliyun.tair.util.JoinParameters;
import redis.clients.jedis.BuilderFactory;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.util.SafeEncoder;

import static redis.clients.jedis.Protocol.toByteArray;

public class TairSearchCluster {
    private JedisCluster jc;

    public TairSearchCluster(JedisCluster jc) {
        this.jc = jc;
    }

    public String tftmappingindex(String key, String request) {
        return tftmappingindex(SafeEncoder.encode(key), SafeEncoder.encode(request));
    }

    public String tftmappingindex(byte[] key, byte[] request) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTMAPPINGINDEX, key, request);
        return BuilderFactory.STRING.build(obj);
    }

    public String tftcreateindex(String key, String request) {
        return tftcreateindex(SafeEncoder.encode(key), SafeEncoder.encode(request));
    }

    public String tftcreateindex(byte[] key, byte[] request) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTCREATEINDEX, key, request);
        return BuilderFactory.STRING.build(obj);
    }

    public String tftupdateindex(String key, String request) {
        return tftupdateindex(SafeEncoder.encode(key), SafeEncoder.encode(request));
    }

    public String tftupdateindex(byte[] key, byte[] request) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTUPDATEINDEX, key, request);
        return BuilderFactory.STRING.build(obj);
    }

    public String tftgetindexmappings(String key) {
        return tftgetindexmappings(SafeEncoder.encode(key));
    }

    public String tftgetindexmappings(byte[] key) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTGETINDEX, key, SafeEncoder.encode("mappings"));
        return BuilderFactory.STRING.build(obj);
    }

    public String tftadddoc(String key, String request) {
        return tftadddoc(SafeEncoder.encode(key), SafeEncoder.encode(request));
    }

    public String tftadddoc(byte[] key, byte[] request) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTADDDOC, key, request);
        return BuilderFactory.STRING.build(obj);
    }

    public String tftadddoc(String key, String request, String docId) {
        return tftadddoc(SafeEncoder.encode(key), SafeEncoder.encode(request), SafeEncoder.encode(docId));
    }

    public String tftadddoc(byte[] key, byte[] request, byte[] docId) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTADDDOC, key, request, SafeEncoder.encode("WITH_ID"), docId);
        return BuilderFactory.STRING.build(obj);
    }

    public String tftmadddoc(String key, Map docs) {
        TFTAddDocParams params = new TFTAddDocParams();
        Object obj = jc.sendCommand(SafeEncoder.encode(key), ModuleCommand.TFTMADDDOC, params.getByteParams(key, docs));
        return BuilderFactory.STRING.build(obj);
    }

    public String tftmadddoc(byte[] key, Map docs) {
        TFTAddDocParams params = new TFTAddDocParams();
        Object obj = jc.sendCommand(key, ModuleCommand.TFTMADDDOC, params.getByteParams(key, docs));
        return BuilderFactory.STRING.build(obj);
    }

    @Deprecated
    public String tftupdatedoc(String key, String docId, String docContent) {
        return tftupdatedoc(SafeEncoder.encode(key), SafeEncoder.encode(docId), SafeEncoder.encode(docContent));
    }

    @Deprecated
    public String tftupdatedoc(byte[] key, byte[] docId, byte[] docContent) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTUPDATEDOC, key, docId, docContent);
        return BuilderFactory.STRING.build(obj);
    }

    public String tftupdatedocfield(String index, String docId, String docContent) {
        return tftupdatedoc(SafeEncoder.encode(index), SafeEncoder.encode(docId), SafeEncoder.encode(docContent));
    }

    public String tftupdatedocfield(byte[] index, byte[] docId, byte[] docContent) {
        Object obj = jc.sendCommand(index, ModuleCommand.TFTUPDATEDOCFIELD, index, docId, docContent);
        return BuilderFactory.STRING.build(obj);
    }

    public Long tftincrlongdocfield(String index, String docId, final String field, final long value) {
        return tftincrlongdocfield(SafeEncoder.encode(index), SafeEncoder.encode(docId), SafeEncoder.encode(field), value);
    }

    public Long tftincrlongdocfield(byte[] index, byte[] docId, byte[] field, long value) {
        Object obj = jc.sendCommand(index, ModuleCommand.TFTINCRLONGDOCFIELD, index, docId, field, toByteArray(value));
        return BuilderFactory.LONG.build(obj);
    }

    public Double tftincrfloatdocfield(String index, String docId, final String field, final double value) {
        return tftincrfloatdocfield(SafeEncoder.encode(index), SafeEncoder.encode(docId), SafeEncoder.encode(field), value);
    }

    public Double tftincrfloatdocfield(byte[] index, byte[] docId, byte[] field, double value) {
        Object obj = jc.sendCommand(index, ModuleCommand.TFTINCRFLOATDOCFIELD, index, docId, field, toByteArray(value));
        return BuilderFactory.DOUBLE.build(obj);
    }

    public Long tftdeldocfield(String index, String docId, final String... field) {
        return tftdeldocfield(SafeEncoder.encode(index), SafeEncoder.encode(docId), SafeEncoder.encodeMany(field));
    }

    public Long tftdeldocfield(byte[] index, byte[] docId, byte[]... field) {
        Object obj = jc.sendCommand(index, ModuleCommand.TFTDELDOCFIELD, JoinParameters.joinParameters(index, docId, field));
        return BuilderFactory.LONG.build(obj);
    }

    public String tftgetdoc(String key, String docId) {
        return tftgetdoc(SafeEncoder.encode(key), SafeEncoder.encode(docId));
    }

    public String tftgetdoc(byte[] key, byte[] docId) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTGETDOC, key, docId);
        return BuilderFactory.STRING.build(obj);
    }

    public String tftgetdoc(String key, String docId, String request) {
        return tftgetdoc(SafeEncoder.encode(key), SafeEncoder.encode(docId), SafeEncoder.encode(request));
    }

    public String tftgetdoc(byte[] key, byte[] docId, byte[] request) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTGETDOC, key, docId, request);
        return BuilderFactory.STRING.build(obj);
    }

    public String tftdeldoc(String key,  String... docId) {
        TFTDelDocParams params = new TFTDelDocParams();
        Object obj = jc.sendCommand(SafeEncoder.encode(key), ModuleCommand.TFTDELDOC, params.getByteParams(key, docId));
        return BuilderFactory.STRING.build(obj);
    }

    public String tftdeldoc(byte[] key, byte[]... docId) {
        TFTDelDocParams params = new TFTDelDocParams();
        Object obj = jc.sendCommand(key, ModuleCommand.TFTDELDOC, params.getByteParams(key, docId));
        return BuilderFactory.STRING.build(obj);
    }

    public String tftdelall(String index) {
        return tftdelall(SafeEncoder.encode(index));
    }

    public String tftdelall(byte[] key) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTDELALL, key);
        return BuilderFactory.STRING.build(obj);
    }

    public String tftsearch(String key, String request) {
        return tftsearch(SafeEncoder.encode(key), SafeEncoder.encode(request));
    }

    public String tftsearch(byte[] key, byte[] request) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTSEARCH, key, request);
        return BuilderFactory.STRING.build(obj);
    }

    public String tftsearch(String key, String request, boolean use_cache) {
        return tftsearch(SafeEncoder.encode(key), SafeEncoder.encode(request), use_cache);
    }

    public String tftsearch(byte[] key, byte[] request, boolean use_cache) {
        Object obj;
        if (use_cache) {
            obj = jc.sendCommand(key, ModuleCommand.TFTSEARCH, key, request, SafeEncoder.encode("use_cache"));
        } else {
            obj = jc.sendCommand(key, ModuleCommand.TFTSEARCH, key, request);
        }
        return BuilderFactory.STRING.build(obj);
    }

    public Long tftexists(String key, String docId) {
        return tftexists(SafeEncoder.encode(key), SafeEncoder.encode(docId));
    }

    public Long tftexists(byte[] key, byte[] docId) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTEXISTS, key, docId);
        return BuilderFactory.LONG.build(obj);
    }

    public Long tftdocnum(String key) {
        return tftdocnum(SafeEncoder.encode(key));
    }

    public Long tftdocnum(byte[] key) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTDOCNUM, key);
        return BuilderFactory.LONG.build(obj);
    }

    public ScanResult tftscandocid(String key, String cursor) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTSCANDOCID, key, cursor);
        List result = (List)obj;
        String newcursor = new String((byte[]) result.get(0));
        List results = new ArrayList<>();
        List rawResults = (List) result.get(1);
        for (byte[] bs : rawResults) {
            results.add(SafeEncoder.encode(bs));
        }
        return new ScanResult<>(newcursor, results);
    }

    public ScanResult tftscandocid(byte[] key, byte[] cursor) {
        Object obj = jc.sendCommand(key, ModuleCommand.TFTSCANDOCID, key, cursor);
        List result = (List)obj;
        byte[] newcursor = (byte[]) result.get(0);
        List rawResults = (List) result.get(1);
        return new ScanResult<>(newcursor, rawResults);
    }

    public ScanResult tftscandocid(String key, String cursor, final TFTScanParams params) {
        final List args = new ArrayList();
        args.add(SafeEncoder.encode(key));
        args.add(SafeEncoder.encode(cursor));
        args.addAll(params.getParams());
        Object obj = jc.sendCommand(SafeEncoder.encode(key), ModuleCommand.TFTSCANDOCID, args.toArray(new byte[args.size()][]));
        List result = (List)obj;
        String newcursor = new String((byte[]) result.get(0));
        List results = new ArrayList<>();
        List rawResults = (List) result.get(1);
        for (byte[] bs : rawResults) {
            results.add(SafeEncoder.encode(bs));
        }
        return new ScanResult<>(newcursor, results);
    }

    public ScanResult tftscandocid(byte[] key, byte[] cursor, final TFTScanParams params) {
        final List args = new ArrayList();
        args.add(key);
        args.add(cursor);
        args.addAll(params.getParams());
        Object obj = jc.sendCommand(key, ModuleCommand.TFTSCANDOCID, args.toArray(new byte[args.size()][]));
        List result = (List)obj;
        byte[] newcursor = (byte[]) result.get(0);
        List rawResults = (List) result.get(1);
        return new ScanResult<>(newcursor, rawResults);
    }
}