package com.cmall.search.es;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
public class QueryDSLTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
searchsCustomScoreQueryBuilder();
}
public static void searchsCustomScoreQueryBuilder() {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder query = QueryBuilders.boolQuery();
query.should(QueryBuilders.queryString("男士文化衫").defaultField(
"cnGoodsName"));
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders
.functionScoreQuery(query);
functionScoreQueryBuilder.add(ScoreFunctionBuilders
.scriptFunction("_score * (1d / (1d + exp(-log(doc['top'].value+1d)/5)))"));
searchSourceBuilder.query(functionScoreQueryBuilder);
System.out.println(searchSourceBuilder);
}
}
{
"query" : {
"function_score" : {
"query" : {
"bool" : {
"should" : {
"query_string" : {
"query" : "男士文化衫",
"default_field" : "cnGoodsName",
"analyzer" : "ik_max_word_synonym"
}
}
}
},
"functions" : [ {
"script_score" : {
"script" : "_score * (1d / (1d + exp(-log(doc['top'].value+1d)/5)))"
}
} ]
}
}
}