Ejemplo n.º 1
0
        public static ISearchResponse <Person> Search_Scroll(SearchParam p)
        {
            /**
             * if p.from >= 10000, we can only use this methods to get data
             * p.size must be less then 10000, and the better choices are 10, 20, 50, 100, 200, 500, 1000, 2000, 5000
             * */
            if (p.from % p.size != 0)
            {
                throw new Exception("p.from must be integrally divided by p.size");
            }
            var discard_count = p.from / p.size;
            int count         = 0;

            ISearchResponse <Person> resp = null;

            resp = ESClientInst.Client.Search <Person>(s => s.Scroll("1m").Index(p.index).Type(p.type).Take(p.size)
                                                       .Query(q => q.Match(m => m.Field("company.max").MinimumShouldMatch(MinimumShouldMatch.Percentage(80)).Query(p.keyword)))
                                                       .Sort(sd => p.isAsc ? sd.Ascending(p.sort.ToString()) : sd.Descending(p.sort.ToString())));

            string scroll_id = resp.ScrollId;

            while (count < discard_count && resp.Documents.Any() && scroll_id != null)
            {
                count++;

                resp = ESClientInst.Client.Scroll <Person>("1m", scroll_id);
                ESClientInst.Client.ClearScroll(cs => cs.ScrollId(scroll_id));
                scroll_id = resp.ScrollId;
            }
            return(resp);
        }
Ejemplo n.º 2
0
        public static ISearchResponse <Person> SearchMany(SearchParam p)
        {
            var resp = ESClientInst.Client.Search <Person>(s => s.Index(p.index).Type(p.type).From(p.from).Take(p.size)
                                                           .Sort(sd => GetHourseSortDescriptor(sd, p))
                                                           .Source(sr => GetSourceFilter(sr, p))
                                                           .Aggregations(agg => GetAggContainer(agg, p))
                                                           .Query(q => q.Terms(t => t.Field(p.batch_cond.Key.ToString()).Terms(p.batch_cond.Value)))
                                                           );

            return(resp);
        }
Ejemplo n.º 3
0
        public static ISearchResponse <Person> SearchByCom(SearchParam p)
        {
            var resp = ESClientInst.Client.Search <Person>(s => s.Index(p.index).Type(p.type).From(p.from).Take(p.size)
                                                           .Sort(sd => GetHourseSortDescriptor(sd, p))
                                                           .Source(sr => GetSourceFilter(sr, p))
                                                           .Aggregations(agg => GetAggContainer(agg, p))
                                                           .Highlight(hl => GetHLDescriptor(hl, p))
                                                           .Query(q => q.DisMax(d => d.Queries(dq => dq.Term(t => t.Field(f => f.company).Value(p.keyword).Boost(100)),
                                                                                               dq => dq.MatchPhrase(m => m.Field("company.max").Query(p.keyword).Boost(50)),
                                                                                               dq => dq.MatchPhrase(m => m.Field("company.std").Query(p.keyword).Boost(1)))))
                                                           );

            return(resp);
        }
Ejemplo n.º 4
0
        public static ISearchResponse <Person> SearchHouse(SearchParam p)
        {
            var resp = ESClientInst.Client.Search <Person>(s => s.Index(p.index).Type(p.type).From(p.from).Take(p.size)
                                                           .Sort(sd => GetHourseSortDescriptor(sd, p))
                                                           .Source(sr => GetSourceFilter(sr, p))
                                                           .Aggregations(agg => GetAggContainer(agg, p))
                                                           .Query(q => q.GeoDistance(g => g.Distance(p.distance, DistanceUnit.Kilometers)
                                                                                     .DistanceType(GeoDistanceType.Arc)
                                                                                     .Field(f => f.house)
                                                                                     .Location(p.lat, p.lon)))
                                                           );

            return(resp);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 按城市统计平均薪水
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public ISearchResponse <Person> AverageSalaryByCity(SearchParam p)
        {
            var resp = ESClientInst.Client.Search <Person>(s => s.Index(p.index).Type(p.type).Take(0)
                                                           .Aggregations(agg => agg
                                                                         .Terms("city", t => t
                                                                                .Field(f => f.city)
                                                                                .Size(100)
                                                                                .MinimumDocumentCount(50)
                                                                                .Aggregations(sa => sa
                                                                                              .Average("average_salary", ave => ave
                                                                                                       .Field(f => f.salary))
                                                                                              )
                                                                                )
                                                                         .AverageBucket("ave_sal_city_buck", ab => ab.BucketsPath("city>average_salary"))
                                                                         )
                                                           );

            return(resp);
        }
Ejemplo n.º 6
0
        /**
         * statistics
         *
         * */

        /// <summary>
        /// 给定公司关键词,按毕业年份统计平均薪水
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public ISearchResponse <Person> AverageSalaryByYear(SearchParam p)
        {
            var resp = ESClientInst.Client.Search <Person>(s => s.Index(p.index).Type(p.type).Take(0)
                                                           .Query(q => q.MatchPhrase(m => m.Field("company.max").Query(p.keyword)))
                                                           .Aggregations(agg => agg
                                                                         .DateHistogram($"average_salary_year", dh => dh
                                                                                        .Field(f => f.graduate)
                                                                                        .Interval(DateInterval.Year)
                                                                                        .Aggregations(sa => sa
                                                                                                      .Average("average_salary", ave => ave
                                                                                                               .Field(f => f.salary)
                                                                                                               //.Script(scr => scr.Source("doc['salary']").Lang("expression"))
                                                                                                               )
                                                                                                      )
                                                                                        )
                                                                         )
                                                           );

            return(resp);
        }
Ejemplo n.º 7
0
 private static ISourceFilter GetSourceFilter(SourceFilterDescriptor <Person> sr, SearchParam p)
 {
     if (p.isInclude)
     {
         if (p.sources == null || p.sources.Count == 0)
         {
             return(sr.ExcludeAll());
         }
         else
         {
             return(sr.Includes(inc => inc.Fields(p.sources.Select(s => s.ToString()).ToArray())));
         }
     }
     else
     {
         if (p.sources == null || p.sources.Count == 0)
         {
             return(sr.IncludeAll());
         }
         else
         {
             return(sr.Excludes(exc => exc.Fields(p.sources.Select(s => s.ToString()).ToArray())));
         }
     }
 }
Ejemplo n.º 8
0
 private static SortDescriptor <Person> GetHourseSortDescriptor(SortDescriptor <Person> sd, SearchParam p)
 {
     return(sd.GeoDistance(g => g.DistanceType(GeoDistanceType.Arc)
                           .Points(new GeoLocation(p.lat, p.lon))
                           .Field(f => f.house)
                           .Order(SortOrder.Ascending)));
 }
Ejemplo n.º 9
0
        private static IAggregationContainer GetAggContainer(AggregationContainerDescriptor <Person> agg, SearchParam p)
        {
            if (p.aggs != null)
            {
                foreach (var a in p.aggs)
                {
                    switch (a)
                    {
                    case PersonEnum.age:
                        agg.Range("age", r => r.Field(f => f.age).Ranges(rs => rs.To(20),
                                                                         rs => rs.From(20).To(30),
                                                                         rs => rs.From(30).To(40),
                                                                         rs => rs.From(40).To(50),
                                                                         rs => rs.From(50).To(60),
                                                                         rs => rs.From(60)));
                        break;

                    case PersonEnum.graduate:
                        agg.DateHistogram("graduate", d => d.Field(f => f.graduate)
                                          .Interval(DateInterval.Year)
                                          .MinimumDocumentCount(1)
                                          .ExtendedBounds("1980", "2050"));
                        break;

                    case PersonEnum.isMan:
                        agg.Terms("isMan", t => t.Field(f => f.isMan));
                        break;

                    case PersonEnum.city:
                        agg.Terms("city", t => t.Field(f => f.city));
                        break;
                    }
                }
            }
            return(agg);
        }
Ejemplo n.º 10
0
 private static HighlightDescriptor <Person> GetHLDescriptor(HighlightDescriptor <Person> hl, SearchParam p)
 {
     return(hl.PreTags("<b>")
            .PostTags("</b>")
            .Fields(f => f.Field("company.std"),
                    f => f.Field("company.max"),
                    f => f.Field(fld => fld.company)));
 }
Ejemplo n.º 11
0
 public static ESWrapper <Person> SearchAndHandle(
     SearchParam p,
     Func <SearchParam, ISearchResponse <Person> > search,
     Func <ISearchResponse <Person>, ESWrapper <Person> > handle) =>
 handle(search(p));