Example #1
0
        public Task <MultiSearchResponse> MultiSearchAsync(string[] matchTerms)
        {
            var searchRequests = matchTerms.ToDictionary(a => a, a => new SearchRequest <Avatar>
            {
                Query = new MatchQuery
                {
                    Field = new Field("FirstName"),
                    Query = a
                },
                //new TermQuery
                //{
                //    Field = "FirstName",
                //    Value = value
                //},
            } as ISearchRequest);

            var multiSearchRequest = new MultiSearchRequest
            {
                Operations = searchRequests
            };

            // Chain dynamically
            var accumulatedQueries = AccumulatedQueries(matchTerms);

            return(_elasticClient.MultiSearchAsync(index: null, accumulatedQueries));

            // Chain statically
            //return _elasticClient.MultiSearchAsync(null, ms => ms
            //    .Search<Avatar>("Hamid", s => s.MatchAll())
            //    .Search<Avatar>("Jimmy", s => s.MatchAll())
            //);

            // Using query dictionary
            //return _elasticClient.MultiSearchAsync(multiSearchRequest);
        }
Example #2
0
        public async Task Test_MuitpleSearch()
        {
            var searchModel = new MultiSearchRequest(Indices.Index("megacorp", "myindex"));
            var response    = await Client.MultiSearchAsync(searchModel);

            Assert.True(response.IsValid);
        }
        public Dictionary <string, int> CountForTags(List <string> tagIds, string tagField)
        {
            if (!tagIds.Any())
            {
                return(new Dictionary <string, int>());
            }
            var resultDic = new Dictionary <string, int>();

            foreach (var tagIdsBatch in tagIds.Batch(1000))
            {
                var mRequest = new MultiSearchRequest {
                    Operations = new Dictionary <string, ISearchRequest>()
                };
                foreach (var tagId in tagIdsBatch)
                {
                    var termQuery = new TermQuery
                    {
                        Field = MapDocumentObjectName(tagField),
                        Value = tagId
                    };
                    mRequest.Operations.Add(tagId, new SearchRequest <DocumentElastic> {
                        Query = new QueryContainer(termQuery), Size = 0
                    });
                }
                var result = Client.MultiSearch(mRequest);
                ResponseValidator(result);
                tagIdsBatch.ToList().ForEach(tid => resultDic.Add(tid, Convert.ToInt32(result.GetResponse <DocumentElastic>(tid).Total)));
            }
            return(resultDic);
        }
        public Dictionary <string, long> CountAll(List <string> indexNames)
        {
            var resultDic = new Dictionary <string, long>();

            if (!indexNames.Any())
            {
                return(resultDic);
            }

            var mRequest = new MultiSearchRequest {
                Operations = new Dictionary <string, ISearchRequest>()
            };

            foreach (var indexName in indexNames)
            {
                var sr = new SearchRequest(indexName);
                mRequest.Operations.Add(indexName, new SearchRequest <DocumentElastic>(indexName)
                {
                    Query = new MatchAllQuery(), Size = 0
                });
            }
            var result = Client.MultiSearch(mRequest);

            ResponseValidator(result);

            indexNames.ForEach(indexName => resultDic.Add(indexName, Convert.ToInt32(result.GetResponse <DocumentElastic>(indexName).Total)));
            return(resultDic);
        }
Example #5
0
        public MultiSearchRequestTest()
        {
            var request = new MultiSearchRequest
            {
                Operations = new Dictionary <string, ISearchRequest>
                {
                    { "first-search", new SearchRequest
                      {
                          Indices    = new IndexNameMarker[] { "my-index" },
                          Query      = Query <ElasticsearchProject> .Term(p => p.Name, "NEST"),
                          SearchType = SearchType.Scan,
                          Preference = "local",
                          Routing    = new string[]
                          {
                              "routingvalue"
                          }
                      } },
                    { "second-search", new SearchRequest
                      {
                          Query = Query <ElasticsearchProject> .Term(p => p.Contributors.First().LastName, "Laarman")
                      } },
                }
            };
            var response = this._client.MultiSearch(request);

            this._status = response.ConnectionStatus;
        }
Example #6
0
        /// <summary>
        /// Get the Aggregated token count per tag
        /// </summary>
        /// <param name="interpretedFields">DON'T PUT THE DOCUMENTELASTIC->TEXT FIELD HERE! RATHER THE INTERPRETEDFIELDS</param>
        /// <param name="ngramCount"></param>
        /// <param name="tagIds">the tagIds to run on</param>
        /// <param name="tagField"></param>
        /// <returns></returns>
        public Dictionary <string, int> CountForWord(List <string> interpretedFields, int ngramCount, List <string> tagIds, string tagField)
        {
            if (!tagIds.Any())
            {
                return(new Dictionary <string, int>());
            }
            var resultDic = new Dictionary <string, int>();

            foreach (var tagIdsBatch in tagIds.Batch(1000))
            {
                var mRequest = new MultiSearchRequest {
                    Operations = new Dictionary <string, ISearchRequest>()
                };
                foreach (var tagId in tagIdsBatch)
                {
                    var desc    = new SearchDescriptor <DocumentElastic>();
                    var aggDesc = new AggregationContainerDescriptor <DocumentElastic>();

                    foreach (var interpretedField in interpretedFields)
                    {
                        aggDesc.Sum(
                            interpretedField,
                            ad => ad.Field($"{interpretedField}.1{_tokenCountSuffix}"));
                    }

                    desc.Size(0);
                    desc.Aggregations(a => aggDesc);

                    if (!string.IsNullOrEmpty(tagId))
                    {
                        desc.Query(q => q.Term($"{tagField}", tagId));
                    }
                    else
                    {
                        desc.Query(q => q.MatchAll());
                    }

                    mRequest.Operations.Add(tagId, desc);
                }
                var result = Client.MultiSearch(mRequest);
                ResponseValidator(result);
                tagIdsBatch.ToList().ForEach(tid => resultDic.Add(tid, Convert.ToInt32(result.GetResponse <DocumentElastic>(tid).Aggregations.Sum(a => Convert.ToInt32(((ValueAggregate)a.Value).Value))) - (ngramCount - 1)));
            }
            return(resultDic);
        }
		public MultiSearchRequestTest()
		{
			var request = new MultiSearchRequest
			{
				Operations = new Dictionary<string, ISearchRequest>
				{
					{ "first-search", new SearchRequest
					{
						Indices = new IndexNameMarker[] {"my-index"},
						Query = Query<ElasticsearchProject>.Term(p=>p.Name, "NEST"),
						SearchType = SearchType.Scan,
						Preference = "local",
						Routing = new string[] {"routingvalue"}
						
					}},
					{ "second-search", new SearchRequest
					{
						Query = Query<ElasticsearchProject>.Term(p=>p.Contributors.First().LastName, "Laarman")
					}},
				}
			};
			var response = this._client.MultiSearch(request);
			this._status = response.ConnectionStatus;
		}
Example #8
0
 public virtual Task <MultiSearchResponse> GetAsync(MultiSearchRequest request) =>
 Client.SendAsync <MultiSearchResponse>(request);
        public List <SearchResult> Search(string query, string category = "")
        {
            var client = GetElasticClient();

            // we'll use multisearch here to get results from
            // multiple indexes
            // https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html
            // .net example:
            // https://discuss.elastic.co/t/elasticsearch-net-and-multisearch/96602/4
            //
            // USE NEST if wanting to return strongly typed objects
            // here lowlevel client is used

            /*
             * var searchResponse = client.LowLevel.Msearch<DynamicResponse>(PostData.MultiJson(new object[]
             * {
             *  new { index = "areas" },
             *  new { query = new { match = new { name = query } }, from = 0, size = 5 },
             *  new { index = "crags" },
             *  new { query = new { match = new { name = query } }, from = 0, size = 5 },
             *  new { index = "sectors" },
             *  new { query = new { match = new { name = query } }, from = 0, size = 5 },
             *  new { index = "zlaggables" },
             *  new { query = new { match = new { name = query } }, from = 0, size = 5 }
             * }));
             */

            // to add highlights:
            // https://stackoverflow.com/questions/28248681/highlight-all-fields-nest-elasticsearch


            // if category is defined, limit search to that categorys index
            var myIndexes = _indexes;
            var pageSize  = 5; // by default we get 5 per category

            if (!string.IsNullOrWhiteSpace(category))
            {
                pageSize  = 50;
                myIndexes = new Dictionary <string, string>
                {
                    { category, _indexes[category] }
                };
            }

            // create request per index
            var requests = new Dictionary <string, ISearchRequest>();

            foreach (var index in myIndexes)
            {
                var request = GetSearchRequest(query, index.Key, pageSize);
                requests.Add(index.Key, request);
            }

            // create and execute multisearch
            var multiSearch = new MultiSearchRequest
            {
                Operations = requests
            };
            var multiResponse = (MultiSearchResponse)client.MultiSearch(multiSearch);

            // create searchresult objects from elastic search results
            var results = new List <SearchResult>();
            var i       = -1; // we track the id for indexes. since response don't have the index name we need to get it from the collection

            foreach (var res in multiResponse.GetResponses <object>())
            {
                i++;
                results.Add(new SearchResult()
                {
                    Category = myIndexes.Keys.ToList()[i],
                    Count    = (int)res.Total,
                    Items    = res.Documents.ToList()
                });
            }
            return(results);
        }
Example #10
0
        public async Task <IActionResult> GetCategoryNewsAsync([FromHeader] String source,
                                                               [FromRoute] SiteIdRoute route,
                                                               [FromQuery] PagerElastic item)
        {
            var response = new Response <List <NewsListResponse> >();

            try {
                //根据当前系统版本号区分是否显示视频
                var entity = source.ToObject <HeaderSource>();

                // 针对安卓版本判断
                var  lastVers        = new Version("1.0.4"); // 最后版本
                var  defalutVersCode = 5;                    //当前安卓版本code
                var  newVers         = new Version();
                var  newVersInt      = 0;
                bool newsBool;
                try {
                    newVers = new Version(entity.SystemVers); // 最新版本
                }
                catch (Exception) {
                    newVersInt = entity.SystemVers.ToInt();
                }
                if (newVersInt == 0)
                {
                    newsBool = entity.Device == "android" && newVers <= lastVers;
                }
                else
                {
                    newsBool = entity.Device == "android" && newVersInt <= defalutVersCode;
                }

                //判断是否展示视频
                if (newsBool)
                {
                    var request = new SearchRequest <WebNewsDoc>(_IWebNewsElastic.IndexName)
                    {
                        TrackTotalHits = true,
                        Query          = new BoolQuery()
                        {
                            Must = new QueryContainer[] {
                                new TermQuery()
                                {
                                    Field = "siteId",
                                    Value = route.mark
                                } &&
                                new TermQuery()
                                {
                                    Field = "categoryId",
                                    Value = route.id
                                }
                            },
                            MustNot = new QueryContainer[] {//过滤掉类型为视频的数据
                                new TermQuery()
                                {
                                    Field = "contentType",
                                    Value = 2
                                }
                            },
                        },
                        Source = new Union <bool, ISourceFilter>(new SourceFilter {
                            Excludes = new[] { "contents" }
                        }),
                        Sort = new List <ISort>()
                        {
                            new FieldSort {
                                Field = "pushTime", Order = SortOrder.Descending
                            },
                            new FieldSort {
                                Field = "categorySort", Order = SortOrder.Ascending
                            }
                        },
                        Size = item.PageSize
                    };
                    if (item.PageIndex != null)
                    {
                        request.From        = 0;
                        request.SearchAfter = item.PageIndex.Split(",");
                    }
                    var result = await this._IWebNewsElastic.Client
                                 .SearchAsync <NewsListResponse>(request);

                    if (result.ApiCall.Success && result.ApiCall.HttpStatusCode == 200)
                    {
                        if (result.Documents.Count > 0)
                        {
                            response.Code    = true;
                            response.Data    = result.Documents.ToList();
                            response.Message = $"返回{result.Documents.Count}条数据";
                            response.Other   = string.Join(',', result.Hits.LastOrDefault().Sorts);
                        }
                    }
                }
                else
                {
                    //根据新闻视频2:1比例设置Size
                    int?newsSize = 0, newsRido = 2;
                    int?videoSize = 0;

                    if (item.PageSize < 10)
                    {
                        item.PageSize = 10;
                    }
                    if (item.PageSize % newsRido == 0)
                    {
                        newsSize = item.PageSize / newsRido + 2;
                    }
                    else
                    {
                        newsSize = (item.PageSize + 1) / newsRido + 2;
                    }
                    videoSize = item.PageSize - newsSize;
                    int?     from         = null;
                    string[] searechAfter = null;

                    if (item.PageIndex != null)
                    {
                        from         = 0;
                        searechAfter = item.PageIndex.Split('|');
                    }
                    //es多重查询
                    var request = new MultiSearchRequest()
                    {
                        TotalHitsAsInteger = true,
                        Operations         = new Dictionary <string, ISearchRequest>
                        {
                            { "news", new SearchRequest <NewsListResponse>(_IWebNewsElastic.IndexName)
                              {
                                  Query = new BoolQuery()
                                  {
                                      Must = new QueryContainer[] {
                                          new TermQuery {
                                              Field = "siteId",
                                              Value = route.mark
                                          } &&
                                          new TermQuery {
                                              Field = "categoryId",
                                              Value = route.id
                                          } &&
                                          new TermQuery {
                                              Field = "contentType",
                                              Value = 1
                                          }
                                      }
                                  },
                                  Sort = new List <ISort>()
                                  {
                                      new FieldSort()
                                      {
                                          Field = "pushTime", Order = SortOrder.Descending
                                      },
                                      new FieldSort()
                                      {
                                          Field = "categorySort", Order = SortOrder.Ascending
                                      }
                                  },
                                  From        = from,
                                  Size        = newsSize,
                                  SearchAfter = item.PageIndex != null?searechAfter[0].Split(','):null
                              } },
                            { "video", new SearchRequest <NewsListResponse>(_IWebNewsElastic.IndexName)
                              {
                                  Query = new BoolQuery()
                                  {
                                      Must = new QueryContainer[] {
                                          new TermQuery {
                                              Field = "siteId",
                                              Value = route.mark
                                          } &&
                                          new TermQuery {
                                              Field = "categoryId",
                                              Value = route.id
                                          } &&
                                          new TermQuery {
                                              Field = "contentType",
                                              Value = 2
                                          }
                                      }
                                  },
                                  Sort = new List <ISort>()
                                  {
                                      new FieldSort()
                                      {
                                          Field = "pushTime", Order = SortOrder.Descending
                                      },
                                      new FieldSort()
                                      {
                                          Field = "categorySort", Order = SortOrder.Ascending
                                      }
                                  },
                                  From        = from,
                                  Size        = videoSize,
                                  SearchAfter = item.PageIndex != null?(searechAfter.Count() > 1?searechAfter[1].Split(','):null):null
                              } }
                        }
                    };
                    var result = await this._IWebNewsElastic.Client.MultiSearchAsync(request); //es多重查询

                    var newsResult  = result.GetResponse <NewsListResponse>("news");           //新闻
                    var videoResult = result.GetResponse <NewsListResponse>("video");          //视频

                    if (newsResult != null && newsResult.ApiCall.Success && videoResult != null && videoResult.ApiCall.Success)
                    {
                        if (newsResult.Documents.Count > 0 || videoResult.Documents.Count > 0)
                        {
                            response.Code = true;
                            //根据ContentType返回结果 2视频 1新闻
                            //每隔两个新闻放置一个视频
                            var vieoList  = videoResult.Documents.Where(c => !string.IsNullOrEmpty(c.ImagePath)).ToList(); //视频结果集
                            var newsList  = newsResult.Documents.ToList();                                                 //新闻结果集
                            var newsIndex = 0;
                            var indexof   = 0;
                            for (int i = 0; i < vieoList.Count; i++)
                            {
                                if (i == 0)
                                {
                                    indexof += 2;
                                }
                                else
                                {
                                    indexof += 3;
                                }
                                if (newsList.Count == item.PageSize)
                                {
                                    break;
                                }
                                if (indexof < newsList.Count)
                                {
                                    newsList.Insert(indexof, vieoList[newsIndex]);
                                }
                                else
                                {
                                    newsList.Insert(newsList.Count, vieoList[newsIndex]);
                                }
                                newsIndex++;
                            }
                            response.Data = newsList;
                            if (newsResult.Hits.Count > 0 && videoResult.Hits.Count == 0)
                            {
                                response.Other = string.Join(",", newsResult.Hits.LastOrDefault().Sorts);
                            }
                            if (newsResult.Hits.Count > 0 && videoResult.Hits.Count > 0)
                            {
                                response.Other = string.Join(",", newsResult.Hits.LastOrDefault().Sorts) + "|" + string.Join(",", videoResult.Hits.LastOrDefault().Sorts);
                            }
                            response.Message = $"返回{newsList.Count}条数据";
                        }
                        else
                        {
                            return(NoContent());
                        }
                    }
                    else
                    {
                        return(NoContent());
                    }
                }
            }
            catch (Exception ex) {
                response.SetError(ex, this._ILogger);
            }
            return(response.ToHttpResponse());
        }