示例#1
0
        public ISphinxDocument GetDocumentById(DocumentId docId)
        {
            Condition.Requires(docId, "id").IsNotNull();
            Condition.Requires(docId.EntityId, "id.EntityId").IsEqualTo(id);

            return CreateSphinxDocument();
        }
        public DefaultSphinxDocument(DocumentId documentId, string title, string body, string url, string protoName)
            : base(title, body, url)
        {
            Condition.Requires(documentId, "documentId").IsNotNull();

            this.documentId = documentId;
            this.protoName = protoName;
        }
示例#3
0
        public ISphinxDocument GetDocumentById(DocumentId id)
        {
            Condition.Requires(id, "id").IsNotNull();
            Condition.Requires(id.EntityId, "id.EntityId").IsEqualTo(GetId());
            Condition.Requires(id.FieldsetId, "id.FieldsetId").IsNotNull("DocumentId must be compound");

            if (Meridian.Default.fieldsetsStore.Exists(id.FieldsetId.Value))
            {
                var entity = CastToEntity();
                var fieldset = Meridian.Default.fieldsetsStore.Get(id.FieldsetId.Value);
                var info = BuildFieldSetInfo(fieldset, entity);
                if (info != null)
                    return BuildSphinxDocument(entity, info);
            }

            return null;
        }
示例#4
0
        private IEnumerable<SphinxDocumentAnnounce> GetSphinxSearchResults(string q, string[] indexes = null)
        {
            if (string.IsNullOrEmpty(q))
                return new SphinxDocumentAnnounce[0];

            using (ConnectionBase connection = new PersistentTcpConnection(MeridianMonitor.Default.SphinxHost, MeridianMonitor.Default.SphinxPort))
            {
                q = q.Replace(" -", "¦").Replace("-", " ").Replace("¦", " -").Replace("!", "\\!").Replace("?", "\\?").Replace("@", "\\@");

                if (q.LastIndexOf('-') == (q.Length - 1))
                {
                    q = q.Substring(0, q.LastIndexOf('-') - 1).Trim();
                }

                q = string.Join(" ", q.Split(' ').Select(s => s.Trim()));
                // Create new search query object and pass query text as argument
                SearchQuery searchQuery = new SearchQuery(q);
                // Set match mode to SPH_MATCH_EXTENDED2
                searchQuery.MatchMode = MatchMode.All;

                string[] protos = indexes ?? new string[]
                    {
                        typeof(hotels).Name,
                        //typeof(countries).Name,
                        typeof(deseases).Name,
                        typeof(cure_profiles).Name,
                        typeof(health_factors).Name,
                        //typeof(regions).Name,
                        //typeof(resort_zones).Name,
                        typeof(resorts).Name,
                        typeof(treatment_options).Name,
                        "static_pages",
                        typeof(dictionary).Name
                    };

                var byIndexResults = new Dictionary<string, List<SphinxDocumentAnnounce>>();
                foreach (var protoIndex in protos)
                {
                    searchQuery.Indexes.Add(protoIndex);
                    byIndexResults[protoIndex] = new List<SphinxDocumentAnnounce>();
                }

                searchQuery.Limit = 5000;
                SearchCommand searchCommand = new SearchCommand(connection);
                searchCommand.QueryList.Add(searchQuery);
                searchCommand.Execute();

                var pubResult = new List<SphinxDocumentAnnounce>();
                foreach (SearchQueryResult result in searchCommand.Result.QueryResults)
                {
                    foreach (var match in result.Matches)
                    {
                        var otype = match.AttributesValues["objecttype"].GetValue().ToString().Trim();
                        var entityId = Convert.ToInt64(match.AttributesValues["entityid"].GetValue());
                        var documentId = new DocumentId(entityId);
                        if (match.AttributesValues.Contains("fieldsetid"))
                            documentId = new DocumentId(entityId,
                                Convert.ToInt64(match.AttributesValues["fieldsetid"].GetValue()));

                        if (!Meridian.Default.Exists(otype, entityId))
                            continue;

                        var entity = Meridian.Default.GetAs<ISphinxExportableEntity>(otype, entityId);
                        var document = entity.GetDocumentById(documentId);
                        if (document != null)
                        {
                            var item = new SphinxDocumentAnnounce(document.GetTitle(), document.GetBody(), document.GetUrl());
                            pubResult.Add(item);
                            byIndexResults[otype].Add(item);
                        }
                    }
                }

                var itemsArray = pubResult.ToArray();

                var querySplit = q.Split(' ');
                foreach (var protoIndex in protos)
                {
                    var items = byIndexResults[protoIndex];

                    if (items.Count == 0)
                        continue;

                    BuildExcerptsCommand excerptsCommand = new BuildExcerptsCommand(connection,
                        items.Select(s => s.GetBody()), querySplit, protoIndex);
                    excerptsCommand.Execute();

                    var index = 0;
                    foreach (var result in excerptsCommand.Result.Excerpts)
                    {
                        var item = items[index++];
                        item.SetBody(result);
                    }
                }

                return itemsArray;
            }
        }