/// <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); } }
/// <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); } }
/// <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()); } }
/// <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); } }
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()); } }
/// <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); } }