PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 25, 2022

[FIXED] How to use Elasticsearch FunctionScore query using Java API

 October 25, 2022     elasticsearch, java     No comments   

Issue

I need to implement the below function_score query using Java APIs. I couldn't find any official documentation for function_score query in the Java API section of elasticsearch

{
  "size": 40,
  "query": {
    "function_score": {
      "functions": [
        {
          "gauss": {
            "location": {
              "origin": {
                "lat": 39.99606,
                "lon": 116.480484
              },
              "offset": "2km",
              "scale": "3km"
            }
          }
        }
      ],
      "score_mode": "sum",
      "boost_mode": "replace",
      "query": {
        "bool": {
          "must": [
            {
              "range": {
                "startTime": {
                  "from": "2016-04-12T12:00:00.000Z",
                  "to": "2016-04-12T16:00:00.000Z",
                  "include_lower": true,
                  "include_upper": true
                }
              }
            },
            {
              "range": {
                "endTime": {
                  "from": "2016-04-12T12:00:00.000Z",
                  "to": "2016-04-12T16:00:00.000Z",
                  "include_lower": true,
                  "include_upper": true
                }
              }
            }
          ]
        }
      }
    }
  }
}

I use the Java api like this

RangeQueryBuilder queryStartDate = QueryBuilders.rangeQuery("startTime").gte(param.getStartTime())
                .lte(param.getEndTime());

        RangeQueryBuilder queryEndDate = QueryBuilders.rangeQuery("endTime").gte(param.getStartTime())
                .lte(param.getEndTime());

// query
QueryBuilder querys = QueryBuilders.boolQuery().must(queryStartDate).must(queryEndDate);

// function
Map<String, Double> locationMap = new HashMap<String, Double>();
        locationMap.put("lat", param.getLat());
        locationMap.put("lon", param.getLng());
        ScoreFunctionBuilder sfb2 = ScoreFunctionBuilders.gaussDecayFunction("location", locationMap, "1km")
                .setOffset("2km");

//function_score
FunctionScoreQueryBuilder sa = new FunctionScoreQueryBuilder().boostMode("replace").add(sfb2);

//final search
SearchResponse response = client.prepareSearch().setIndices("film").setTypes("data").setQuery(sa)
                .setQuery(querys).setSize(40).execute().actionGet();

but it dose not work


Solution

You need to add your query in your FunctionScoreQueryBuilder as below :

    FunctionScoreQueryBuilder fqBuilder = QueryBuilders.functionScoreQuery(querys);
    fqBuilder.boostMode(CombineFunction.REPLACE);
    fqBuilder.scoreMode(ScoreMode.Sum.name().toLowerCase());
    fqBuilder.add(sfb2);
    SearchResponse response = client.prepareSearch().setIndices("film").setTypes("data").setQuery(fqBuilder).setSize(40).execute().actionGet();


Answered By - Rahul
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing