示例#1
0
        /// <summary>
        /// Returns a list of statuses
        /// </summary>
        /// <returns></returns>
        public List <string> Status()
        {
            SphinxBinaryWriter req_bw = new SphinxBinaryWriter(new MemoryStream(), Encoding);

            req_bw.WriteShort((short)SearchdCommand.SEARCHD_COMMAND_STATUS);
            req_bw.WriteShort((short)VerCommand.VER_COMMAND_STATUS);
            req_bw.WriteInt(4);
            req_bw.WriteInt(1);
            req_bw.Flush();

            using (SphinxConnection sc = GetConnection())
            {
                SphinxBinaryWriter client_bw = new SphinxBinaryWriter(sc.Stream, Encoding);
                client_bw.WriteStream(req_bw.Stream);

                SphinxBinaryReader res_br = new SphinxBinaryReader(ReadResponse(sc.Stream), Encoding);
                int           num_rows    = res_br.ReadInt();
                int           num_cols    = res_br.ReadInt();
                List <string> results     = new List <string>();
                for (int i = 0; i < num_rows; i++)
                {
                    for (int j = 0; j < num_cols; j++)
                    {
                        results.Add(res_br.ReadStr());
                    }
                }
                return(results);
            }
        }
示例#2
0
        /// <summary>
        /// Builds keywords.
        /// </summary>
        /// <param name="_query">The _query.</param>
        /// <param name="_index">The _index.</param>
        /// <param name="_hits">if set to <c>true</c> [_hits].</param>
        /// <returns></returns>
        public List <Keyword> BuildKeywords(string _query, string _index = "", bool _hits = true)
        {
            SphinxBinaryWriter req_bw = new SphinxBinaryWriter(new MemoryStream(), Encoding);

            req_bw.WriteShort((short)SearchdCommand.SEARCHD_COMMAND_KEYWORDS);
            req_bw.WriteShort((short)VerCommand.VER_COMMAND_KEYWORDS);
            req_bw.WriteInt(12 + _query.Length + _index.Length);
            req_bw.WriteStr(_query);
            req_bw.WriteStr(_index);
            req_bw.WriteInt(_hits ? 1 : 0);
            req_bw.Flush();

            using (SphinxConnection sc = GetConnection())
            {
                SphinxBinaryWriter client_bw = new SphinxBinaryWriter(sc.Stream, Encoding);
                client_bw.WriteStream(req_bw.Stream);

                SphinxBinaryReader res_br   = new SphinxBinaryReader(ReadResponse(sc.Stream), Encoding);
                int            num_keywords = res_br.ReadInt();
                List <Keyword> results      = new List <Keyword>();
                while (num_keywords-- > 0)
                {
                    Keyword kw = new Keyword();
                    kw.Tokenized  = res_br.ReadStr();
                    kw.Normalized = res_br.ReadStr();
                    if (_hits)
                    {
                        kw.NumDocs = res_br.ReadInt();
                        kw.NumHits = res_br.ReadInt();
                    }
                    results.Add(kw);
                }
                return(results);
            }
        }
示例#3
0
 internal void ReadFrom(SphinxBinaryReader _br, IEnumerable <Attribute> _attributes)
 {
     Weight = _br.ReadInt();
     foreach (Attribute attr in _attributes)
     {
         AttributeValue attr_val = new AttributeValue(attr);
         attr_val.ReadFrom(_br);
         Attributes.Add(attr.Name, attr_val);
     }
 }
示例#4
0
        internal void ReadFrom(SphinxBinaryReader _br)
        {
            Status = (SeachdStatusCodes)_br.ReadInt();

            if (Status != SeachdStatusCodes.SEARCHD_OK)
            {
                if (Status == SeachdStatusCodes.SEARCHD_WARNING)
                {
                    Warning = _br.ReadStr();
                }
                else
                {
                    Error = _br.ReadStr();
                    return;
                }
            }

            int count = _br.ReadInt(); // num_fields

            while (count-- > 0)
            {
                FieldList.Add(_br.ReadStr());
            }

            count = _br.ReadInt(); // num_attr
            while (count-- > 0)
            {
                AttributeList.Add(new Attribute(_br.ReadStr(), (AttributeTypes)_br.ReadInt()));
            }

            count = _br.ReadInt(); // num_matches
            bool id64 = _br.ReadInt() == 1;

            while (count-- > 0)
            {
                Match m = new Match((id64) ? _br.ReadLong() : _br.ReadInt());
                m.ReadFrom(_br, Attributes);
                MatcheList.Add(m);
            }

            Total      = _br.ReadInt();
            TotalFound = _br.ReadInt();
            TimeMsec   = _br.ReadInt();

            count = _br.ReadInt(); // num_words
            while (count-- > 0)
            {
                WordInfo wi = new WordInfo();
                wi.ReadFrom(_br);
                WordList.Add(wi);
            }
        }
        public SphinxConnection(String _host, int _port)
        {
            tcpclient.Connect(_host, _port);
            SphinxBinaryWriter bw = new SphinxBinaryWriter(tcpclient.GetStream());

            bw.WriteInt(1); // dummy write (Nagle)
            SphinxBinaryReader br = new SphinxBinaryReader(tcpclient.GetStream());

            if (br.ReadInt() < 1)
            {
                throw new SphinxClientException("Server version < 1");
            }
        }
示例#6
0
        /// <summary>
        /// Forces searchd to flush pending attribute updates to disk, and blocks until completion.
        /// </summary>
        /// <returns>Returns a non-negative internal "flush tag"</returns>
        public int FlushAttributes()
        {
            SphinxBinaryWriter req_bw = new SphinxBinaryWriter(new MemoryStream(), Encoding);

            req_bw.WriteShort((short)SearchdCommand.SEARCHD_COMMAND_FLUSHATTRS);
            req_bw.WriteShort((short)VerCommand.VER_COMMAND_FLUSHATTRS);
            req_bw.WriteInt(0);
            req_bw.Flush();

            using (SphinxConnection sc = GetConnection())
            {
                SphinxBinaryWriter client_bw = new SphinxBinaryWriter(sc.Stream, Encoding);
                client_bw.WriteStream(req_bw.Stream);

                SphinxBinaryReader res_br = new SphinxBinaryReader(ReadResponse(sc.Stream), Encoding);
                return(res_br.ReadInt());
            }
        }
示例#7
0
        /// <summary>
        /// Reads the response from a _tcpclient
        /// This method can set LastError or LastWarning
        /// </summary>
        /// <param name="_tcpclient">A connected TcpClient.</param>
        /// <returns></returns>
        private Stream ReadResponse(NetworkStream _stream)
        {
            LastError   = "";
            LastWarning = "";
            Stream ms = null;

            // read response header (8 bytes)
            SphinxBinaryReader client_br  = new SphinxBinaryReader(_stream, Encoding);
            SphinxBinaryReader res_br     = new SphinxBinaryReader(new MemoryStream(client_br.ReadBytes(8)), Encoding);
            SeachdStatusCodes  res_status = (SeachdStatusCodes)res_br.ReadShort();

            res_br.ReadShort();
            int res_len = res_br.ReadInt();

            if ((res_len < 0) || (res_len > MAX_PACKET_LEN))
            {
                throw new SphinxClientException(string.Format("response length out of bounds (len={0})", res_len));
            }

            // read response (res_len)
            res_br = new SphinxBinaryReader(new MemoryStream(client_br.ReadBytes(res_len)), Encoding);
            switch (res_status)
            {
            case SeachdStatusCodes.SEARCHD_OK:
                ms = res_br.Stream;
                break;

            case SeachdStatusCodes.SEARCHD_ERROR:
            case SeachdStatusCodes.SEARCHD_RETRY:
                LastError = res_br.ReadStr();
                throw new SphinxClientException(LastError);

            case SeachdStatusCodes.SEARCHD_WARNING:
                LastWarning = res_br.ReadStr();
                ms          = res_br.Stream;
                break;

            default:
                throw new SphinxClientException(string.Format("unknown status (status={0})", res_status));
            }

            return(ms);
        }
示例#8
0
        /// <summary>
        /// Runs all queries.
        /// </summary>
        /// <returns>a list of SphinxResult</returns>
        public List <Result> RunQueries()
        {
            SphinxBinaryWriter req_bw = new SphinxBinaryWriter(new MemoryStream(), Encoding);

            // prepare header
            req_bw.WriteShort((short)SearchdCommand.SEARCHD_COMMAND_SEARCH);
            req_bw.WriteShort((short)VerCommand.VER_COMMAND_SEARCH);
            int req_len = 8;

            foreach (Stream s in queries)
            {
                req_len += (int)s.Length;
            }
            req_bw.WriteInt(req_len);
            req_bw.WriteInt(0);
            req_bw.WriteInt(queries.Count);
            // prepare all queries
            foreach (Stream s in queries)
            {
                req_bw.WriteStream(s);
            }
            req_bw.Flush();

            using (SphinxConnection sc = GetConnection())
            {
                SphinxBinaryWriter client_bw = new SphinxBinaryWriter(sc.Stream, Encoding);
                client_bw.WriteStream(req_bw.Stream);

                SphinxBinaryReader res_br = new SphinxBinaryReader(ReadResponse(sc.Stream), Encoding);
                List <Result>      result = new List <Result>();
                for (int i = 0; i < queries.Count; i++)
                {
                    Result sr = new Result();
                    sr.ReadFrom(res_br);
                    result.Add(sr);
                }
                queries.Clear(); // clear all queries
                return(result);
            }
        }
示例#9
0
        public int UpdateAttributes(string _index, List <Attribute> _attrs, Dictionary <long, List <AttributeValue> > _values)
        {
            SphinxBinaryWriter req_bw = new SphinxBinaryWriter(new MemoryStream(), Encoding);

            req_bw.WriteShort((short)SearchdCommand.SEARCHD_COMMAND_UPDATE);
            req_bw.WriteShort((short)VerCommand.VER_COMMAND_UPDATE);
            req_bw.WriteInt(0); // request length
            req_bw.WriteStr(_index);
            req_bw.WriteInt(_attrs.Count);
            foreach (Attribute a in _attrs)
            {
                req_bw.WriteStr(a.Name);
                req_bw.WriteInt(a.isMVA ? 1 : 0);
            }
            req_bw.WriteInt(_values.Count);
            foreach (KeyValuePair <long, List <AttributeValue> > kv in _values)
            {
                req_bw.WriteLong(kv.Key);
                foreach (AttributeValue av in kv.Value)
                {
                    av.WriteTo(req_bw);
                }
            }
            req_bw.Flush();

            req_bw.Seek(4, SeekOrigin.Begin);               // move to the request length position
            req_bw.WriteInt((int)req_bw.Stream.Length - 8); // request length - 8 (fixed header)
            req_bw.Flush();

            using (SphinxConnection sc = GetConnection())
            {
                SphinxBinaryWriter client_bw = new SphinxBinaryWriter(sc.Stream, Encoding);
                client_bw.WriteStream(req_bw.Stream);

                SphinxBinaryReader res_br = new SphinxBinaryReader(ReadResponse(sc.Stream), Encoding);
                return(res_br.ReadInt());
            }
        }
示例#10
0
        /// <summary>
        /// Builds excerpts.
        /// </summary>
        /// <param name="_docs">The _docs.</param>
        /// <param name="_index">The _index.</param>
        /// <param name="_words">The _words.</param>
        /// <param name="_opts">The _opts.</param>
        /// <returns></returns>
        public List <string> BuildExcerpts(List <string> _docs, string _index, string _words, ExcerptOptions _opts)
        {
            SphinxBinaryWriter req_bw = new SphinxBinaryWriter(new MemoryStream(), Encoding);

            req_bw.WriteShort((short)SearchdCommand.SEARCHD_COMMAND_EXCERPT);
            req_bw.WriteShort((short)VerCommand.VER_COMMAND_EXCERPT);
            req_bw.WriteInt(0);                // request length
            req_bw.WriteInt(0);                // mode=0
            req_bw.WriteInt((int)_opts.Flags); // flags
            req_bw.WriteStr(_index);           // index
            req_bw.WriteStr(_words);           // words
            _opts.WriteTo(req_bw);             // ExcerptOptions
            // docs
            req_bw.WriteInt(_docs.Count);
            foreach (string d in _docs)
            {
                req_bw.WriteStr(d);
            }
            req_bw.Flush();

            req_bw.Seek(4, SeekOrigin.Begin);               // move to the request length position
            req_bw.WriteInt((int)req_bw.Stream.Length - 8); // request length - 8 (fixed header)
            req_bw.Flush();

            using (SphinxConnection sc = GetConnection())
            {
                SphinxBinaryWriter client_bw = new SphinxBinaryWriter(sc.Stream, Encoding);
                client_bw.WriteStream(req_bw.Stream);

                SphinxBinaryReader res_br  = new SphinxBinaryReader(ReadResponse(sc.Stream), Encoding);
                List <string>      results = new List <string>();
                for (int i = 0; i < _docs.Count; i++)
                {
                    results.Add(res_br.ReadStr());
                }
                return(results);
            }
        }
示例#11
0
 internal void ReadFrom(SphinxBinaryReader _br)
 {
     Word = _br.ReadStr();
     Docs = _br.ReadInt();
     Hits = _br.ReadInt();
 }