本文整理汇总了Java中org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder类的典型用法代码示例。如果您正苦于以下问题:Java FunctionScoreQueryBuilder类的具体用法?Java FunctionScoreQueryBuilder怎么用?Java FunctionScoreQueryBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FunctionScoreQueryBuilder类属于org.elasticsearch.index.query.functionscore包,在下文中一共展示了FunctionScoreQueryBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: searchCity
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
@Override
public List<City> searchCity(Integer pageNumber,
Integer pageSize,
String searchContent) {
// 分页参数
Pageable pageable = new PageRequest(pageNumber, pageSize);
// Function Score Query
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
.add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("cityname", searchContent)),
ScoreFunctionBuilders.weightFactorFunction(1000))
.add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("description", searchContent)),
ScoreFunctionBuilders.weightFactorFunction(100));
// 创建搜索 DSL 查询
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withPageable(pageable)
.withQuery(functionScoreQueryBuilder).build();
LOGGER.info("\n searchCity(): searchContent [" + searchContent + "] \n DSL = \n " + searchQuery.getQuery().toString());
Page<City> searchPageResults = cityRepository.search(searchQuery);
return searchPageResults.getContent();
}
示例2: minMaxQuery
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
private SearchResponse minMaxQuery(ScoreMode scoreMode, int minChildren, Integer maxChildren) throws SearchPhaseExecutionException {
HasChildQueryBuilder hasChildQuery = hasChildQuery(
"child",
QueryBuilders.functionScoreQuery(constantScoreQuery(QueryBuilders.termQuery("foo", "two")),
new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{
new FunctionScoreQueryBuilder.FilterFunctionBuilder(weightFactorFunction(1)),
new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("foo", "three"), weightFactorFunction(1)),
new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("foo", "four"), weightFactorFunction(1))
}).boostMode(CombineFunction.REPLACE).scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM), scoreMode)
.minMaxChildren(minChildren, maxChildren != null ? maxChildren : HasChildQueryBuilder.DEFAULT_MAX_CHILDREN);
return client()
.prepareSearch("test")
.setQuery(hasChildQuery)
.addSort("_score", SortOrder.DESC).addSort("id", SortOrder.ASC).get();
}
示例3: testUnsupportedQueries
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
public void testUnsupportedQueries() {
RangeQueryBuilder rangeQuery1 = new RangeQueryBuilder("field").from("2016-01-01||/D").to("2017-01-01||/D");
RangeQueryBuilder rangeQuery2 = new RangeQueryBuilder("field").from("2016-01-01||/D").to("now");
PercolatorFieldMapper.verifyQuery(rangeQuery1);
expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(rangeQuery2));
PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(rangeQuery1));
expectThrows(IllegalArgumentException.class, () ->
PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(rangeQuery2)));
PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder((rangeQuery1)));
expectThrows(IllegalArgumentException.class, () ->
PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder(rangeQuery2)));
PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(rangeQuery1, new MatchAllQueryBuilder()));
expectThrows(IllegalArgumentException.class, () ->
PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(rangeQuery2, new MatchAllQueryBuilder())));
PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(rangeQuery1, new RandomScoreFunctionBuilder()));
expectThrows(IllegalArgumentException.class, () ->
PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(rangeQuery2, new RandomScoreFunctionBuilder())));
HasChildQueryBuilder hasChildQuery = new HasChildQueryBuilder("_type", new MatchAllQueryBuilder(), ScoreMode.None);
expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasChildQuery));
expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery)));
HasParentQueryBuilder hasParentQuery = new HasParentQueryBuilder("_type", new MatchAllQueryBuilder(), false);
expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasParentQuery));
expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasParentQuery)));
}
示例4: searchGoods
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
/**
*
*
*
* @param pageNumber
* @param pageSize
* @param searchContent
* @return
*/
public List<GoodsModel> searchGoods(Integer pageNumber,
Integer pageSize,
String searchContent) {
// 分页参数
Pageable pageable = new PageRequest(pageNumber, pageSize);
// Function Score Query
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
.add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("goodsName", searchContent)),ScoreFunctionBuilders.weightFactorFunction(1000))
.add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("description", searchContent)),ScoreFunctionBuilders.weightFactorFunction(1000));
// 创建搜索 DSL 查询
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withPageable(pageable)
.withQuery(functionScoreQueryBuilder).build();
logger.info("\n searchGoods(): searchContent [" + searchContent + "] \n DSL = \n " + searchQuery.getQuery().toString());
Page<GoodsModel> searchPageResults = goodsESDocRepository.search(searchQuery);
return searchPageResults.getContent();
}
示例5: buildFunctionScoreQuery
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
protected QueryBuilder buildFunctionScoreQuery(final String query, final QueryBuilder queryBuilder) {
final List<FunctionScoreQueryBuilder.FilterFunctionBuilder> flist = new ArrayList<>();
if (isSingleWordQuery(query) && !isHiraganaQuery(query)) {
flist.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.prefixQuery(FieldNames.TEXT, query),
ScoreFunctionBuilders.weightFactorFunction(prefixMatchWeight)));
}
flist.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(ScoreFunctionBuilders.fieldValueFactorFunction(FieldNames.DOC_FREQ)
.missing(0.1f).modifier(FieldValueFactorFunction.Modifier.LOG2P).setWeight(1.0F)));
flist.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(ScoreFunctionBuilders.fieldValueFactorFunction(FieldNames.QUERY_FREQ)
.missing(0.1f).modifier(FieldValueFactorFunction.Modifier.LOG2P).setWeight(1.0F)));
flist.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(ScoreFunctionBuilders.fieldValueFactorFunction(FieldNames.USER_BOOST)
.missing(1f).setWeight(1.0F)));
final FunctionScoreQueryBuilder functionScoreQueryBuilder =
QueryBuilders.functionScoreQuery(queryBuilder,
flist.toArray(new FunctionScoreQueryBuilder.FilterFunctionBuilder[flist.size()]));
functionScoreQueryBuilder.boostMode(CombineFunction.REPLACE);
functionScoreQueryBuilder.scoreMode(FunctionScoreQuery.ScoreMode.MULTIPLY);
return functionScoreQueryBuilder;
}
示例6: getCitySearchQuery
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
/**
* 根据搜索词构造搜索查询语句
*
* 代码流程:
* - 权重分查询
* - 短语匹配
* - 设置权重分最小值
* - 设置分页参数
*
* @param pageNumber 当前页码
* @param pageSize 每页大小
* @param searchContent 搜索内容
* @return
*/
private SearchQuery getCitySearchQuery(Integer pageNumber, Integer pageSize,String searchContent) {
// 短语匹配到的搜索词,求和模式累加权重分
// 权重分查询 https://www.elastic.co/guide/cn/elasticsearch/guide/current/function-score-query.html
// - 短语匹配 https://www.elastic.co/guide/cn/elasticsearch/guide/current/phrase-matching.html
// - 字段对应权重分设置,可以优化成 enum
// - 由于无相关性的分值默认为 1 ,设置权重分最小值为 10
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
.add(QueryBuilders.matchPhraseQuery("name", searchContent),
ScoreFunctionBuilders.weightFactorFunction(1000))
.add(QueryBuilders.matchPhraseQuery("description", searchContent),
ScoreFunctionBuilders.weightFactorFunction(500))
.scoreMode(SCORE_MODE_SUM).setMinScore(MIN_SCORE);
// 分页参数
Pageable pageable = new PageRequest(pageNumber, pageSize);
return new NativeSearchQueryBuilder()
.withPageable(pageable)
.withQuery(functionScoreQueryBuilder).build();
}
示例7: searchCity
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
@Override
public List<City> searchCity(Integer pageNumber,
Integer pageSize,
String searchContent) {
// 分页参数
Pageable pageable = new PageRequest(pageNumber, pageSize);
// Function Score Query
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
.add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("cityname", searchContent)),
ScoreFunctionBuilders.weightFactorFunction(1000))
.add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("description", searchContent)),
ScoreFunctionBuilders.weightFactorFunction(100));
// 创建搜索 DSL 查询
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withPageable(pageable)
.withQuery(functionScoreQueryBuilder).build();
LOGGER.info("\n searchCity(): searchContent [" + searchContent + "] \n DSL = \n " + searchQuery.getQuery().toString());
Page<City> searchPageResults = cityRepository.search(searchQuery);
return searchPageResults.getContent();
}
示例8: setUp
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
super.setUp();
store.clear();
StoredFeature feature1 = new StoredFeature("match1", Collections.singletonList("query_string"),
"mustache",
new MatchQueryBuilder("field1", "{{query_string}}").toString());
StoredFeature feature2 = new StoredFeature("match2", Collections.singletonList("query_string"),
"mustache",
new MatchQueryBuilder("field2", "{{query_string}}").toString());
StoredFeature feature3 = new StoredFeature("score3", Collections.emptyList(),
"mustache",
new FunctionScoreQueryBuilder(new FieldValueFactorFunctionBuilder("scorefield2")
.factor(1.2F)
.modifier(FieldValueFactorFunction.Modifier.LN2P)
.missing(0F)).toString());
StoredFeatureSet set = new StoredFeatureSet("set1", Arrays.asList(feature1, feature2, feature3));
store.add(set);
LtrRanker ranker = new LinearRanker(new float[]{0.1F, 0.2F, 0.3F});
CompiledLtrModel model = new CompiledLtrModel("model1", set, ranker);
store.add(model);
}
示例9: prepareModels
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
public void prepareModels() throws Exception {
List<StoredFeature> features = new ArrayList<>(3);
features.add(new StoredFeature("text_feature1", Collections.singletonList("query"), "mustache",
QueryBuilders.matchQuery("field1", "{{query}}").toString()));
features.add(new StoredFeature("text_feature2", Collections.singletonList("query"), "mustache",
QueryBuilders.matchQuery("field2", "{{query}}").toString()));
features.add(new StoredFeature("numeric_feature1", Collections.singletonList("query"), "mustache",
new FunctionScoreQueryBuilder(QueryBuilders.matchAllQuery(), new FieldValueFactorFunctionBuilder("scorefield1")
.factor(FACTOR)
.modifier(FieldValueFactorFunction.Modifier.LN2P)
.missing(0F)).scoreMode(FiltersFunctionScoreQuery.ScoreMode.MULTIPLY).toString()));
features.add(new StoredFeature("derived_feature", Collections.singletonList("query"), "derived_expression",
"100"));
StoredFeatureSet set = new StoredFeatureSet("my_set", features);
addElement(set);
StoredLtrModel model = new StoredLtrModel("my_model", set,
new StoredLtrModel.LtrModelDefinition("model/linear",
LinearRankerParserTests.generateRandomModelString(set), true));
addElement(model);
}
示例10: testFunctionScore
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
public void testFunctionScore() {
FilterFunctionBuilder[] functions = {
new FunctionScoreQueryBuilder.FilterFunctionBuilder(
matchQuery("name", "kimchy"),
randomFunction("ABCDEF")),
new FunctionScoreQueryBuilder.FilterFunctionBuilder(
exponentialDecayFunction("age", 0L, 1L))
};
functionScoreQuery(functions);
}
示例11: testInlineLeafInnerHitsNestedQueryViaFunctionScoreQuery
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
public void testInlineLeafInnerHitsNestedQueryViaFunctionScoreQuery() {
InnerHitBuilder leafInnerHits = randomInnerHits();
NestedQueryBuilder nestedQueryBuilder = new NestedQueryBuilder("path", new MatchAllQueryBuilder(), ScoreMode.None)
.innerHit(leafInnerHits, false);
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(nestedQueryBuilder);
Map<String, InnerHitBuilder> innerHitBuilders = new HashMap<>();
((AbstractQueryBuilder<?>) functionScoreQueryBuilder).extractInnerHitBuilders(innerHitBuilders);
assertThat(innerHitBuilders.get(leafInnerHits.getName()), notNullValue());
}
示例12: test
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
@Test
public void test() throws Exception {
FunctionScoreQueryBuilder.FilterFunctionBuilder [] filterFunctionBuilders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[]
{
new FunctionScoreQueryBuilder.FilterFunctionBuilder(ScoreFunctionBuilders.scriptFunction("doc['pubTime'].value*0.00000000001")),
new FunctionScoreQueryBuilder.FilterFunctionBuilder(ScoreFunctionBuilders.scriptFunction("doc['opinionValue'].value*0.1")),
};
QueryBuilders.functionScoreQuery(
QueryBuilders.queryStringQuery("北京").defaultField("FIELD").field("titleZh^3.0"),
filterFunctionBuilders
).boostMode(CombineFunction.MULTIPLY).scoreMode(FiltersFunctionScoreQuery.ScoreMode.AVG);
}
示例13: testForClient
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
@Test
public void testForClient() throws Exception {
FunctionScoreQueryBuilder.FilterFunctionBuilder[] functions = {
new FunctionScoreQueryBuilder.FilterFunctionBuilder(
QueryBuilders.matchQuery("name", "kimchy"),
ScoreFunctionBuilders. randomFunction("ABCDEF")),
new FunctionScoreQueryBuilder.FilterFunctionBuilder(
ScoreFunctionBuilders.exponentialDecayFunction("age", 0L, 1L))
};
QueryBuilder qb = QueryBuilders.functionScoreQuery(functions);
client.prepareSearch().setQuery(qb).execute().actionGet();
}
示例14: getClosestId
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
@Nullable
String getClosestId(int limit) {
@SuppressWarnings("unchecked")
Map<String, Object> primaryLocation = (Map<String, Object>) featureStructure.get("primaryLocation");
BoolQueryBuilder boolQueryBuilder = target.queryInDocument(documentId);
if (primaryLocation == null || !primaryLocation.containsKey("begin") || !primaryLocation.containsKey("end")) {
return null;
}
SearchResponse searchResponse;
if (limit != 0) {
FunctionScoreQueryBuilder query = new FunctionScoreQueryBuilder(boolQueryBuilder)
.add(ScoreFunctionBuilders.scriptFunction(new Script("annotationDistance", ScriptService.ScriptType.INLINE, "native", primaryLocation)));
float minScore = 1.0f / (limit + 1.0f);
searchResponse = client.prepareSearch(target.getSystemIndex())
.setQuery(query)
.setMinScore(minScore)
.execute().actionGet();
} else {
searchResponse = client.prepareSearch(target.getSystemIndex())
.setQuery(boolQueryBuilder.must(QueryBuilders.termQuery("primaryLocation.begin", primaryLocation.get("begin")))
.must(QueryBuilders.termQuery("primaryLocation.end", primaryLocation.get("end"))))
.get();
}
SearchHit[] hits = searchResponse.getHits().hits();
if (hits.length > 0) {
return hits[0].getId();
}
return null;
}
示例15: makeQueryBuilder
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; //导入依赖的package包/类
/**
* 构建查询
*
* @return
*/
public QueryBuilder makeQueryBuilder(SearchRequestBuilder srb) {
// 拼接查询层次
BoolQueryBuilder boolQ = QueryBuilders.boolQuery();
Map<SearchCondition, ConditionType> searchTypeMap = this.request.getSearchConditions();
ConditionType conditionType;
BoolQueryBuilder subBoolQ;
for (SearchCondition searchCondition : searchTypeMap.keySet()) {
conditionType = searchTypeMap.get(searchCondition);
subBoolQ = queryBuilder(searchCondition, conditionType);
mergeBuilder(boolQ, subBoolQ, conditionType);
}
// 没有条件直接返回
if (!boolQ.hasClauses()) {
return null;
}
// 得分脚本
if (request.getSafeScoreScript().size() < 1) {
srb.setQuery(boolQ);
return boolQ;
}
String boost_mode = request.getSafeExtend().get(Constants.BOOST_MODE_KEY);
if (StringUtils.isBlank(boost_mode)) {
boost_mode = Constants.boost_mode;
}
FunctionScoreQueryBuilder functionScoreQ = QueryBuilders.functionScoreQuery(boolQ);
functionScoreQ.boostMode(boost_mode);
for (ScoreScript scoreScript : request.getSafeScoreScript()) {
ScriptScoreFunctionBuilder builder = ScoreFunctionBuilders.scriptFunction(scoreScript
.getScript());
if (scoreScript.getScriptParams() != null && scoreScript.getScriptParams().size() > 0) {
builder.params(scoreScript.getScriptParams());
}
functionScoreQ.add(builder);
}
srb.setQuery(functionScoreQ);
return functionScoreQ;
}