Ejemplo n.º 1
0
        private ISearchResponse <Document> DoSearch(int?skip)
        {
            ISearchResponse <Document> searchResult;

            if (_luceneQuery != null)
            {
                _queryContainer = new QueryContainer(new QueryStringQuery()
                {
                    Query = _luceneQuery.ToString()
                });
            }


            SearchDescriptor <Document> searchDescriptor = new SearchDescriptor <Document>();

            searchDescriptor.Index(_indexName)
            .Skip(skip)
            .Size(_maxResults)
            .Query(q => _queryContainer);

            var json = _client.RequestResponseSerializer.SerializeToString(searchDescriptor);

            searchResult = _client.Search <Document>(searchDescriptor);


            //TODO: Get sorting working
            //TODO: Get filtering/range working
            //TODO: We need to escape the resulting query

            TotalItemCount = searchResult.Total;

            return(searchResult);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="descriptor">查询描述器</param>
        /// <param name="cancellationToken">取消令牌</param>
        public async Task <IQueryResult <TEntity> > SearchAsync(SearchDescriptor <TEntity> descriptor, CancellationToken cancellationToken = default)
        {
            var result = new List <TEntity>();

            descriptor = descriptor.Index(Context.GetIndexName <TEntity>(IndexName));
            Func <SearchDescriptor <TEntity>, ISearchRequest> selector = x => descriptor;
            var response = await Context.SearchAsync(selector, cancellationToken);

            if (response.ApiCall.RequestBodyInBytes != null)
            {
                var responseJson = Encoding.UTF8.GetString(response.ApiCall.RequestBodyInBytes);
            }

            if (!response.IsValid)
            {
                return(new CustomQueryResult <TEntity>());
            }
            result.AddRange(response.Hits.Select(x => x.Source));
            return(new CustomQueryResult <TEntity>
            {
                Data = result,
                Took = response.Took,
                TotalCount = response.Total
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 查询日志
        /// </summary>
        /// <param name="proj"></param>
        /// <param name="logLevel"></param>
        /// <param name="keyWrod">ES Json 查询语法</param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async override Task <List <string> > List(string proj, string logLevel, string keyWrod = null, int page = 1, int pageSize = 100)
        {
            page = page <= 1 ? 0 : page;
            List <string> result = new List <string>();

            try
            {
                var descriptor = new SearchDescriptor <LogMessage>();
                descriptor.Index(proj.ToLower())
                .Type(logLevel.ToLower())
                .Sort(f => f.Descending(l => l.Timestamp))
                .From(1)
                .Size(pageSize);
                if (!string.IsNullOrEmpty(keyWrod))
                {
                    descriptor.Query(q => q.SimpleQueryString(s => s.Query(keyWrod)));
                }

                var res = await client.SearchAsync <LogMessage>(descriptor);

                result.AddRange(res.Hits.Select(s => JsonSerializer.Serialize(s.Source)));
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message, ex);
            }
            return(result);
        }
        public async Task <ISearchResponse <T> > SimpleSearchAsync <T>(string indexName, SearchDescriptor <T> query) where T : class, new()
        {
            query.Index(indexName);
            var response = await ElasticSearchClient.SearchAsync <T>(query);

            return(response);
        }
Ejemplo n.º 5
0
        public SearchResult <TEntity> Search(QueryContainer query, SearchOptions searchOptions, string index = null, string type = null)
        {
            SearchDescriptor <TEntity> descriptor = new SearchDescriptor <TEntity>();

            descriptor.Index(index ?? $"{this.Options.DefaultIndexName}*")
            .Type(type ?? this.Options.DefaultTypeName)
            .AddQuery(query)
            .AddPaging(searchOptions)
            .AddSorting(searchOptions)
            .IgnoreUnavailable(true)
            .AllowNoIndices(true)
            .AllowPartialSearchResults(true);

            try
            {
                var elasticResponse = this.ElasticClient.Search <TEntity>(descriptor);

                return(new SearchResult <TEntity>
                {
                    Total = elasticResponse.Total,
                    Items = elasticResponse.Documents
                });
            }
            catch (Exception e)
            {
                throw new OperationCanceledException("Invalid result on search", e);
            }
        }
Ejemplo n.º 6
0
        public virtual async Task <ISearchResponse <T> > SimpleSearchAsync <T, TKey>(string indexName, SearchDescriptor <T> query) where T : ElasticEntity <TKey>
        {
            query.Index(indexName);
            var response = await ElasticSearchClient.SearchAsync <T>(query);

            return(response);
        }
Ejemplo n.º 7
0
        public ScrollResult <T> Scroll(QueryContainer query, ScrollOptions scrollOptions)
        {
            ISearchResponse <T> elasticResponse = null;

            if (!string.IsNullOrWhiteSpace(scrollOptions.ScrollId))
            {
                ScrollRequest scrollRequest = new ScrollRequest(scrollOptions.ScrollId, scrollOptions.Scroll);
                elasticResponse = this.ElasticClient.Scroll <T>(scrollRequest);
            }
            else
            {
                SearchDescriptor <T> descriptor = new SearchDescriptor <T>();

                descriptor.Index($"{this.Options.DefaultIndexName}*")
                .Type(this.Options.DefaultTypeName)
                .AddQuery(query)
                .AddScroll(scrollOptions)
                .AddSorting(scrollOptions);

                elasticResponse        = this.ElasticClient.Search <T>(descriptor);
                scrollOptions.ScrollId = elasticResponse.ScrollId;
            }

            return(new ScrollResult <T>
            {
                Total = elasticResponse.Total,
                Items = elasticResponse.Documents,
                ScrollId = elasticResponse.ScrollId
            });
        }
Ejemplo n.º 8
0
        public async Task <List <NearestPlace> > GetNearest(string type, double lat, double lng, int distance)
        {
            var client           = GetClient();
            var searchDescriptor = new SearchDescriptor <Place>();
            var dumper           = new NestDescriptorDumper(client.RequestResponseSerializer);

            searchDescriptor
            .Index <Place>()
            .Query(x =>
                   x.Bool(b =>
                          b.Must(
                              m => m.Term(t => t.Field(f => f.Types).Value(type)),
                              m => m.GeoDistance(g =>
                                                 g.Distance(new Distance(distance, DistanceUnit.Kilometers))
                                                 .Location(lat, lng)
                                                 .Field(f => f.Geometry.Location))
                              )
                          ))
            .ScriptFields(x =>
                          x.ScriptField("distance", s => s.Source($"doc['geometry.location'].arcDistance({lat},{lng})")))
            .Source(true)
            .DocValueFields(d =>
                            d.Field(f => f.Name)
                            .Field(f => f.Vicinity)
                            .Field(f => f.Types))
            ;

            var sss = dumper.Dump <SearchDescriptor <Place> >(searchDescriptor);

            var results = await client.SearchAsync <Place>(searchDescriptor);

            return(ToNearestPlaces(results.Hits));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 命中查询
        /// </summary>
        /// <param name="descriptor">查询描述符</param>
        /// <param name="cancellationToken">取消令牌</param>
        public async Task <IEnumerable <IHit <TEntity> > > HitsSearchAsync(SearchDescriptor <TEntity> descriptor, CancellationToken cancellationToken = default)
        {
            descriptor = descriptor.Index(Context.GetIndexName <TEntity>(IndexName));
            Func <SearchDescriptor <TEntity>, ISearchRequest> selector = x => descriptor;
            var response = await Context.SearchAsync(selector, cancellationToken);

            return(response.IsValid ? response.Hits : null);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 聚合查询
        /// </summary>
        /// <param name="descriptor">查询描述符</param>
        /// <param name="key">键名</param>
        /// <param name="cancellationToken">取消令牌</param>
        public async Task <TermsAggregate <string> > AggregationsSearchAsync(SearchDescriptor <TEntity> descriptor, string key, CancellationToken cancellationToken = default)
        {
            descriptor = descriptor.Index(Context.GetIndexName <TEntity>(IndexName));
            Func <SearchDescriptor <TEntity>, ISearchRequest> selector = x => descriptor;
            var response = await Context.SearchAsync(selector, cancellationToken);

            return(response.IsValid ? response.Aggregations.Terms(key) : null);
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        protected async Task <IReadOnlyCollection <Dictionary <string, object> > > SearchAllInElastic()
        {
            var searchDescriptor = new SearchDescriptor <TAggregateRoot>();

            searchDescriptor.MatchAll();
            searchDescriptor.Index(_client.IndexFor(ModuleName()));

            return((await _client.Client.SearchAsync <Dictionary <string, object> >(searchDescriptor))?.Documents);
        }
Ejemplo n.º 12
0
        public static IQueryResponse<JsonObject> Do(IElasticClient client)
        {
            var sed = new SearchDescriptor<JsonObject>();

            sed.Index("entities").Type("locations").ConcreteTypeSelector((o, hit) => typeof (JsonObject));

            sed.FacetQuery("one", q => q.QueryString(a => a.Query("parentId:59791")));
            sed.FacetQuery("two", q => q.QueryString(a => a.Query("parentId:7309")));

            return client.Search(sed);
        }
Ejemplo n.º 13
0
        public async Task <TermVectorsModel> TermVectors(string queryString, double lat, double lng)
        {
            var client           = GetClient();
            var searchDescriptor = new SearchDescriptor <Place>();
            var dumper           = new NestDescriptorDumper(client.RequestResponseSerializer);

            #region Search
            QueryContainerDescriptor <Place> queryContainer = new QueryContainerDescriptor <Place>();
            var query = queryContainer.Bool(b =>
                                            b.Should(
                                                q => q.Match(m => m.Field(f => f.Name).Query(queryString)),
                                                q => q.Match(m => m.Field(f => f.Vicinity).Query(queryString))
                                                )
                                            );

            Func <SortDescriptor <Place>, SortDescriptor <Place> > SortByGeo =
                (SortDescriptor <Place> s) => s.GeoDistance(g =>
                                                            g.Field(f => f.Geometry.Location)
                                                            .DistanceType(GeoDistanceType.Arc)
                                                            .Unit(DistanceUnit.Kilometers)
                                                            .Order(SortOrder.Ascending)
                                                            .Points(new GeoLocation(lat, lng)));

            Func <SortDescriptor <Place>, IPromise <IList <ISort> > > sort = s => SortByGeo(s.Descending(SortSpecialField.Score).Descending(d => d.Rating));

            searchDescriptor
            .Index <Place>()
            .Query(x => query)
            .Sort(sort)
            .ScriptFields(x =>
                          x.ScriptField("distance", s => s.Source($"doc['geometry.location'].arcDistance({lat},{lng})")))
            .Take(10)
            .Source(true)
            ;
            #endregion

            var results = await client.SearchAsync <Place>(searchDescriptor);

            var model = ToSearchPlaces(results.Hits).First();

            var term = new TermVectorsDescriptor <Place>(IndexName.From <Place>(), TypeName.From <Place>());
            term.Index <Place>()
            .Id(Id.From(model))
            .Fields(tf => tf.Vicinity, tf => tf.Name)
            ;

            var sss = dumper.Dump <TermVectorsDescriptor <Place> >(term);

            var termVectorsResult = await client.TermVectorsAsync <Place>(term);

            return(ToTermVectorsModel(termVectorsResult, model));
        }
Ejemplo n.º 14
0
        public IActionResult Search(string indexName, string query)
        {
            SearchViewModel           model      = new SearchViewModel();
            SearchDescriptor <ESData> descriptor = new SearchDescriptor <ESData>();

            descriptor.Index(indexName);
            descriptor.Query(x => x.Match(qs => qs.Query(query)));
            var response = _client.Search <ESData>(descriptor);

            model.Respuesta = response.Documents;
            model.IndexName = indexName;
            return(View(model));
        }
Ejemplo n.º 15
0
        public ScrollResult <TEntity> Scroll(QueryContainer query, ScrollOptions scrollOptions, string index = null, string type = null)
        {
            ISearchResponse <TEntity> elasticResponse = null;

            if (!string.IsNullOrWhiteSpace(scrollOptions.ScrollId))
            {
                ScrollRequest scrollRequest = new ScrollRequest(scrollOptions.ScrollId, scrollOptions.Scroll);

                try
                {
                    elasticResponse = this.ElasticClient.Scroll <TEntity>(scrollRequest);
                }
                catch (Exception e)
                {
                    throw new OperationCanceledException("Invalid result on scroll by id", e);
                }
            }
            else
            {
                SearchDescriptor <TEntity> descriptor = new SearchDescriptor <TEntity>();

                descriptor.Index(index ?? $"{this.Options.DefaultIndexName}*")
                .Type(this.Options.DefaultTypeName)
                .AddQuery(query)
                .AddScroll(scrollOptions)
                .AddSorting(scrollOptions)
                .IgnoreUnavailable(true)
                .AllowNoIndices(true)
                .AllowPartialSearchResults(true);

                try
                {
                    elasticResponse = this.ElasticClient.Search <TEntity>(descriptor);
                }
                catch (Exception e)
                {
                    throw new OperationCanceledException("Invalid result on new scroll", e);
                }

                scrollOptions.ScrollId = elasticResponse.ScrollId;
            }

            return(new ScrollResult <TEntity>
            {
                Total = elasticResponse.Total,
                Items = elasticResponse.Documents,
                ScrollId = elasticResponse.ScrollId
            });
        }
Ejemplo n.º 16
0
        /// <summary>
        /// 查询。单一条件查询,一般是精确查询
        /// </summary>
        /// <typeparam name="TDocument">文档类型</typeparam>
        /// <param name="context">ES上下恩</param>
        /// <param name="field">字段名</param>
        /// <param name="value">值</param>
        /// <param name="index">索引名称。注意:必须小写</param>
        /// <param name="cancellationToken">取消令牌</param>
        public static async Task <IEnumerable <TDocument> > SearchAsync <TDocument>(this IElasticsearchContext context, string field, object value, string index = null, CancellationToken cancellationToken = default)
            where TDocument : class
        {
            if (field.IsEmpty())
            {
                throw new ArgumentNullException(nameof(field));
            }
            index = context.GetIndexName(Helper.SafeIndexName <TDocument>(index));
            var descriptor = new SearchDescriptor <TDocument>();

            descriptor.Index(index)
            .PostFilter(f => f.Term(x => x.Field(field).Value(value)));
            Func <SearchDescriptor <TDocument>, ISearchRequest> selector = x => descriptor;
            var response = await context.SearchAsync(selector, cancellationToken);

            return(response.Documents);
        }
Ejemplo n.º 17
0
        public SearchResult <T> Search(QueryContainer query, SearchOptions searchOptions)
        {
            SearchDescriptor <T> descriptor = new SearchDescriptor <T>();

            descriptor.Index($"{this.Options.DefaultIndexName}*")
            .Type(this.Options.DefaultTypeName)
            .AddQuery(query)
            .AddPaging(searchOptions)
            .AddSorting(searchOptions);

            var elasticResponse = this.ElasticClient.Search <T>(descriptor);

            return(new SearchResult <T>
            {
                Total = elasticResponse.Total,
                Items = elasticResponse.Documents
            });
        }
Ejemplo n.º 18
0
        private static List <ProductListV2> LoadData(int page)
        {
            var client = new ElasticClient(new Uri("http://172.16.42.28:9200/"));
            var sd     = new SearchDescriptor <ProductListV2>();

            sd = sd.Index("productlist");
            int pagesize = 100;
            var range    = PagerHelper.GetQueryRange(page, pagesize);

            sd = sd.Skip(range[0]).Take(range[1]);
            var response = client.Search <ProductListV2>(x => sd);

            if (response.IsValid)
            {
                return(response?.Hits.Select(x => x.Source).Where(x => x != null).ToList());
            }
            return(null);
        }
Ejemplo n.º 19
0
        public virtual async Task <ISearchResponse <T> > SearchAsync <T, TKey>(string indexName, SearchDescriptor <T> query, int skip, int size, string[] includeFields = null,
                                                                               string preTags   = "<strong style=\"color: red;\">", string productTags = "</strong>",
                                                                               bool disableHigh = false, params string[] highField) where T : ElasticEntity <TKey>
        {
            query.Index(indexName);
            var highdes = new HighlightDescriptor <T>();

            if (disableHigh)
            {
                preTags     = "";
                productTags = "";
            }
            highdes.PreTags(preTags).PostTags(productTags);

            var ishigh = highField != null && highField.Length > 0;

            var hfs = new List <Func <HighlightFieldDescriptor <T>, IHighlightField> >();

            //Pagination
            query.Skip(skip).Take(size);
            //Keyword highlighting
            if (ishigh)
            {
                foreach (var s in highField)
                {
                    hfs.Add(f => f.Field(s));
                }
            }

            highdes.Fields(hfs.ToArray());
            query.Highlight(h => highdes);
            if (includeFields != null)
            {
                query.Source(ss => ss.Includes(ff => ff.Fields(includeFields.ToArray())));
            }


            var data     = JsonConvert.SerializeObject(query);
            var response = await ElasticSearchClient.SearchAsync <T>(query);


            return(response);
        }
Ejemplo n.º 20
0
        //public ElasticSearchContext(ConnectionSettingsBase<ConnectionSettings> settings, ILogger<ElasticSearchContext> logger)
        //{
        //    var _settings = settings ?? throw new ArgumentNullException(nameof(settings));
        //    Context = new ElasticClient(_settings);
        //    Logger = logger ?? throw new ArgumentNullException(nameof(logger));
        //}
        #endregion
        /// <summary>
        ///
        /// </summary>
        /// <param name="Ids"></param>
        /// <param name="indexName"></param>
        /// <returns></returns>
        public async Task <ISearchResponse <TEntity> > GetByIdsAsync <TEntity>(IEnumerable <Id> Ids, string indexName = null)
            where TEntity : class
        {
            SearchDescriptor <TEntity> selector = new SearchDescriptor <TEntity>();

            if (!string.IsNullOrWhiteSpace(indexName))
            {
                selector.Index(indexName);
            }
            selector.Query(q => q.Ids(i => i.Values(Ids)));
            var response = await Context.SearchAsync <TEntity>(selector);

            if (!response.IsValid)
            {
                Logger.LogError($"[Success:{response.ApiCall.Success}]\t{response.ApiCall.Uri}");
                Logger.LogError(response.ApiCall.DebugInformation);
                Logger.LogError(response.ApiCall.OriginalException?.Message);
            }
            return(response);
        }
        /// <summary>
        /// search
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="indexName"></param>
        /// <param name="query"></param>
        /// <param name="skip">skip num</param>
        /// <param name="size">return document size</param>
        /// <param name="includeFields">return fields</param>
        /// <param name="preTags">Highlight tags</param>
        /// <param name="postTags">Highlight tags</param>
        /// <param name="disableHigh"></param>
        /// <param name="highField">Highlight fields</param>
        /// <returns></returns>
        public virtual async Task <ISearchResponse <T> > SearchAsync <T, TKey>(string indexName, SearchDescriptor <T> query,
                                                                               int skip, int size, string[] includeFields = null,
                                                                               string preTags = "<strong style=\"color: red;\">", string postTags = "</strong>", bool disableHigh = false,
                                                                               params string[] highField) where T : class
        {
            query.Index(indexName);
            var highlight = new HighlightDescriptor <T>();

            if (disableHigh)
            {
                preTags  = "";
                postTags = "";
            }

            highlight.PreTags(preTags).PostTags(postTags);

            var isHigh = highField != null && highField.Length > 0;

            var hfs = new List <Func <HighlightFieldDescriptor <T>, IHighlightField> >();

            //分页
            query.Skip(skip).Take(size);
            //关键词高亮
            if (isHigh)
            {
                foreach (var s in highField)
                {
                    hfs.Add(f => f.Field(s));
                }
            }

            highlight.Fields(hfs.ToArray());
            query.Highlight(h => highlight);
            if (includeFields != null)
            {
                query.Source(ss => ss.Includes(ff => ff.Fields(includeFields.ToArray())));
            }
            var response = await _esClient.SearchAsync <T>(query);

            return(response);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// </summary>
        /// <param name="indexName">if it's empty or null, than all the properties get back (for all indices)</param>
        /// <returns></returns>
        public Dictionary <string, IHit <PropertiesElastic> > GetPropertiesHit(string indexName)
        {
            var sdesc = new SearchDescriptor <PropertiesElastic>();
            ISearchResponse <PropertiesElastic> searchResponse;

            if (string.IsNullOrEmpty(indexName))
            {
                sdesc.Index(Indices.AllIndices);
                var client = clientFactory.GetClient();
                var size   = client.Search <PropertiesElastic>(sdesc).Total;
                sdesc.Size((int)size);
                searchResponse = client.Search <PropertiesElastic>(sdesc);
            }
            else
            {
                searchResponse = Client.Search <PropertiesElastic>(sdesc);
            }

            ResponseValidator(searchResponse);
            return(searchResponse?.Hits.ToDictionary(h => h.Index, h => h));
        }
Ejemplo n.º 23
0
        private static ISearchRequest CreateSearchDescriptor <TEntity>(ElasticSearchRequest <TEntity> elasticSearchRequest,
                                                                       SearchDescriptor <TEntity> descriptor,
                                                                       string indexName)
            where TEntity : class
        {
            var sort = elasticSearchRequest.Sort;

            var searchDescriptor = descriptor
                                   .Index(indexName)
                                   .Query(elasticSearchRequest.Query)
                                   .Sort(sort)
                                   .From(elasticSearchRequest.Offset)
                                   .Take(elasticSearchRequest.Limit);

            if (elasticSearchRequest.Aggregation != null)
            {
                searchDescriptor.Aggregations(elasticSearchRequest.Aggregation);
            }

            return(searchDescriptor);
        }
Ejemplo n.º 24
0
        public async Task <List <SearchPlace> > Search(string queryString, double lat, double lng, bool descRates)
        {
            var client           = GetClient();
            var searchDescriptor = new SearchDescriptor <Place>();
            var dumper           = new NestDescriptorDumper(client.RequestResponseSerializer);

            QueryContainerDescriptor <Place> queryContainer = new QueryContainerDescriptor <Place>();
            var query = queryContainer.Bool(b =>
                                            b.Should(
                                                q => q.Match(m => m.Field(f => f.Name).Query(queryString)),
                                                q => q.Match(m => m.Field(f => f.Vicinity).Query(queryString))
                                                )
                                            );

            Func <SortDescriptor <Place>, SortDescriptor <Place> > SortByGeo =
                (SortDescriptor <Place> s) => s.GeoDistance(g =>
                                                            g.Field(f => f.Geometry.Location)
                                                            .DistanceType(GeoDistanceType.Arc)
                                                            .Unit(DistanceUnit.Kilometers)
                                                            .Order(SortOrder.Ascending)
                                                            .Points(new GeoLocation(lat, lng)));

            Func <SortDescriptor <Place>, IPromise <IList <ISort> > > sort = s => SortByGeo(descRates ? s.Descending(SortSpecialField.Score).Descending(d => d.Rating) : s.Descending(SortSpecialField.Score));

            searchDescriptor
            .Index <Place>()
            .Query(x => query)
            .Sort(sort)
            .ScriptFields(x =>
                          x.ScriptField("distance", s => s.Source($"doc['geometry.location'].arcDistance({lat},{lng})")))
            .Take(10)
            .Source(true)
            ;

            var sss = dumper.Dump <SearchDescriptor <Place> >(searchDescriptor);

            var results = await client.SearchAsync <Place>(searchDescriptor);

            return(ToSearchPlaces(results.Hits));
        }
Ejemplo n.º 25
0
        public ISearchResponse <ProductListV2> SearchEsProducts(SearchParamModel model)
        {
            ISearchResponse <ProductListV2> response = null;
            var temp = new ProductListV2();

            PrepareES(client =>
            {
                var sd = new SearchDescriptor <ProductListV2>();

                sd = sd.Index(ES_PRODUCTLIST_INDEX);

                sd = sd.Query(q => BuildQuery(model));

                var NAMEOF_ShowCatalogIdList = nameof(temp.ShowCatalogIdList);
                var NAMEOF_BrandId           = nameof(temp.BrandId);
                var NAMEOF_ProductAttributes = nameof(temp.ProductAttributes);

                sd = sd.Aggregations(agg => agg
                                     .Terms(NAMEOF_ShowCatalogIdList, av => av.Field(NAMEOF_ShowCatalogIdList).Size(1000))
                                     .Terms(NAMEOF_BrandId, av => av.Field(NAMEOF_BrandId).Size(1000))
                                     .Terms(NAMEOF_ProductAttributes, av => av.Field(NAMEOF_ProductAttributes).Size(1000)));

                sd = sd.Sort(x => BuildSort(model));

                //var range = PagerHelper.GetQueryRange(model.page, model.pagesize);
                //sd = sd.Skip(range[0]).Take(range[1]);
                sd = sd.QueryPage_(model.page, model.pagesize);

                response = client.Search <ProductListV2>(x => sd);

                var mx = response.Aggs.Max("");

                return(true);
            });
            if (response == null || !response.IsValid || response.OriginalException != null)
            {
                throw new Exception("ES 挂了");
            }
            return(response);
        }
        public IActionResult Index(IFormCollection form)
        {
            ConnectionSettings connectionSettings = new ConnectionSettings(new Uri("http://10.1.9.180:9200"));

            connectionSettings.DefaultIndex("product_pool");
            var mapping = this.client.Instance.GetMapping(new GetMappingRequest(Nest.Indices.Index("product_pool")));

            List <KeyValuePair <string, string> > FieldList = mapping.Indices.First().Value.Mappings.Values.First().Properties.Select(x => new KeyValuePair <string, string>(x.Key.Name, x.Value.Type))
                                                              .ToList();
            SearchDescriptor <Dictionary <string, object> > search = new SearchDescriptor <Dictionary <string, object> >();

            search = search.Index("product_pool");
            search = search.AllTypes();
            var qr = new QueryContainerDescriptor <Dictionary <string, object> >();

            QueryContainer queryContainer = new QueryContainer();

            foreach (var itePair in form.Where(x => !string.IsNullOrEmpty(x.Value)).ToList())
            {
                if (FieldList.Any(x => x.Key == itePair.Key.Replace("searchKey_", "")))
                {
                    queryContainer &= qr.Term(itePair.Key.Replace("searchKey_", ""), itePair.Value[0]);
                }
            }

            search.Query(descriptor => queryContainer);
            var documents = this.client.Instance.Search <Dictionary <string, object> >(search);

            //var documents = this.client.Instance.Search<Dictionary<string, object>>(new SearchRequest() { Query = queryContainer });

            //var documents = this.client.Instance.Search<Dictionary<string, object>>(x => x.Index("product_pool").AllTypes().MatchAll());
            var model = new SearchViewModel
            {
                Documents = documents.Hits.Select(x => x.Source).ToList(),
                FieldList = FieldList
            };

            return(View(model));
        }
        private ISearchResponse <Document> DoSearch(int?skip)
        {
            lastskip = skip ?? 0;
            ISearchResponse <Document> searchResult;

            if (_luceneQuery != null)
            {
                _queryContainer = new QueryContainer(new QueryStringQuery()
                {
                    Query           = _luceneQuery.ToString(),
                    AnalyzeWildcard = true
                });
            }
            if (_queryContainer != null)
            {
                SearchDescriptor <Document> searchDescriptor = new SearchDescriptor <Document>();
                searchDescriptor.Index(_indexName)
                .Skip(skip)
                .Size(_maxResults)
                .Query(q => _queryContainer)

                .Sort(s => _sortDescriptor);

                var json = _client.RequestResponseSerializer.SerializeToString(searchDescriptor);
                searchResult = _client.Search <Document>(searchDescriptor);
            }
            else if (_searchRequest != null)
            {
                searchResult = _client.Search <Document>(_searchRequest);
            }
            else
            {
                searchResult = _client.Search <Document>(_searchSelector);
            }

            TotalItemCount = searchResult.Total;
            Aggregation    = searchResult.Aggregations;
            return(searchResult);
        }
Ejemplo n.º 28
0
        public SearchDescriptor <T> DescribeSearch(SearchDescriptor <T> search)
        {
            search.Index(indexName).Type(typeName);

            type.DescribeSearch(search, Parameters);

            if (!string.IsNullOrEmpty(Parameters["count"]))
            {
                search.Size(int.Parse(Parameters["count"]));
            }
            else
            {
                search.Size(OpenSearchEngine.DEFAULT_COUNT);
            }

            if (!string.IsNullOrEmpty(Parameters["startIndex"]))
            {
                search.From(int.Parse(Parameters["startIndex"]) - 1);
            }

            return(search);
        }
Ejemplo n.º 29
0
        public async Task <List <BestPlaceAround> > GetBestPlacesAround(int distance, double lat, double lng, bool descRates, bool openedOnly)
        {
            var client           = GetClient();
            var searchDescriptor = new SearchDescriptor <Place>();
            var dumper           = new NestDescriptorDumper(client.RequestResponseSerializer);

            Func <SortDescriptor <Place>, bool, SortDescriptor <Place> > SortByRates = (SortDescriptor <Place> s, bool desc) => desc?s.Descending(d => d.Rating) : s;

            searchDescriptor
            .Index <Place>()
            .Query(x =>
                   x.Bool(b =>
                          b.Must(
                              m => m.Term(t => t.Field(f => f.OpeningHours.OpenNow).Value(openedOnly)),
                              m => m.GeoDistance(g =>
                                                 g.Distance(new Distance(distance, DistanceUnit.Kilometers))
                                                 .Location(lat, lng)
                                                 .Field(f => f.Geometry.Location))
                              )
                          ))
            .Sort(s => SortByRates(s.GeoDistance(g =>
                                                 g.Field(f => f.Geometry.Location)
                                                 .DistanceType(GeoDistanceType.Arc)
                                                 .Unit(DistanceUnit.Kilometers)
                                                 .Order(SortOrder.Ascending)
                                                 .Points(new GeoLocation(lat, lng))), descRates))
            .ScriptFields(x =>
                          x.ScriptField("distance", s => s.Source($"doc['geometry.location'].arcDistance({lat},{lng})")))
            .Take(10)
            .Source(true)
            ;

            var sss = dumper.Dump <SearchDescriptor <Place> >(searchDescriptor);

            var results = await client.SearchAsync <Place>(searchDescriptor);

            return(ToBestPlacesAround(results.Hits));
        }
        protected virtual async Task <SearchDescriptor <T> > ConfigureSearchDescriptorAsync(SearchDescriptor <T> search, IRepositoryQuery query, ICommandOptions options)
        {
            search ??= new SearchDescriptor <T>();

            query = ConfigureQuery(query.As <T>()).Unwrap();
            var indices = ElasticIndex.GetIndexesByQuery(query);

            if (indices?.Length > 0)
            {
                search.Index(String.Join(",", indices));
            }
            if (HasVersion)
            {
                search.SequenceNumberPrimaryTerm(HasVersion);
            }

            search.IgnoreUnavailable();
            search.TrackTotalHits();

            await ElasticIndex.QueryBuilder.ConfigureSearchAsync(query, options, search).AnyContext();

            return(search);
        }
Ejemplo n.º 31
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public async Task <ISearchResponse <T> > ToListAsync()
        {
            SearchDescriptor <T>    selector  = new SearchDescriptor <T>();
            BoolQueryDescriptor <T> boolQuery = new BoolQueryDescriptor <T>();

            if (_mustSelector.Count > 0)
            {
                boolQuery.Must(_mustSelector);
            }
            if (_mustNotSelector.Count > 0)
            {
                boolQuery.MustNot(_mustNotSelector);
            }
            if (!string.IsNullOrWhiteSpace(IndexName))
            {
                selector.Index(IndexName);
            }
            selector = selector.Query(q => q.Bool(b => boolQuery)).From(SkipCount ?? 0);
            if (TakeCount > 0)
            {
                selector.Size(TakeCount.Value);
            }
            if (sortor != null)
            {
                selector.Sort(sortor);
            }
            var response = await _searchContext.Context.SearchAsync <T>(selector);

            if (!response.IsValid)
            {
                _searchContext.Logger.LogError($"[Success:{response.ApiCall.Success}]\t{response.ApiCall.Uri}");
                _searchContext.Logger.LogError(response.ApiCall.DebugInformation);
                _searchContext.Logger.LogError(response.ApiCall.OriginalException?.Message);
            }
            return(response);
        }