示例#1
0
        public void Insert(TModel model, string indexName)
        {
            CheckIndex(indexName, false);
            var response = elasticClient.Index <TModel>(model, i => i.Index(indexName));

            ElasticResponseValidator.Validate(response);
        }
示例#2
0
        public void BulkInsert(IEnumerable <TModel> models, string indexName)
        {
            CheckIndex(indexName, true);
            var bulk = new BulkDescriptor();

            foreach (var model in models)
            {
                bulk.Index <TModel>(x => x.Index(indexName).Document(model));
            }
            var response = elasticClient.Bulk(bulk);

            ElasticResponseValidator.Validate(response);
        }
示例#3
0
 public IReadOnlyCollection <IHit <TModel> > RetrieveQueryHits(
     QueryContainer container,
     string indexName,
     Pagination pagination = null
     )
 {
     if (pagination != null)
     {
         var response = RetrieveQueryResponse(container, indexName, pagination);
         ElasticResponseValidator.Validate(response);
         return(response.Hits);
     }
     return(FetchAllHitsByQuery(container, indexName));
 }
示例#4
0
 public IEnumerable <TModel> RetrieveQueryDocuments(
     QueryContainer container,
     string indexName,
     Pagination pagination = null
     )
 {
     if (pagination != null)
     {
         var response = RetrieveQueryResponse(container, indexName, pagination);
         ElasticResponseValidator.Validate(response);
         return(response.Documents);
     }
     return(FetchAllByQuery(container, indexName));
 }
示例#5
0
        protected ISearchResponse <TModel> RetrieveQueryResponse(
            QueryContainer container,
            string indexName,
            Pagination pagination
            )
        {
            var response = elasticClient.Search <TModel>(s => s
                                                         .Index(indexName)
                                                         .Query(q => container)
                                                         .Size(pagination.PageSize)
                                                         .From(pagination.PageIndex * pagination.PageSize));

            ElasticResponseValidator.Validate(response);
            return(response);
        }
示例#6
0
        private ISearchResponse <TModel> NestScrollSearchInit(
            QueryContainer container,
            string indexName,
            string scrollTimeout = "2m",
            int scrollSize       = 10000
            )
        {
            var response = elasticClient.Search <TModel>(s => s
                                                         .Index(indexName)
                                                         .From(0)
                                                         .Take(scrollSize)
                                                         .Query(q => container)
                                                         .Scroll(scrollTimeout)
                                                         );

            ElasticResponseValidator.Validate(response);
            return(response);
        }
示例#7
0
        private IReadOnlyCollection <IHit <TModel> > FetchAllHitsByQuery(QueryContainer container, string indexName)
        {
            var    response    = NestScrollSearchInit(container, indexName);
            var    result      = new List <IHit <TModel> >();
            var    anyHitsLeft = true;
            string scrollId    = response.ScrollId;

            while (anyHitsLeft)
            {
                if (response.IsValid)
                {
                    result.AddRange(response.Hits);
                    scrollId = response.ScrollId;
                    response = elasticClient.Scroll <TModel>("2m", scrollId);
                    ElasticResponseValidator.Validate(response);
                }
                anyHitsLeft = response.Hits.Any();
            }
            elasticClient.ClearScroll(new ClearScrollRequest(scrollId));
            return(new ReadOnlyCollection <IHit <TModel> >(result));
        }
示例#8
0
        public IEnumerable <TModel> FetchAllByQuery(QueryContainer queryContainer, string indexName)
        {
            var    response    = NestScrollSearchInit(queryContainer, indexName);
            var    result      = new List <TModel>();
            var    anyHitsLeft = true;
            string scrollId    = response.ScrollId;

            while (anyHitsLeft)
            {
                if (response.IsValid)
                {
                    result.AddRange(response.Documents);
                    scrollId = response.ScrollId;
                    response = elasticClient.Scroll <TModel>("2m", scrollId);
                    ElasticResponseValidator.Validate(response);
                }
                anyHitsLeft = response.Documents.Any();
            }
            elasticClient.ClearScroll(new ClearScrollRequest(scrollId));
            return(result);
        }
示例#9
0
        protected void UpdateById_(string id_, string indexName, TModel newModel)
        {
            var response = elasticClient.Update <TModel>(id_, u => u.Index(indexName).Doc(newModel));

            ElasticResponseValidator.Validate(response);
        }
示例#10
0
        protected void DeleteById_(string id_, string indexName)
        {
            var response = elasticClient.Delete <TModel>(id_, dd => dd.Index(indexName));

            ElasticResponseValidator.Validate(response);
        }