當前位置: 首頁>>代碼示例>>Java>>正文


Java FunctionScoreQueryBuilder類代碼示例

本文整理匯總了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();
}
 
開發者ID:nickleeguan,項目名稱:springboot-learning-example-master,代碼行數:25,代碼來源:CityESServiceImpl.java

示例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();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:17,代碼來源:ChildQuerySearchIT.java

示例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)));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:27,代碼來源:PercolatorFieldMapperTests.java

示例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();
}
 
開發者ID:aillamsun,項目名稱:spring-boot-elastcsearch-example,代碼行數:31,代碼來源:GoodsEsDocService.java

示例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;
    }
 
開發者ID:codelibs,項目名稱:fess-suggest,代碼行數:25,代碼來源:SuggestRequest.java

示例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();
}
 
開發者ID:JeffLi1993,項目名稱:springboot-learning-example,代碼行數:34,代碼來源:CityESServiceImpl.java

示例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();
}
 
開發者ID:jeikerxiao,項目名稱:SpringBootStudy,代碼行數:25,代碼來源:CityServiceImpl.java

示例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);
}
 
開發者ID:o19s,項目名稱:elasticsearch-learning-to-rank,代碼行數:23,代碼來源:StoredLtrQueryBuilderTests.java

示例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);
}
 
開發者ID:o19s,項目名稱:elasticsearch-learning-to-rank,代碼行數:22,代碼來源:LoggingIT.java

示例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);
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:11,代碼來源:QueryDSLDocumentationTests.java

示例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());
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:10,代碼來源:InnerHitBuilderTests.java

示例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);
}
 
開發者ID:felayman,項目名稱:elasticsearch-full,代碼行數:14,代碼來源:FunctionsDemo.java

示例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();
}
 
開發者ID:felayman,項目名稱:elasticsearch-full,代碼行數:13,代碼來源:FunctionScoreQueryDemo.java

示例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;
}
 
開發者ID:nlpie,項目名稱:nlptab,代碼行數:36,代碼來源:ClosestFsFinder.java

示例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;
}
 
開發者ID:hailin0,項目名稱:es-service-parent,代碼行數:44,代碼來源:EsQueryBuilder.java


注:本文中的org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。