Ejemplo n.º 1
0
        private static ISearchDocuments CreateSearchDocuments(PanelContent pc)
        {
            if (pc == null || !pc.HasSubpanel("search-results"))
            {
                return(EmptySearchDocuments());
            }
            pc = pc.Subpanel("search-results");
            if (!pc.HasSubpanel("search-hits"))
            {
                return(EmptySearchDocuments());
            }
            pc = pc.Subpanel("search-hits");
            if (!pc.HasResult)
            {
                return(EmptySearchDocuments());
            }

            var documents = new SearchDocuments();

            foreach (var product in pc.ResultAsProducts())
            {
                var productDocument = new SearchDocument();
                foreach (var field in product.Select(a => new SearchField(a.Name, a.Value)))
                {
                    productDocument.Add(field);
                }
                documents.Add(productDocument);

                foreach (var variant in product.VariantList)
                {
                    var variantDocument = new SearchDocument();
                    foreach (var field in variant.Select(a => new SearchField(a.Name, a.Value)))
                    {
                        variantDocument.Add(field);
                    }
                    documents.Add(variantDocument);
                }
            }

            documents.TotalCount = documents.Count; //Bug in SearchResults, SearchDocuments.TotalCount is never set implicitly.
            return(documents);
        }
Ejemplo n.º 2
0
        private void PopulateDocuments(string initData)
        {
            var searchDocuments = new SearchDocuments();

            foreach (var t in initData)
            {
                var searchDocument = new SearchDocument();
                var fieldTitle     = new SearchField("Title", $"Tieu de {t}");
                searchDocument.Add(fieldTitle);
                var fieldContent = new SearchField("Content", $"Noi dung {t}");
                searchDocument.Add(fieldContent);
                searchDocuments.Add((ISearchDocument)searchDocument);
            }

            searchDocuments.TotalCount = initData.Length;
            this.Documents             = (ISearchDocuments)searchDocuments;
        }
Ejemplo n.º 3
0
        private ISearchResults Search <T>(ISearchCriteria criteria) where T : EntryContentBase
        {
            // set up filters

            // 1: phrase

            var filters = new List <Nest.QueryContainer>();

            if (criteria is CatalogEntrySearchCriteria)
            {
                var cesc = criteria as CatalogEntrySearchCriteria;

                if (!string.IsNullOrWhiteSpace(cesc.SearchPhrase))
                {
                    filters.Add(new Nest.QueryContainerDescriptor <T>().SimpleQueryString(
                                    sq => sq.Fields(f => f.Field("*.analyzed")).Query(cesc.SearchPhrase.Trim())));
                }
            }

            // 2: id ... NOTE: Vulcan supports 1 and only 1 filter field, code

            if (criteria.ActiveFilterFields != null && criteria.ActiveFilterFields.Count() == 1 && criteria.ActiveFilterFields[0].Equals("code", StringComparison.InvariantCultureIgnoreCase))
            {
                filters.Add(new Nest.QueryContainerDescriptor <T>().Term(
                                p => p.Field(f => f.Code).Value((criteria.ActiveFilterValues[0] as SimpleValue).value)));
            }

            // 3: inactive... TODO, not sure what this should check!

            /*
             * if(!criteria.IncludeInactive)
             * {
             *  filters.Add(new Nest.QueryContainerDescriptor<T>().Term(
             *      p => p.Field(f => f.)
             * }*/

            // get catalog filter, if needed

            var catalogReferences = new List <ContentReference>();

            if (criteria is CatalogEntrySearchCriteria)
            {
                var cesc = criteria as CatalogEntrySearchCriteria;

                if (cesc.CatalogNames != null)
                {
                    var catalogs = ContentLoader.Service.GetChildren <CatalogContent>(ReferenceConverter.Service.GetRootLink());

                    if (catalogs != null && catalogs.Any())
                    {
                        foreach (var catalogName in cesc.CatalogNames)
                        {
                            var catalog = catalogs.FirstOrDefault(c => c.Name.Equals(catalogName, StringComparison.InvariantCultureIgnoreCase));

                            if (catalog != null)
                            {
                                catalogReferences.Add(catalog.ContentLink);
                            }
                        }
                    }
                }
            }

            if (!catalogReferences.Any())
            {
                catalogReferences = null;
            }

            // do search

            var searchDescriptor = new Nest.SearchDescriptor <T>();

            searchDescriptor.Skip(criteria.StartingRecord);
            searchDescriptor.Take(criteria.RecordsToRetrieve);

            if (filters.Any())
            {
                searchDescriptor.Query(q => q.Bool(b => b.Must(filters.ToArray())));
            }

            var client = VulcanHandler.Service.GetClient(new CultureInfo(criteria.Locale));

            var results = client.SearchContent <T>(q => searchDescriptor, false, catalogReferences);

            //var id = ReferenceConverter.Service.GetObjectId();

            var searchDocuments = new SearchDocuments()
            {
                TotalCount = Convert.ToInt32(results.Total)
            };

            if (results.Hits != null && results.Hits.Any())
            {
                foreach (var hit in results.Hits)
                {
                    var doc = new SearchDocument();
                    doc.Add(new SearchField("_id", ReferenceConverter.Service.GetObjectId(hit.Source.ContentLink)));

                    searchDocuments.Add(doc);
                }
            }

            return(new SearchResults(searchDocuments, criteria));
        }