Beispiel #1
0
        public async Task <IList <string> > ExecuteQueryAsync(object query, string indexName, int start, int end)
        {
            var contentItemIds = new List <string>();

            await _elasticIndexManager.SearchAsync(indexName, () =>
            {
                //the actual elastic search to perform and return content itemids

                var results = _searchService.Search(query.ToString(), start, end);
                //  return Ok(results);

//                var collector = TopScoreDocCollector.Create(end, true);
//
//                searcher.Search(query, collector);
//                var hits = collector.GetTopDocs(start, end);
//
//                foreach (var hit in hits.ScoreDocs)
//                {
//                    var d = searcher.Doc(hit.Doc, IdSet);
//                    contentItemIds.Add(d.GetField("ContentItemId").GetStringValue());
//                }

                return(Task.CompletedTask);
            });

            return(contentItemIds);
        }
Beispiel #2
0
        public async Task <IQueryResults> ExecuteQueryAsync(Query query, IDictionary <string, object> parameters)
        {
            var    elasticQuery        = query as ElasticQuery;
            var    elasticQueryResults = new SQLQueryResults();
            object result = null;


            await _elasticIndexProvider.SearchAsync(elasticQuery.Index, async() =>
            {
                //var templateContext = new TemplateContext();
                var templateContext = _liquidTemplateManager.Context;
                // await  templateContext.ContextualizeAsync(_serviceProvider);

                if (parameters != null)
                {
                    foreach (var parameter in parameters)
                    {
                        templateContext.SetValue(parameter.Key, parameter.Value);
                    }
                }

                var tokenizedContent   = await _liquidTemplateManager.RenderAsync(elasticQuery.Template, System.Text.Encodings.Web.JavaScriptEncoder.Default, templateContext);
                var parameterizedQuery = JObject.Parse(tokenizedContent);

                //var analyzer = _luceneAnalyzerManager.CreateAnalyzer(LuceneSettings.StandardAnalyzer);
                //var context = new ElasticQueryContext(searcher, LuceneSettings.DefaultVersion, analyzer);
                var context = new ElasticQueryContext(elasticQuery.Index);
                var docs    = await _queryService.SearchAsync(context, parameterizedQuery);

                if (elasticQuery.ReturnContentItems)
                {
                    /*todo
                     * // Load corresponding content item versions
                     * var contentItemVersionIds = docs.Results.Select(x => doc.Get("Content.ContentItem.ContentItemVersionId")).ToArray();
                     * var contentItems = await _session.Query<ContentItem, ContentItemIndex>(x => x.ContentItemVersionId.IsIn(contentItemVersionIds)).ListAsync();
                     *
                     * // Reorder the result to preserve the one from the lucene query
                     * var indexed = contentItems.ToDictionary(x => x.ContentItemVersionId, x => x);
                     * result = contentItemVersionIds.Select(x => indexed[x]).ToArray();
                     */
                }
                else
                {
                    var results = new List <JObject>();
                    foreach (var document in docs.Results.Select(hit => hit))
                    {
                        //todo
                        // results.Add(new JObject(document.Select(x => new JProperty(x.Name, x.GetStringValue()))));
                    }

                    result = results;
                }
            });

            elasticQueryResults.Items = new object[] { result };    //result; // new  object[results];
            return(elasticQueryResults);
        }
Beispiel #3
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);
        }