Java Code Examples for org.elasticsearch.index.query.QueryBuilders#functionScoreQuery()

The following examples show how to use org.elasticsearch.index.query.QueryBuilders#functionScoreQuery() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticsearchFieldValueScoringStrategy.java    From vertexium with Apache License 2.0 6 votes vote down vote up
@Override
public QueryBuilder updateElasticsearchQuery(
    Graph graph,
    Elasticsearch7SearchIndex searchIndex,
    QueryBuilder query,
    QueryParameters queryParameters
) {
    List<String> fieldNames = getFieldNames(graph, searchIndex, queryParameters, getField());
    if (fieldNames == null) {
        return query;
    }

    HashMap<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("fieldNames", fieldNames);
    Script script = new Script(ScriptType.INLINE, "painless", scriptSrc, scriptParams);
    return QueryBuilders.functionScoreQuery(query, new ScriptScoreFunctionBuilder(script));
}
 
Example 2
Source File: ElasticsearchHammingDistanceScoringStrategy.java    From vertexium with Apache License 2.0 6 votes vote down vote up
@Override
public QueryBuilder updateElasticsearchQuery(
    Graph graph,
    Elasticsearch7SearchIndex searchIndex,
    QueryBuilder query,
    QueryParameters queryParameters
) {
    List<String> fieldNames = getFieldNames(graph, searchIndex, queryParameters, getField());
    if (fieldNames == null) {
        return query;
    }

    HashMap<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("hash", getHash());
    scriptParams.put("fieldNames", fieldNames);
    Script script = new Script(ScriptType.INLINE, "painless", scriptSrc, scriptParams);
    return QueryBuilders.functionScoreQuery(query, new ScriptScoreFunctionBuilder(script));
}
 
Example 3
Source File: ElasticsearchFieldValueScoringStrategy.java    From vertexium with Apache License 2.0 6 votes vote down vote up
@Override
public QueryBuilder updateElasticsearchQuery(
    Graph graph,
    Elasticsearch5SearchIndex searchIndex,
    QueryBuilder query,
    QueryParameters queryParameters
) {
    List<String> fieldNames = getFieldNames(graph, searchIndex, queryParameters, getField());
    if (fieldNames == null) {
        return query;
    }

    HashMap<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("fieldNames", fieldNames);
    Script script = new Script(ScriptType.INLINE, "painless", scriptSrc, scriptParams);
    return QueryBuilders.functionScoreQuery(query, new ScriptScoreFunctionBuilder(script));
}
 
Example 4
Source File: ElasticsearchHammingDistanceScoringStrategy.java    From vertexium with Apache License 2.0 6 votes vote down vote up
@Override
public QueryBuilder updateElasticsearchQuery(
    Graph graph,
    Elasticsearch5SearchIndex searchIndex,
    QueryBuilder query,
    QueryParameters queryParameters
) {
    List<String> fieldNames = getFieldNames(graph, searchIndex, queryParameters, getField());
    if (fieldNames == null) {
        return query;
    }

    HashMap<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("hash", getHash());
    scriptParams.put("fieldNames", fieldNames);
    Script script = new Script(ScriptType.INLINE, "painless", scriptSrc, scriptParams);
    return QueryBuilders.functionScoreQuery(query, new ScriptScoreFunctionBuilder(script));
}
 
Example 5
Source File: UserServiceImpl.java    From springBoot-study with Apache License 2.0 5 votes vote down vote up
@Override
public List<User> searchUserByWeight(String searchContent) {
       // 根据权重进行查询
       FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(QueryBuilders.matchQuery("name", searchContent));
       System.out.println("查询的语句:" + functionScoreQueryBuilder.toString());
       Iterable<User> searchResult = userDao.search(functionScoreQueryBuilder);
       List<User> list= Lists.newArrayList(searchResult);
       return list;
}
 
Example 6
Source File: FunctionScoreQueryDemo.java    From elasticsearch-full with Apache License 2.0 5 votes vote down vote up
@Test
public void testForClient() throws Exception {
    FunctionScoreQueryBuilder.FilterFunctionBuilder[] functions = {
            new FunctionScoreQueryBuilder.FilterFunctionBuilder(
                    QueryBuilders.matchQuery("name", "kimchy"),
                    ScoreFunctionBuilders. randomFunction()),
            new FunctionScoreQueryBuilder.FilterFunctionBuilder(
                    ScoreFunctionBuilders.exponentialDecayFunction("age", 0L, 1L))
    };
    QueryBuilder qb = QueryBuilders.functionScoreQuery(functions);
    client.prepareSearch().setQuery(qb).execute().actionGet();
}
 
Example 7
Source File: ElasticsearchIndex.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected Iterable<? extends DocumentDistance> geoQuery(final IRI geoProperty, Point p, final IRI units,
		double distance, String distanceVar, Var contextVar) throws MalformedQueryException, IOException {
	double unitDist;
	final DistanceUnit unit;
	if (GEOF.UOM_METRE.equals(units)) {
		unit = DistanceUnit.METERS;
		unitDist = distance;
	} else if (GEOF.UOM_DEGREE.equals(units)) {
		unit = DistanceUnit.KILOMETERS;
		unitDist = unit.getDistancePerDegree() * distance;
	} else if (GEOF.UOM_RADIAN.equals(units)) {
		unit = DistanceUnit.KILOMETERS;
		unitDist = DistanceUtils.radians2Dist(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM);
	} else if (GEOF.UOM_UNITY.equals(units)) {
		unit = DistanceUnit.KILOMETERS;
		unitDist = distance * Math.PI * DistanceUtils.EARTH_MEAN_RADIUS_KM;
	} else {
		throw new MalformedQueryException("Unsupported units: " + units);
	}

	double lat = p.getY();
	double lon = p.getX();
	final String fieldName = toGeoPointFieldName(SearchFields.getPropertyField(geoProperty));
	QueryBuilder qb = QueryBuilders.functionScoreQuery(
			QueryBuilders.geoDistanceQuery(fieldName).point(lat, lon).distance(unitDist, unit),
			ScoreFunctionBuilders.linearDecayFunction(fieldName, GeohashUtils.encodeLatLon(lat, lon),
					new DistanceUnit.Distance(unitDist, unit).toString()));
	if (contextVar != null) {
		qb = addContextTerm(qb, (Resource) contextVar.getValue());
	}

	SearchRequestBuilder request = client.prepareSearch();
	SearchHits hits = search(request, qb);
	final GeoPoint srcPoint = new GeoPoint(lat, lon);
	return Iterables.transform(hits, (Function<SearchHit, DocumentDistance>) hit -> {
		return new ElasticsearchDocumentDistance(hit, geoContextMapper, fieldName, units, srcPoint, unit);
	});
}
 
Example 8
Source File: EsQueryBuilder.java    From es-service-parent with Apache License 2.0 5 votes vote down vote up
/**
 * 构建查询
 * 
 * @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;
}
 
Example 9
Source File: QueryContext.java    From fess with Apache License 2.0 4 votes vote down vote up
public void addFunctionScore(final Consumer<List<FilterFunctionBuilder>> functionScoreQuery) {
    final List<FilterFunctionBuilder> list = new ArrayList<>();
    functionScoreQuery.accept(list);
    queryBuilder = QueryBuilders.functionScoreQuery(queryBuilder, list.toArray(new FilterFunctionBuilder[list.size()]));
}
 
Example 10
Source File: EsAbstractConditionQuery.java    From fess with Apache License 2.0 4 votes vote down vote up
protected FunctionScoreQueryBuilder regFunctionScoreQ(QueryBuilder queryBuilder, Collection<FilterFunctionBuilder> list) {
    FunctionScoreQueryBuilder functionScoreQuery =
            QueryBuilders.functionScoreQuery(queryBuilder, list.toArray(new FilterFunctionBuilder[list.size()]));
    regQ(functionScoreQuery);
    return functionScoreQuery;
}
 
Example 11
Source File: EsAbstractConditionQuery.java    From fess with Apache License 2.0 4 votes vote down vote up
protected FunctionScoreQueryBuilder regFunctionScoreQ(QueryBuilder queryBuilder, Collection<FilterFunctionBuilder> list) {
    FunctionScoreQueryBuilder functionScoreQuery =
            QueryBuilders.functionScoreQuery(queryBuilder, list.toArray(new FilterFunctionBuilder[list.size()]));
    regQ(functionScoreQuery);
    return functionScoreQuery;
}
 
Example 12
Source File: EsAbstractConditionQuery.java    From fess with Apache License 2.0 4 votes vote down vote up
protected FunctionScoreQueryBuilder regFunctionScoreQ(QueryBuilder queryBuilder, Collection<FilterFunctionBuilder> list) {
    FunctionScoreQueryBuilder functionScoreQuery =
            QueryBuilders.functionScoreQuery(queryBuilder, list.toArray(new FilterFunctionBuilder[list.size()]));
    regQ(functionScoreQuery);
    return functionScoreQuery;
}
 
Example 13
Source File: QueryHelperTest.java    From fess with Apache License 2.0 4 votes vote down vote up
private QueryBuilder functionScoreQuery(QueryBuilder queryBuilder) {
    return QueryBuilders.functionScoreQuery(queryBuilder, ScoreFunctionBuilders.fieldValueFactorFunction("boost"));
}