Esempio n. 1
0
        public async Task <SearchTemplate> GetTemplateAsync(string id)
        {
            SearchTemplate template = null;

            if (_cache == null)
            {
                template = await GetTemplateFromRepositoryAsync(id);
            }
            else
            {
                template = _cache.Get(id);

                if (template == null)
                {
                    template = await GetTemplateFromRepositoryAsync(id);

                    if (template != null)
                    {
                        _cache.Set(id, template, _expiration);
                    }
                }
            }

            return(template);
        }
Esempio n. 2
0
        public SearchRequest GetSearchRequest(IDictionary <string, object> parameters)
        {
            var jo = ToJson().SetParameters(parameters);

            var template = new SearchTemplate(jo);

            var request = new SearchRequest(template.RequestBody, true)
                          .SetIndex(template.Index)
                          .SetType(template.Type);

            if (request.Query != null)
            {
                request.SetQuery(request.Query.Transform(template.FieldAliases, template.QueryExpander));
            }

            return(request);
        }
Esempio n. 3
0
        public async Task <IndexDocumentResponse> SaveTemplateAsync(SearchTemplate template, bool refresh = false)
        {
            if (_cache != null)
            {
                _cache.Set(template.Id, template, _expiration);
            }

            bool exists = await _es.IndexExistsAsync(_index);

            if (!exists)
            {
                var index = await _es.CreateIndexAsync(new CreateIndexRequest(_index, Schema));

                if (!index.IsSuccess)
                {
                    throw new Exception(index.ToString());
                }
            }

            var doc      = new ElasticsearchDocument(template.Id, template.ToJson(true), _index);
            var response = await _es.IndexDocumentAsync(new IndexDocumentRequest(doc, refresh));

            return(response);
        }