Solr 使用权重 实现按照特定值排序

需求

solr中需要排序的字段()为字符类型的
现需根据demoField值进行排序,需按照下列值出现的顺序排序( 当demoField值为zValue的时候更靠前)

  • zValue
  • High
  • Low
  • aValue

如果直接使用solr的sort进行排序则会按照值的字符比较顺序进行排序。

解决方案

不使用solr的sort,改为使用权重进行排序
主要思路是使用solr中的if(判断)、eq(比较)、sum(合计)函数对demoField权重进行计算。

如要实现需求则可以这样写solr查询参数

defType=edismax&boost=sum(if(eq(demoField,'zValue'),4,0),if(eq(demoField,'High'),3,0),if(eq(demoField,'Low'),2,0),if(eq(aValue,'aValue'),1,0))

对应java代码

 
SolrQuery params = new SolrQuery();
List<String> demoFields = Arrays.asList(new String[]{"zValue", "High", "Low", "aValue"});
int edismaxValue = demoFields.size();
if(demoFields.size() > 0){
    StringBuilder edismaxBoostBuilder = new StringBuilder();
    edismaxBoostBuilder.append("sum(");
    for(String demoValue : demoFields){
        edismaxBoostBuilder.append("if(eq(demoField,").append("'").append(demoValue).append("'").append("),").append(edismaxValue--).append(",0),");
    }
    edismaxBoostBuilder.append("0)");
    // 设置权重排序
    params.setParam("defType", "edismax");
    params.setParam("boost", edismaxBoostBuilder.toString());
}
// 一定要移除排序
// params.removeSort("");

对应solr查询界面

请添加图片描述

附录

权重(edismax) 可用字段说明

  q.alt: 当q字段为空时,用于设置缺省的query,通常设置q.alt为*:*。 如q.alt = title:计算机

  qf:query fields,指定solr从哪些field中搜索。 如qf=title

  mm: Minimum ‘Should’ Match。 Solr支持三种查询clause,即“必须出现”, “不能出现”和“可以出现”,分别对应于AND, -, OR

  pf: boosting phrases over words。用于指定一组field,当query完全匹配pf指定的某一个field时,来进行boost,给搜索匹配到的字段打分  如pf =字段1^0.5 字段2^0.2  

  ps: Phrase Slop. 短语坡度。短语查询的坡度量用在pf字段,影响boost。

  qs:Query Phrase Slop。查询短语坡度。查询短语坡度是指短语查询明确包含用户查询的字符串(在qf字段,影响匹配)。

  tie:tie breaker。float值作为决胜局中DisjunctionMaxQueries使用(应该是远小于1)。

  bq: Boost Query。对某个field的value进行boost,例如brand:xq^5.0。

  bf :Boost Functions。用函数的方式计算boost。

  uf:User Fields。用户字段。制定模式的字段可以被用户显示的查询。此参数支持通配符。

  pf2:Phrase bigram fields。短语两字母字段。e.g. “the brown fox jumped” is queried as “the brown” “brown fox” “fox jumped”。

  pf3:Phrase trigram fields。短语三字母字段。e.g. “the brown fox jumped” is queried as “the brown fox” “brown fox jumped”。

  ps2:短语两字母坡度。如果未指定,将使用”ps”。

  ps3:短语三字母坡度。如果未指定,将使用”ps”。

  boost:Boost Function, multiplicative。作为bf,score=bf*score。bf =sum(div(字段,100),1),

  stopwords:单词停用,true 或false。

  lowercaseOperators:此参数用于控制小写单词作为布尔运算符,如”and” and “or”。设置与lowercaseOperators= true来允许此。默认为true。

solr函数

ifEnables conditional function queries. In if(test,value1,value2):test is or refers to a logical value or expression that returns a logical value (TRUE or FALSE).value1 is the value that is returned by the function if test yields TRUE.value2 is the value that is returned by the function if test yields FALSE.An expression can be any function which outputs boolean values, or even functions returning numeric values, in which case value 0 will be interpreted as false, or strings, in which case empty string is interpreted as false.if(termfreq (cat,'electronics'), popularity,42): This function checks each document for the to see if it contains the term “electronics” in the cat field. If it does, then the value of the popularity field is returned, otherwise the value of 42 is returned.
sumReturns the sum of multiple values or functions, which are specified in a comma-separated list. add(…) may be used as an alias for this functionsum(x,y,…) sum(x,1)``sum(x,y)``sum(sqrt(x),log(y),z,0.5)``add(x,y)

The following functions are boolean – they return true or false. They are mostly useful as the first argument of the if function, and some of these can be combined. If used somewhere else, it will yield a ‘1’ or ‘0’.

FunctionDescriptionSyntax Examples
andReturns a value of true if and only if all of its operands evaluate to true.and(not (exists (popularity)), exists (price)): returns true for any document which has a value in the price field, but does not have a value in the popularity field
orA logical disjunction.or(value1,value2): TRUE if either value1 or value2 is true.
xorLogical exclusive disjunction, or one or the other but not both.xor(field1,field2) returns TRUE if either field1 or field2 is true; FALSE if both are true.
notThe logically negated value of the wrapped function.not(exists(author)): TRUE only when exists(author) is false.
existsReturns TRUE if any member of the field exists.exists(author) returns TRUE for any document has a value in the “author” field. exists(query(price:5.00)) returns TRUE if “price” matches “5.00”.
gt, gte, lt, lte, eq5 comparison functions: Greater Than, Greater Than or Equal, Less Than, Less Than or Equal, Equalif(lt(ms(mydatefield),315569259747),0.8,1) translates to this pseudocode: if mydatefield < 315569259747 then 0.8 else 1
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值