Beispiel #1
0
        public async Task <IEnumerable <ContentPickerResult> > Search(ContentPickerSearchContext searchContext)
        {
            var indexName = "Search";

            var fieldSettings = searchContext.PartFieldDefinition?.GetSettings <ContentPickerFieldElasticEditorSettings>();

            if (!string.IsNullOrWhiteSpace(fieldSettings?.Index))
            {
                indexName = fieldSettings.Index;
            }

            if (!await _elasticIndexProvider.ExistsAsync(indexName))
            {
                return(new List <ContentPickerResult>());
            }

            var results = new List <ContentPickerResult>();

            await _elasticIndexProvider.SearchAsync(indexName, () =>
            {
                //todo
//                Query query = null;
//
//                if (string.IsNullOrWhiteSpace(searchContext.Query))
//                {
//                    query = new MatchAllDocsQuery();
//                }
//                else
//                {
//                    query = new WildcardQuery(new Term("Content.ContentItem.DisplayText.Analyzed", searchContext.Query.ToLowerInvariant() + "*"));
//                }
//
//                var filter = new FieldCacheTermsFilter("Content.ContentItem.ContentType", searchContext.ContentTypes.ToArray());
//
//                var docs = searcher.Search(query, filter, 50, Sort.RELEVANCE);
//
//                foreach (var hit in docs.ScoreDocs)
//                {
//                    var doc = searcher.Doc(hit.Doc);
//
//                    results.Add(new ContentPickerResult
//                    {
//                        ContentItemId = doc.GetField("ContentItemId").GetStringValue(),
//                        DisplayText = doc.GetField("Content.ContentItem.DisplayText").GetStringValue()
//                    });
//                }

                return(Task.CompletedTask);
            });

            return(results);
        }
Beispiel #2
0
        public async Task <IActionResult> Index(string id, string q, PagerParameters pagerParameters)
        {
            var siteSettings = await _siteService.GetSiteSettingsAsync();

            var pager = new Pager(pagerParameters, siteSettings.PageSize);

            var indexName = "Search";

            if (!string.IsNullOrWhiteSpace(id))
            {
                indexName = id;
            }

            if (!await _elasticIndexProvider.ExistsAsync(indexName))
            {
                return(NotFound());
            }

            if (string.IsNullOrWhiteSpace(q))
            {
                return(View(new SearchIndexViewModel
                {
                    Pager = pager, IndexName = id, ContentItems = Enumerable.Empty <ContentItem>()
                }));
            }

            var luceneSettings = await _elasticIndexingService.GetElasticSettingsAsync();

            if (luceneSettings == null)  // || luceneSettings? .DefaultSearchFields == null)
            {
                Logger.LogInformation("Couldn't execute search. No Elastic settings was defined.");

                return(View(new SearchIndexViewModel
                {
                    HasMoreResults = false,
                    Query = q,
                    Pager = pager,
                    IndexName = id,
                    ContentItems = Enumerable.Empty <ContentItem>()
                }));
            }

            //todo
//            var queryParser = new MultiFieldQueryParser(LuceneSettings.DefaultVersion, luceneSettings.DefaultSearchFields, new StandardAnalyzer(LuceneSettings.DefaultVersion));
//            var query = queryParser.Parse(QueryParser.Escape(q));
            object query       = null;
            int    start       = pager.GetStartIndex(),
                   size        = pager.PageSize,
                   end         = size + 1; // Fetch one more result than PageSize to generate "More" links
            var contentItemIds = await _searchQueryService.ExecuteQueryAsync(query, indexName, start, end);

            var contentItems = new List <ContentItem>();

            foreach (var contentItemId in contentItemIds.Take(size))
            {
                var contentItem = await _contentManager.GetAsync(contentItemId);

                if (contentItem != null)
                {
                    contentItems.Add(contentItem);
                }
            }

            var model = new SearchIndexViewModel
            {
                HasMoreResults = contentItemIds.Count > size,
                Query          = q,
                Pager          = pager,
                IndexName      = id,
                ContentItems   = contentItems
            };

            return(View(model));
        }