/// <summary>
        /// Add new boolean values to override real attribute values stored in Sphinx index by document IDs.
        /// </summary>
        /// <param name="name">Attribute name</param>
        /// <param name="values">Document id and override values map to override</param>
        /// <returns>New attribute override object</returns>
        public AttributeOverrideBoolean Add(string name, IDictionary <long, bool> values)
        {
            AttributeOverrideBoolean item = new AttributeOverrideBoolean(name, values);

            Add(item);
            return(item);
        }
        /// <summary>
        /// Add new boolean value to override real attribute value stored in Sphinx index by document ID.
        /// </summary>
        /// <param name="name">Attribute name</param>
        /// <param name="id">Document ID</param>
        /// <param name="value">New value</param>
        /// <returns>New attribute override object</returns>
        public AttributeOverrideBoolean Add(string name, long id, bool value)
        {
            Dictionary <long, bool> dict = new Dictionary <long, bool>();

            dict.Add(id, value);
            AttributeOverrideBoolean item = new AttributeOverrideBoolean(name, dict);

            Add(item);
            return(item);
        }
Example #3
0
        private string DoSearch()
        {
            // set hostname and port defaults
            using (TcpConnection connection = new PersistentTcpConnection(Host, Port))
            {

                SearchCommand search = new SearchCommand(connection);
                SearchQuery query = new SearchQuery(QueryString);
                // Sphinx indexes
                query.Indexes.UnionWith(Indexes);
                // select fields clause
                query.Select = SelectFields;
                // match type
                query.MatchMode = MatchMode;
                // ranking
                query.RankingMode = RankingMode;
                // comment
                query.Comment = Comment;
                // sorting
                query.SortMode = SortMode;
                query.SortBy = SortClause;
                // limits
                query.Limit = query.MaxMatches = MaxMatches;
                // document id filtering
                query.MinDocumentId = MinDocumentId;
                query.MaxDocumentId = MaxDocumentId;
                // grouping
                query.GroupFunc = GroupFunc;
                query.GroupBy = GroupBy;
                query.GroupSort = GroupSortBy;
                query.GroupDistinct = GroupDistinct;

                //query.AttributeFilters.Add(new AttributeFilterRangeDateTime("PublishDate", PublicationStartDate.Value, PublicationEndDate.Value, false));

                // index weights
                foreach (NameValuePair item in _indexWeights)
                {
                    if (!query.IndexWeights.ContainsKey(item.Name))
                        query.IndexWeights.Add(item.Name, item.Value);
                }

                // fields weights
                foreach (NameValuePair item in _fieldWeights)
                {
                    if (!query.FieldWeights.ContainsKey(item.Name)) 
                        query.FieldWeights.Add(item.Name, item.Value);
                }

                // attribute overrides
                foreach (AttributeOverrideMapping item in _attributeOverrides)
                {
                    AttributeOverrideBase attr;
                    AttributeType type = (AttributeType) item.Type;
                    if (!query.AttributeOverrides.Contains(item.Name))
                    {
                        switch (type)
                        {
                            case AttributeType.Integer:
                                Dictionary<long, int> ints = new Dictionary<long, int>();
                                ints.Add(item.DocumentId, Convert.ToInt32(item.Value));
                                attr = new AttributeOverrideInt32(item.Name, ints);
                                break;
                            case AttributeType.Bigint:
                                Dictionary<long, long> longs = new Dictionary<long, long>();
                                longs.Add(item.DocumentId, Convert.ToInt64(item.Value));
                                attr = new AttributeOverrideInt64(item.Name, longs);
                                break;
                            case AttributeType.Boolean:
                                Dictionary<long, bool> bools = new Dictionary<long, bool>();
                                bools.Add(item.DocumentId, Convert.ToBoolean(item.Value));
                                attr = new AttributeOverrideBoolean(item.Name, bools);
                                break;
                            case AttributeType.Float:
                                Dictionary<long, float> floats = new Dictionary<long, float>();
                                floats.Add(item.DocumentId, float.Parse(item.Value.ToString()));
                                attr = new AttributeOverrideFloat(item.Name, floats);
                                break;
                            case AttributeType.Ordinal:
                                Dictionary<long, int> ordinals = new Dictionary<long, int>();
                                ordinals.Add(item.DocumentId, Convert.ToInt32(item.Value));
                                attr = new AttributeOverrideOrdinal(item.Name, ordinals);
                                break;
                            case AttributeType.Timestamp:
                                Dictionary<long, DateTime> timestamps = new Dictionary<long, DateTime>();
                                timestamps.Add(item.DocumentId, Convert.ToDateTime(item.Value));
                                attr = new AttributeOverrideDateTime(item.Name, timestamps);
                                break;
                            default:
                                throw new InvalidOperationException("Unknown attribute type");
                        }
                        query.AttributeOverrides.Add(attr);
                    }
                    else
                    {
                        attr = query.AttributeOverrides[item.Name];
                        if (type != attr.AttributeType) throw new InvalidOperationException("Attribute type mismatch");
                        switch (type)
                        {
                            case AttributeType.Integer:
                                ((AttributeOverrideInt32) attr).Values.Add(item.DocumentId, Convert.ToInt32(item.Value));
                                break;
                            case AttributeType.Bigint:
                                ((AttributeOverrideInt64)attr).Values.Add(item.DocumentId, Convert.ToInt64(item.Value));
                                break;
                            case AttributeType.Boolean:
                                ((AttributeOverrideBoolean)attr).Values.Add(item.DocumentId, Convert.ToBoolean(item.Value));
                                break;
                            case AttributeType.Float:
                                ((AttributeOverrideFloat)attr).Values.Add(item.DocumentId, float.Parse(item.Value.ToString()));
                                break;
                            case AttributeType.Ordinal:
                                ((AttributeOverrideOrdinal)attr).Values.Add(item.DocumentId, Convert.ToInt32(item.Value));
                                break;
                            case AttributeType.Timestamp:
                                ((AttributeOverrideDateTime)attr).Values.Add(item.DocumentId, Convert.ToDateTime(item.Value));
                                break;
                            default:
                                throw new InvalidOperationException("Unknown attribute type");
                        }
                    }
                }

                // add new query
                search.QueryList.Add(query);

                // run the query and get the results
                string output = "";
                try
                {
                    search.Execute();
                }
                catch (SphinxException ex)
                {
                    output = "<h2 style='color: red;'>Error is occured:<h2>" + ex.Message;
                    return output;
                }
                if (search.Result.Status == CommandStatus.Warning)
                {
                    output = "<h2 style='color: olive;'>Warnings: </h2><ul>";
                    foreach (var s in search.Result.Warnings)
                    {
                        output += "<li>" + s + "</li>";
                    }
                    output += "</ul>";
                }
                foreach (SearchQueryResult res in search.Result.QueryResults)
                {
                    output += ParseResult(res);
                }
                return output;
            }
        }
Example #4
0
        private string DoSearch()
        {
            // set hostname and port defaults
            using (TcpConnection connection = new PersistentTcpConnection(Host, Port))
            {
                SearchCommand search = new SearchCommand(connection);
                SearchQuery   query  = new SearchQuery(QueryString);
                // Sphinx indexes
                query.Indexes.UnionWith(Indexes);
                // select fields clause
                query.Select = SelectFields;
                // match type
                query.MatchMode = MatchMode;
                // ranking
                query.RankingMode = RankingMode;
                // comment
                query.Comment = Comment;
                // sorting
                query.SortMode = SortMode;
                query.SortBy   = SortClause;
                // limits
                query.Limit = query.MaxMatches = MaxMatches;
                // document id filtering
                query.MinDocumentId = MinDocumentId;
                query.MaxDocumentId = MaxDocumentId;
                // grouping
                query.GroupFunc     = GroupFunc;
                query.GroupBy       = GroupBy;
                query.GroupSort     = GroupSortBy;
                query.GroupDistinct = GroupDistinct;

                // index weights
                foreach (NameValuePair item in _indexWeights)
                {
                    if (!query.IndexWeights.ContainsKey(item.Name))
                    {
                        query.IndexWeights.Add(item.Name, item.Value);
                    }
                }

                // fields weights
                foreach (NameValuePair item in _fieldWeights)
                {
                    if (!query.FieldWeights.ContainsKey(item.Name))
                    {
                        query.FieldWeights.Add(item.Name, item.Value);
                    }
                }

                // attribute overrides
                foreach (AttributeOverrideMapping item in _attributeOverrides)
                {
                    AttributeOverrideBase attr;
                    AttributeType         type = (AttributeType)item.Type;
                    if (!query.AttributeOverrides.Contains(item.Name))
                    {
                        switch (type)
                        {
                        case AttributeType.Integer:
                            Dictionary <long, int> ints = new Dictionary <long, int>();
                            ints.Add(item.DocumentId, Convert.ToInt32(item.Value));
                            attr = new AttributeOverrideInt32(item.Name, ints);
                            break;

                        case AttributeType.Bigint:
                            Dictionary <long, long> longs = new Dictionary <long, long>();
                            longs.Add(item.DocumentId, Convert.ToInt64(item.Value));
                            attr = new AttributeOverrideInt64(item.Name, longs);
                            break;

                        case AttributeType.Boolean:
                            Dictionary <long, bool> bools = new Dictionary <long, bool>();
                            bools.Add(item.DocumentId, Convert.ToBoolean(item.Value));
                            attr = new AttributeOverrideBoolean(item.Name, bools);
                            break;

                        case AttributeType.Float:
                            Dictionary <long, float> floats = new Dictionary <long, float>();
                            floats.Add(item.DocumentId, float.Parse(item.Value.ToString()));
                            attr = new AttributeOverrideFloat(item.Name, floats);
                            break;

                        case AttributeType.Ordinal:
                            Dictionary <long, int> ordinals = new Dictionary <long, int>();
                            ordinals.Add(item.DocumentId, Convert.ToInt32(item.Value));
                            attr = new AttributeOverrideOrdinal(item.Name, ordinals);
                            break;

                        case AttributeType.Timestamp:
                            Dictionary <long, DateTime> timestamps = new Dictionary <long, DateTime>();
                            timestamps.Add(item.DocumentId, Convert.ToDateTime(item.Value));
                            attr = new AttributeOverrideDateTime(item.Name, timestamps);
                            break;

                        default:
                            throw new InvalidOperationException("Unknown attribute type");
                        }
                        query.AttributeOverrides.Add(attr);
                    }
                    else
                    {
                        attr = query.AttributeOverrides[item.Name];
                        if (type != attr.AttributeType)
                        {
                            throw new InvalidOperationException("Attribute type mismatch");
                        }
                        switch (type)
                        {
                        case AttributeType.Integer:
                            ((AttributeOverrideInt32)attr).Values.Add(item.DocumentId, Convert.ToInt32(item.Value));
                            break;

                        case AttributeType.Bigint:
                            ((AttributeOverrideInt64)attr).Values.Add(item.DocumentId, Convert.ToInt64(item.Value));
                            break;

                        case AttributeType.Boolean:
                            ((AttributeOverrideBoolean)attr).Values.Add(item.DocumentId, Convert.ToBoolean(item.Value));
                            break;

                        case AttributeType.Float:
                            ((AttributeOverrideFloat)attr).Values.Add(item.DocumentId, float.Parse(item.Value.ToString()));
                            break;

                        case AttributeType.Ordinal:
                            ((AttributeOverrideOrdinal)attr).Values.Add(item.DocumentId, Convert.ToInt32(item.Value));
                            break;

                        case AttributeType.Timestamp:
                            ((AttributeOverrideDateTime)attr).Values.Add(item.DocumentId, Convert.ToDateTime(item.Value));
                            break;

                        default:
                            throw new InvalidOperationException("Unknown attribute type");
                        }
                    }
                }

                // add new query
                search.QueryList.Add(query);

                // run the query and get the results
                string output = "";
                try
                {
                    search.Execute();
                }
                catch (SphinxException ex)
                {
                    output = "<h2 style='color: red;'>Error is occured:<h2>" + ex.Message;
                    return(output);
                }
                if (search.Result.Status == CommandStatus.Warning)
                {
                    output = "<h2 style='color: olive;'>Warnings: </h2><ul>";
                    foreach (var s in search.Result.Warnings)
                    {
                        output += "<li>" + s + "</li>";
                    }
                    output += "</ul>";
                }
                foreach (SearchQueryResult res in search.Result.QueryResults)
                {
                    output += ParseResult(res);
                }
                return(output);
            }
        }