public ISearchResponse <T> Query(QueryDes qd) { var s = new SearchDescriptor <T>().Index(_config.ES_INDEX).Type(_config.ES_TYPE).From(qd.From).Take(qd.Take) .Source(sr => GetSource(sr, qd)) .Query(q => GetQuery(q, qd)); if (qd.SortField != null) { if (qd.IsAsc) { s.Sort(st => st.Ascending(qd.SortField)); } else { s.Sort(st => st.Descending(qd.SortField)); } } if (qd.TerminateAfter > 0) // 设置单个节点上的数据查询截断阈值,提高响应速度(但会导致数据搜索不全面) { s.TerminateAfter(qd.TerminateAfter); } if (qd.Aggs != null) { s.Aggregations(agg => Get_Agg(agg, qd)); } if (qd.HLPreTag != null) { s.Highlight(hl => Get_HL(hl, qd)); } return(_client.Search <T>(s)); }
private QueryContainer GetQueryContainer(QueryContainerDescriptor <T> q, QueryDes qd) { var qcs = new List <QueryContainer>(); foreach (var p in qd.MultiFilters) { var qc = new QueryContainer(); switch (p.Key) { case 10: foreach (var s in p.Value) { qc |= GetQueryContainer_Generic(q, "<field name(id 10)>", s); } break; } qcs.Add(qc); } var res = qcs[0]; for (int i = 1; i < qcs.Count; i++) { res &= qcs[i]; } return(res); }
private QueryContainer GetQuery(QueryContainerDescriptor <T> q, QueryDes qd) { var input = qd.MultiFilters[10].First(); var score_idx = input.Length + 1; return(q.FunctionScore(fs => fs .BoostMode(FunctionBoostMode.Sum) .ScoreMode(FunctionScoreMode.Sum) .Functions(fun => fun .ScriptScore(ss => ss .Script(s => s .Inline("for(s in doc['scores']) {if(s.contains(\"" // scores: ["xxx-10","yyy-20",...] + input + "\")){ return 1000 * Double.parseDouble(s.substring(" + score_idx + ")) + _score + doc['oc_weight'].value ;}} return _score + doc['oc_weight'].value;") .Lang("painless"))) .FieldValueFactor(f => f .Filter(flt => flt.Term(t => t.Field("<field name>").Value("<field value>"))) .Factor(1) .Field("<field name whose value type is number>") .Missing(1) ) ) .Query(qq => qq.Bool(b => b .Filter(f => GetMultiFilterContainer(f, qd)) .Must(m => GetQueryContainer(m, qd)) ) ) )); }
private HighlightDescriptor <T> Get_HL(HighlightDescriptor <T> hl, QueryDes qd) => hl .PreTags(qd.HLPreTag) .PostTags(qd.HLPostTag) .Fields( f => f.Field("oc_code"), f => f.Field("oc_number") );
public ISourceFilter GetSource(SourceFilterDescriptor <T> sr, QueryDes qd) { if (qd.Srcs == null || qd.Srcs.Count == 0) { return(qd.IsInclude ? sr.ExcludeAll() : sr.IncludeAll()); } var arr = qd.Srcs.Select(fld => fld.ToString()).ToArray(); return(qd.IsInclude ? sr.Includes(f => f.Fields(arr)) : sr.Excludes(f => f.Fields(arr))); }
/// <summary> /// 获取聚合 /// </summary> /// <param name="agg"></param> /// <param name="ci"></param> /// <returns></returns> private IAggregationContainer Get_Agg(AggregationContainerDescriptor <T> agg, QueryDes qd) { var ls = qd.Aggs; if (ls == null) { return(agg); } foreach (var s in ls) { switch (s) { case 1: agg.Terms("m_area", t => t.Field("m_area").Size(33)); break; case 2: agg.Range("od_regm", r => r.Field("od_regm").Ranges(rs => rs.To(100), rs => rs.From(100).To(500), rs => rs.From(500).To(1000), rs => rs.From(1000))); break; case 3: agg.DateHistogram("od_regdate", t => t.Field("od_regdate").Interval(DateInterval.Year) .MinimumDocumentCount(1).ExtendedBounds(new DateTime(1900, 1, 1), DateTime.Now)); break; } } return(agg); }
private QueryContainer GetMultiFilterContainer(QueryContainerDescriptor <T> q, QueryDes qd) { if (qd.MultiFilters == null) { return(q); } var qcs = new List <QueryContainer>(qd.MultiFilters.Count); foreach (var p in qd.MultiFilters) { var qc = new QueryContainer(); switch (p.Key) { case 0: foreach (var s in p.Value) { qc |= q.Term(t => t.Field("<field name(id 0) whose value is not tokenized>").Value(s)); } break; case 1: foreach (var s in p.Value) { qc |= q.Prefix(pre => pre.Field("<field name(id 1) whose value is standard tokenized").Value(s)); } break; case 2: foreach (var s in p.Value) { var year = int.Parse(s); qc |= q.DateRange(d => d.Field("<field name(id 2) whose value has a datetime type").GreaterThanOrEquals(new DateTime(year)).LessThan(new DateTime(year + 1))); //qc |= q.DateRange(d => d.Field("<field name(id 2) whose value has a datetime format").GreaterThanOrEquals(s).LessThan((year+1).ToString()).Format("yyyy")); } break; case 3: foreach (var s in p.Value) { var segs = s.Split('-'); if (s[0] == '*') { qc |= q.Range(r => r.Field("<field name(id 3) whose value has a number type>").LessThan(double.Parse(segs[1]))); } else if (s[s.Length - 1] == '*') { qc |= q.Range(r => r.Field("<field name(id 3) whose value has a number type>").GreaterThanOrEquals(double.Parse(segs[0]))); } else { qc |= q.Range(r => r.Field("<field name(id 3) whose value has a number type>").GreaterThanOrEquals(double.Parse(segs[0])).LessThan(double.Parse(segs[1]))); } } break; case 4: var f = p.Value.FirstOrDefault(); if (f[0] == 't') { qc = q.Exists(e => e.Field("<field name(id 4)>")); } else if (f[0] == 'f') { qc = q.Bool(b => b.MustNot(m => m.Exists(e => e.Field("<field name(id 4)>")))); } break; case 5: foreach (var s in p.Value) { if (s[0] == 'm') { qc |= q.Regexp(r => r.Field("<field name(id 5)>").Value(@"\+?(00)?((86)|(86-))?1[345689]\d{9}")); // mobile phone } else if (s[0] == 't') { qc |= q.Regexp(r => r.Field("<field name(id 5)>").Value(@"\+?(00)?((86)|(86-))?((0\d{3}[\s/-])|(\(0\d{3})))?\d{7,8}")); // telephone } } break; } qcs.Add(qc); } var res = qcs[0]; for (int i = 1; i < qcs.Count; i++) { res &= qcs[i]; } return(res); }