예제 #1
0
        /**
         * Connect to searchd server and run current search query.
         *
         * @param query     query string
         * @param index     index name(s) to query. May contain anything-separated
         *                  list of index names, or "*" which means to query all indexes.
         * @return          {@link SphinxResult} object
         *
         * @throws SphinxException on invalid parameters
         */
        public SphinxResult Query(string query, string index)
        {
            //MyAssert(_requestQueries == null || _requestQueries.Count == 0, "AddQuery() and Query() can not be combined; use RunQueries() instead");

            AddQuery(query, index);
            SphinxResult[] results = RunQueries();
            if (results == null || results.Length < 1)
            {
                return(null); /* probably network error; error message should be already filled */
            }

            SphinxResult res = results[0];

            _warning = res.warning;
            _error   = res.error;
            return(res);
        }
예제 #2
0
        private SphinxResult[] ParseResponse(byte[] response)
        {
            if (response == null)
            {
                return(null);
            }

            /* parse response */
            SphinxResult[] results = new SphinxResult[_requestQueries.Count];

            BinaryReader br = new BinaryReader(new MemoryStream(response));

            /* read schema */
            int ires;

            try
            {
                for (ires = 0; ires < _requestQueries.Count; ires++)
                {
                    SphinxResult res = new SphinxResult();
                    results[ires] = res;

                    int status = ReadInt32(br);
                    res.setStatus(status);
                    if (status != SEARCHD_OK)
                    {
                        string message = ReadUtf8(br);
                        if (status == SEARCHD_WARNING)
                        {
                            res.warning = message;
                        }
                        else
                        {
                            res.error = message;
                            continue;
                        }
                    }

                    /* read fields */
                    int nfields = ReadInt32(br);
                    res.fields = new string[nfields];
                    //int pos = 0;
                    for (int i = 0; i < nfields; i++)
                    {
                        res.fields[i] = ReadUtf8(br);
                    }

                    /* read arrts */
                    int nattrs = ReadInt32(br);
                    res.attrTypes = new int[nattrs];
                    res.attrNames = new string[nattrs];
                    for (int i = 0; i < nattrs; i++)
                    {
                        string AttrName = ReadUtf8(br);
                        int    AttrType = ReadInt32(br);
                        res.attrNames[i] = AttrName;
                        res.attrTypes[i] = AttrType;
                    }

                    /* read match count */
                    int count = ReadInt32(br);
                    int id64  = ReadInt32(br);
                    res.matches = new SphinxMatch[count];
                    for (int matchesNo = 0; matchesNo < count; matchesNo++)
                    {
                        SphinxMatch docInfo;
                        docInfo = new SphinxMatch(
                            (id64 == 0) ? ReadUInt32(br) : ReadInt64(br),
                            ReadInt32(br));

                        /* read matches */
                        for (int attrNumber = 0; attrNumber < res.attrTypes.Length; attrNumber++)
                        {
                            string attrName = res.attrNames[attrNumber];
                            int    type     = res.attrTypes[attrNumber];

                            /* handle bigint */
                            if (type == SPH_ATTR_BIGINT)
                            {
                                docInfo.attrValues.Add(ReadInt64(br));
                                continue;
                            }

                            /* handle floats */
                            if (type == SPH_ATTR_FLOAT)
                            {
                                docInfo.attrValues.Add(ReadFloat(br));
                                //docInfo.attrValues.add ( attrNumber, bw.ReadDouble  ) );
                                //throw new NotImplementedException("we don't read floats yet");
                                continue;
                            }

                            /* handle everything else as unsigned ints */
                            long val = ReadUInt32(br);
                            if ((type & SPH_ATTR_MULTI) != 0)
                            {
                                long[] vals = new long[(int)val];
                                for (int k = 0; k < val; k++)
                                {
                                    vals[k] = ReadUInt32(br);
                                }

                                docInfo.attrValues.Add(vals);
                            }
                            else
                            {
                                docInfo.attrValues.Add(val);
                            }
                        }
                        res.matches[matchesNo] = docInfo;
                    }

                    res.total      = ReadInt32(br);
                    res.totalFound = ReadInt32(br);
                    res.time       = ReadInt32(br) / 1000.0f; /*  format is %.3f */
                    int wordsCount = ReadInt32(br);

                    //res.words = new SphinxWordInfo[ReadInt32(bw)];
                    //for (int i = 0; i < res.words.Length; i++)
                    //    res.words[i] = new SphinxWordInfo(ReadUtf8(bw), ReadUInt32(bw), ReadUInt32(bw));
                }

                br.Close();
                return(results);
            }
            catch (IOException e)
            {
                //MyAssert(false, "unable to parse response: " + e.Message);
                return(null);
            }
        }