发布网友 发布时间:2022-04-24 19:41
共1个回答
热心网友 时间:2022-05-03 09:23
不会java操作ES 只会DotNet
SDK使用NEST
代码如下
```
var response = Client.Search<Estimation>(s => s
.Analyzer("ik_max_word")
.From(pageSize * (pageIndex - 1))
.Size(pageSize)
.Highlight(h => h
.PreTags("<span>")
.PostTags("</span>")
.Fields(
a => a.Field(p => p.estimationTitle)
)
)
.Query(q => q
.FunctionScore(qf => qf
.Query(qfq => qfq
.Bool(qb => qb
.Filter(
qbf =>
{
if (isJianding == 1)
{
return qbf.Bool(b => b
.Should(
bs => bs.MatchPhrase(qbsm => qbsm.Field(f => f.estimationJianding).Query("1")),
bs => bs.MatchPhrase(qbsm => qbsm.Field(f => f.estimationJianding).Query("2"))
)
);
}
return null;
},
qbf =>
{
if (isCommend == 1)
{
return qbf.Bool(b => b
.Should(
bs => bs.MatchPhrase(qbsm => qbsm.Field(f => f.estimationCommend).Query("1")),
bs => bs.MatchPhrase(qbsm => qbsm.Field(f => f.estimationCommend).Query("3"))
)
);
}
if (isCommend == 2)
{
return qbf.Bool(b => b
.Should(
bs => bs.MatchPhrase(qbsm => qbsm.Field(f => f.estimationCommend).Query("2")),
bs => bs.MatchPhrase(qbsm => qbsm.Field(f => f.estimationCommend).Query("3"))
)
);
}
if (isCommend == 3)
{
return qbf.MatchPhrase(qbsm => qbsm.Field(f => f.estimationCommend).Query("3"));
}
return null;
}
)
.Must(
qbsmm => qbsmm.Match(qbsm => qbsm.Field(f => f.estimationTitle).Query(queryString).Boost(titleWeight))
)
.Should(
qbsmm => qbsmm.Match(qbsm => qbsm.Field(f => f.estimationContent).Query(queryString).Boost(contentWeight))
)
)
)
.ScoreMode(FunctionScoreMode.Sum)
.BoostMode(FunctionBoostMode.Multiply)
.Functions(f =>
{
string dateOffset = GetRedis.GetString(RedisKey.Estimation_ExDate_Offset, "2d");
string dateScale = GetRedis.GetString(RedisKey.Estimation_ExDate_Scale, "1d");
double dateDecay = GetRedis.GetDouble(RedisKey.Estimation_ExDate_Decay, 0.6);
double exdateWeight = GetRedis.GetDouble(RedisKey.Estimation_Time_ExDate_Weight, 1);
double hitFieldWeight = GetRedis.GetDouble(RedisKey.Estimation_Time_Field_Hits_Weight, 1);
double replyCntFieldWeight = GetRedis.GetDouble(RedisKey.Estimation_Time_Field_ReplyCnt_Weight, 1);
switch (sortBy)
{
case 1:
break;
case 2:
exdateWeight = GetRedis.GetDouble(RedisKey.Estimation_Popularity_ExDate_Weight, 1);
hitFieldWeight = GetRedis.GetDouble(RedisKey.Estimation_Popularity_Field_Hits_Weight, 1);
replyCntFieldWeight= GetRedis.GetDouble(RedisKey.Estimation_Popularity_Field_ReplyCnt_Weight, 1);
break;
case 3:
exdateWeight = GetRedis.GetDouble(RedisKey.Estimation_Commented_ExDate_Weight, 1);
hitFieldWeight = GetRedis.GetDouble(RedisKey.Estimation_Commented_Field_Hits_Weight, 1);
replyCntFieldWeight = GetRedis.GetDouble(RedisKey.Estimation_Commented_Field_ReplyCnt_Weight, 1);
break;
default:
break;
}
return f.ExponentialDate(b => b
.Field(ff => ff.estimationDate)
.Origin(DateMath.Now)
.Offset(dateOffset)
.Scale(dateScale)
.Decay(dateDecay)
.Weight(exdateWeight)
)
.FieldValueFactor(ss => ss
.Field(ff => ff.estimationHits)
.Factor(0.1)
.Missing(0)
.Weight(hitFieldWeight)
)
.FieldValueFactor(ss => ss
.Field(ff => ff.estimationReplyCnt)
.Factor(0.5)
.Missing(0)
.Weight(replyCntFieldWeight)
)
;
})
)
)
);