# Trouble building query from Java

**URL:** https://discuss.elastic.co/t/trouble-building-query-from-java/85810
**Category:** Elasticsearch
**Created:** [May 15, 2017, 7:18pm UTC](https://discuss.elastic.co/t/trouble-building-query-from-java/85810 "2017-05-15T19:18:17Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![taylorKonigsmark](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@taylorKonigsmark](https://discuss.elastic.co/u/taylorKonigsmark)
#### Post date: [May 15, 2017, 7:18pm UTC](https://discuss.elastic.co/t/trouble-building-query-from-java/85810/1 "2017-05-15T19:18:17Z")

</div>

Hello all,

I'm having trouble creating a function score query to match this query I made via HTTP:

curl -X POST   
'[http://localhost:9200/myIndex/\_search?explain=&offset=1000&limit=10](http://localhost:9200/myIndex/_search?explain=&offset=1000&limit=10)'   
-H 'cache-control: no-cache'   
-H 'content-type: application/json'   
-H 'postman-token: cc6fe50f-ef69-08b8-2e8b-c301b964a109'   
-d '{  
"from": 0,  
"size": 2000,  
"query": {  
"function\_score": {  
"boost\_mode": "replace",  
"score\_mode": "sum",  
"functions": [  
{  
"filter": {  
"match": {  
"propertyToMatch1": "valueToMatch1"  
}  
},  
"weight": 1  
},  
{  
"filter": {  
"match": {  
"propertyToMatch1": "valueToMatch2"  
}  
},  
"weight": 1  
}],  
"query" : {  
"bool" : {  
"must" : [ {  
"match" : {  
"propertyToNotMatch1" : {  
"query" : true,  
"type" : "boolean"  
}  
}  
}, {  
"match" : {  
"propertyToNotMatch2" : {  
"query" : true,  
"type" : "boolean"  
}  
}  
}, {  
"bool" : {  
"should" : {  
"match" : {  
"propertyToNotMatch3" : {  
"query" : "value3",  
"type" : "boolean"  
}  
}  
}  
}  
}, {  
"bool" : {  
"should" : [ {  
"match" : {  
"propertyToMatch1" : {  
"query" : "valueToMatch1",  
"type" : "boolean"  
}  
}  
}, {  
"match" : {  
"propertyToMatch1" : {  
"query" : "valueToMatch1",  
"type" : "boolean"  
}  
}  
}]  
}  
}, {  
"bool" : {  
"should" : {  
"match" : {  
"propertyToNotMatch4" : {  
"query" : "value4",  
"type" : "boolean"  
}  
}  
}  
}  
} ]  
}  
}  
}  
},  
"\_source": false,  
"sort": [{  
"\_score": {  
"order": "desc"  
}  
}],  
"track\_scores": true  
}'

The main trouble I'm having with here is adding the filter properties that is embedded in the functions array above. the ScoreFunctionBuilders doesn't seem to have what I'm looking for. It does have weightFactorFunction, but it doesn't seem to allow me to add a filter with it. If I could get some direction for adding a filter to the functions array, I can probably figure out the rest.

This is all I have so far for building the function score query:

```
    FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(buildSearchQuery(getSearchAttributes(request)));
    functionScoreQueryBuilder.scoreMode(FiltersFunctionScoreQuery.ScoreMode.Sum.name());
    functionScoreQueryBuilder.boostMode(CombineFunction.REPLACE.name());

    functionScoreQueryBuilder.add(ScoreFunctionBuilders.weightFactorFunction(1));

```

Thanks for taking the time for reading this! Any help is appreciated.

---

<div class="post-metadata">

### Author: ![taylorKonigsmark](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@taylorKonigsmark](https://discuss.elastic.co/u/taylorKonigsmark)
#### Post date: [May 15, 2017, 9:02pm UTC](https://discuss.elastic.co/t/trouble-building-query-from-java/85810/2 "2017-05-15T21:02:46Z")

</div>

I figured it out, for anyone having that question in the future. You can do something like this (note this is 2.4 code):

```
    FunctionScoreQueryBuilder functionScoreQueryBuilder = functionScoreQuery(buildSearchQuery(getSearchAttributes(request)));
    functionScoreQueryBuilder.scoreMode(FiltersFunctionScoreQuery.ScoreMode.Sum.name());
    functionScoreQueryBuilder.boostMode(CombineFunction.REPLACE.name());
    
    functionScoreQueryBuilder.add(matchQuery("propertyToMatch1", "valueToMatch1"), weightFactorFunction(3));
```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [June 12, 2017, 9:09pm UTC](https://discuss.elastic.co/t/trouble-building-query-from-java/85810/3 "2017-06-12T21:09:43Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
