# Function\_score and elasticsearch-php

**URL:** <https://discuss.elastic.co/t/function-score-and-elasticsearch-php/16329>\
**Category:** Elasticsearch\
**Created:** [March 12, 2014, 6:47pm UTC](https://discuss.elastic.co/t/function-score-and-elasticsearch-php/16329 "2014-03-12T18:47:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erdal\_Gunyar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erdal_gunyar/32/1702_2.png) [@Erdal\_Gunyar](https://discuss.elastic.co/u/Erdal_Gunyar)\
**Post date:** [March 12, 2014, 6:47pm UTC](https://discuss.elastic.co/t/function-score-and-elasticsearch-php/16329/1 "2014-03-12T18:47:00Z")

</div>

Hi everybody,

Does anyone here successfully implemented function\_score with  
elasticsearch-php?  
Of course, without passing all the body as a JSON string.

I've actually tried but it failed, it looks like it's impossible to pass  
the "array+object" located in the "functions" part :  
"query": {  
"function\_score": {  
"query": {  
"query\_string": {  
"query": "MyQuery",  
}  
},  
"functions": [{  
"script\_score": {  
"script": "doc['boostfield'].value"  
}  
}],  
"score\_mode": "multiply"  
}  
},

Any help will be appreciated! 🙂

Thanks,

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/f51a1488-ce22-4119-94ed-57ba13a5cdf2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f51a1488-ce22-4119-94ed-57ba13a5cdf2%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [March 12, 2014, 7:01pm UTC](https://discuss.elastic.co/t/function-score-and-elasticsearch-php/16329/2 "2014-03-12T19:01:04Z")

</div>

Hey there. It's possible, but it requires explicit object creation via the  
PHP \stdClass() object. PHP isn't very good at deciding between arrays and  
objects when using json\_encode, unless you use explicit objects.

Untested, but try this:

$scriptScore = new \stdClass();$scriptScore-\>script = "doc['boostfield'].value";  
$qry = array(  
'query' =\> array(  
'function\_score' =\> array(  
'functions' =\> array(  
array("script\_score" =\> $scriptScore)  
),  
'query' =\> array(  
'query\_string' =\> array('query' =\> 'MyQuery')  
),  
'score\_mode' =\> 'multiply'  
)  
));  
$searchParams['body'] = $qry;  
$client = new Elasticsearch\Client();$retDoc = $client-\>search($searchParams);print\_r($retDoc);

This particular PHP irritation just came up the other day as a ticket  
([function\_score not working? · Issue #47 · elastic/elasticsearch-php · GitHub](https://github.com/elasticsearch/elasticsearch-php/issues/47)), so you're  
not alone in being confused. I'm going to write some documentation about  
how to accomplish it, since the syntax is different from normal DSL queries.

-Zach

On Wednesday, March 12, 2014 2:47:00 PM UTC-4, Erdal Gunyar wrote:

> Hi everybody,
> 
> Does anyone here successfully implemented function\_score with  
> elasticsearch-php?  
> Of course, without passing all the body as a JSON string.
> 
> I've actually tried but it failed, it looks like it's impossible to pass  
> the "array+object" located in the "functions" part :  
> "query": {  
> "function\_score": {  
> "query": {  
> "query\_string": {  
> "query": "MyQuery",  
> }  
> },  
> "functions": [{  
> "script\_score": {  
> "script": "doc['boostfield'].value"  
> }  
> }],  
> "score\_mode": "multiply"  
> }  
> },
> 
> Any help will be appreciated! 🙂
> 
> Thanks,

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/479748b5-55b4-4c2d-a370-4d8740118b5b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/479748b5-55b4-4c2d-a370-4d8740118b5b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [March 12, 2014, 7:05pm UTC](https://discuss.elastic.co/t/function-score-and-elasticsearch-php/16329/3 "2014-03-12T19:05:11Z")

</div>

For the record, this array syntax should work as well:

$qry = array(  
'query' =\> array(  
'function\_score' =\> array(  
'functions' =\> array(  
array("script\_score" =\> array('script' =\> "doc['boostfield'].value"))  
),  
'query' =\> array(  
'query\_string' =\> array('query' =\> 'MyQuery')  
),  
'score\_mode' =\> 'multiply'  
)  
));

But the object notation tends to be safer because it can handle empty  
objects (for example, a random function without a seed is just  
`"random_score" : {}`, which will break the array notation. Objects make  
sure that doesn't happen.

On Wednesday, March 12, 2014 2:47:00 PM UTC-4, Erdal Gunyar wrote:

> Hi everybody,
> 
> Does anyone here successfully implemented function\_score with  
> elasticsearch-php?  
> Of course, without passing all the body as a JSON string.
> 
> I've actually tried but it failed, it looks like it's impossible to pass  
> the "array+object" located in the "functions" part :  
> "query": {  
> "function\_score": {  
> "query": {  
> "query\_string": {  
> "query": "MyQuery",  
> }  
> },  
> "functions": [{  
> "script\_score": {  
> "script": "doc['boostfield'].value"  
> }  
> }],  
> "score\_mode": "multiply"  
> }  
> },
> 
> Any help will be appreciated! 🙂
> 
> Thanks,

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/4af5db19-af3d-4a01-b428-6b8e3e90af75%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4af5db19-af3d-4a01-b428-6b8e3e90af75%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Erdal\_Gunyar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erdal_gunyar/32/1702_2.png) [@Erdal\_Gunyar](https://discuss.elastic.co/u/Erdal_Gunyar)\
**Post date:** [March 13, 2014, 1:37pm UTC](https://discuss.elastic.co/t/function-score-and-elasticsearch-php/16329/4 "2014-03-13T13:37:42Z")

</div>

Hello Zachary,

Thank you for your quick and working responses!  
I've previously tried with double array method and didn't worked, I should  
have missed something at that time.

And thanks also for the object method, didn't know 🙂

Have a good day all,

Erdal.

Le mercredi 12 mars 2014 20:05:11 UTC+1, Zachary Tong a écrit :

> For the record, this array syntax should work as well:
> 
> $qry = array(  
> 'query' =\> array(  
> 'function\_score' =\> array(  
> 'functions' =\> array(  
> array("script\_score" =\> array('script' =\> "doc['boostfield'].value"))  
> ),  
> 'query' =\> array(  
> 'query\_string' =\> array('query' =\> 'MyQuery')  
> ),  
> 'score\_mode' =\> 'multiply'  
> )  
> ));
> 
> But the object notation tends to be safer because it can handle empty  
> objects (for example, a random function without a seed is just  
> `"random_score" : {}`, which will break the array notation. Objects make  
> sure that doesn't happen.
> 
> On Wednesday, March 12, 2014 2:47:00 PM UTC-4, Erdal Gunyar wrote:
> 
> > Hi everybody,
> > 
> > Does anyone here successfully implemented function\_score with  
> > elasticsearch-php?  
> > Of course, without passing all the body as a JSON string.
> > 
> > I've actually tried but it failed, it looks like it's impossible to pass  
> > the "array+object" located in the "functions" part :  
> > "query": {  
> > "function\_score": {  
> > "query": {  
> > "query\_string": {  
> > "query": "MyQuery",  
> > }  
> > },  
> > "functions": [{  
> > "script\_score": {  
> > "script": "doc['boostfield'].value"  
> > }  
> > }],  
> > "score\_mode": "multiply"  
> > }  
> > },
> > 
> > Any help will be appreciated! 🙂
> > 
> > Thanks,

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7c556d4b-88b7-4a0b-bf73-3c4442ab707b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7c556d4b-88b7-4a0b-bf73-3c4442ab707b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [March 13, 2014, 1:42pm UTC](https://discuss.elastic.co/t/function-score-and-elasticsearch-php/16329/5 "2014-03-13T13:42:24Z")

</div>

No problem, glad to help! The syntax is definitely kinda gross, I'll try  
to write some docs on it soon to help others.

Let me know if you run into any more problems, and feel free to open a  
ticket at the Elasticsearch-PHP github repo, I keep a closer eye on tickets  
than the mailing list 🙂

-Z

On Thursday, March 13, 2014 9:37:42 AM UTC-4, Erdal Gunyar wrote:

> Hello Zachary,
> 
> Thank you for your quick and working responses!  
> I've previously tried with double array method and didn't worked, I should  
> have missed something at that time.
> 
> And thanks also for the object method, didn't know 🙂
> 
> Have a good day all,
> 
> Erdal.
> 
> Le mercredi 12 mars 2014 20:05:11 UTC+1, Zachary Tong a écrit :
> 
> > For the record, this array syntax should work as well:
> > 
> > $qry = array(  
> > 'query' =\> array(  
> > 'function\_score' =\> array(  
> > 'functions' =\> array(  
> > array("script\_score" =\> array('script' =\> "doc['boostfield'].value"))  
> > ),  
> > 'query' =\> array(  
> > 'query\_string' =\> array('query' =\> 'MyQuery')  
> > ),  
> > 'score\_mode' =\> 'multiply'  
> > )  
> > ));
> > 
> > But the object notation tends to be safer because it can handle empty  
> > objects (for example, a random function without a seed is just  
> > `"random_score" : {}`, which will break the array notation. Objects make  
> > sure that doesn't happen.
> > 
> > On Wednesday, March 12, 2014 2:47:00 PM UTC-4, Erdal Gunyar wrote:
> > 
> > > Hi everybody,
> > > 
> > > Does anyone here successfully implemented function\_score with  
> > > elasticsearch-php?  
> > > Of course, without passing all the body as a JSON string.
> > > 
> > > I've actually tried but it failed, it looks like it's impossible to pass  
> > > the "array+object" located in the "functions" part :  
> > > "query": {  
> > > "function\_score": {  
> > > "query": {  
> > > "query\_string": {  
> > > "query": "MyQuery",  
> > > }  
> > > },  
> > > "functions": [{  
> > > "script\_score": {  
> > > "script": "doc['boostfield'].value"  
> > > }  
> > > }],  
> > > "score\_mode": "multiply"  
> > > }  
> > > },
> > > 
> > > Any help will be appreciated! 🙂
> > > 
> > > Thanks,

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6d868de2-1c3e-4f4b-abfa-d8fc45e8cd67%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6d868de2-1c3e-4f4b-abfa-d8fc45e8cd67%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 1:43am UTC](https://discuss.elastic.co/t/function-score-and-elasticsearch-php/16329/6 "2017-07-06T01:43:29Z")

</div>


