# Elasticsearch-php and function\_score

**URL:** <https://discuss.elastic.co/t/elasticsearch-php-and-function-score/17377>\
**Category:** Elasticsearch\
**Created:** [May 6, 2014, 3:51pm UTC](https://discuss.elastic.co/t/elasticsearch-php-and-function-score/17377 "2014-05-06T15:51:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris\_8](https://avatars.discourse-cdn.com/v4/letter/c/fbc32d/32.png) [@chris\_8](https://discuss.elastic.co/u/chris_8)\
**Post date:** [May 6, 2014, 3:51pm UTC](https://discuss.elastic.co/t/elasticsearch-php-and-function-score/17377/1 "2014-05-06T15:51:13Z")

</div>

hi !

the query below is working but when i use the php official client but i  
still have the exception : _malformed query, expected a START\_OBJECT_

"query": {  
"function\_score": {  
"query": {  
"terms": {  
"categoryName": [  
"toto",  
"tutu"  
],  
"minimum\_match": 1  
}  
},  
"functions": [  
{  
"filter": {  
"terms": {  
"tags": [  
"truc",  
"bidule"  
]  
}  
},  
"boost\_factor": 2  
}  
]  
}  
}

in my script the query is divided  
both $category and $boost are arrays of values

// function\_score --\> init main query  
$functionScoreQuery = array( 'query' =\> array(  
'terms' =\> array(  
'categoryName' =\> $category  
)  
));

// function\_score functions --\> init boost  
$functionScoreFunctions = array( 'functions' =\> array(  
'filter' =\> array(  
'terms' =\> array(  
'tags' =\> $boost  
)  
),  
'boost\_factor' =\> 2  
));

$defaultSubQuery['function\_score'] = $functionScoreQuery;  
if (($boost)) {  
$defaultSubQuery['function\_score'] +=  
$functionScoreFunctions;  
}

a print\_r() of the request show :

Array  
(  
[index] =\> myIndex  
[body] =\> Array  
(  
[query] =\> Array  
(  
[function\_score] =\> Array  
(  
[query] =\> Array  
(  
[terms] =\> Array  
(  
[categoryName] =\> Array  
(  
[0] =\> toto  
[1] =\> tutu  
)

```
                                    )

                            )

                        [functions] => Array
                            (
                                [filter] => Array
                                    (
                                        [terms] => Array
                                            (
                                                [tags] => Array
                                                    (
                                                        [0] => truc
                                                        [1] => bidule
                                                    )

                                            )

                                    )

                                [boost_factor] => 2
                            )

                    )

            )

[from] => 0
[size] => 10

```

)

--\> function\_score: malformed query, expected a START\_OBJECT while parsing  
functions but got a FIELD\_NAME];

I know the _function\_score_ as a special syntax combining arrays and  
objects  
([http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/\_search\_operations.html](http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/_search_operations.html)  
)

but i didn' found the correct syntax using the official php client  
...examples are quite light in this case so any help will be appreciated 🙂

regards,  
chris

--  
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/68c9c8b4-96f4-46a0-bf45-6f29693991a1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/68c9c8b4-96f4-46a0-bf45-6f29693991a1%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Stef](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@Stef](https://discuss.elastic.co/u/Stef)\
**Post date:** [May 7, 2014, 1:06pm UTC](https://discuss.elastic.co/t/elasticsearch-php-and-function-score/17377/2 "2014-05-07T13:06:30Z")

</div>

Hello,

Anyone has an idea ? i've got the same problem ...

Le mardi 6 mai 2014 17:51:13 UTC+2, chris a écrit :

> hi !
> 
> the query below is working but when i use the php official client but i  
> still have the exception : _malformed query, expected a START\_OBJECT_
> 
> "query": {  
> "function\_score": {  
> "query": {  
> "terms": {  
> "categoryName": [  
> "toto",  
> "tutu"  
> ],  
> "minimum\_match": 1  
> }  
> },  
> "functions": [  
> {  
> "filter": {  
> "terms": {  
> "tags": [  
> "truc",  
> "bidule"  
> ]  
> }  
> },  
> "boost\_factor": 2  
> }  
> ]  
> }  
> }
> 
> in my script the query is divided  
> both $category and $boost are arrays of values
> 
> // function\_score --\> init main query  
> $functionScoreQuery = array( 'query' =\> array(  
> 'terms' =\> array(  
> 'categoryName' =\> $category  
> )  
> ));
> 
> // function\_score functions --\> init boost  
> $functionScoreFunctions = array( 'functions' =\> array(  
> 'filter' =\> array(  
> 'terms' =\> array(  
> 'tags' =\> $boost  
> )  
> ),  
> 'boost\_factor' =\> 2  
> ));
> 
> $defaultSubQuery['function\_score'] = $functionScoreQuery;  
> if (($boost)) {  
> $defaultSubQuery['function\_score'] +=  
> $functionScoreFunctions;  
> }
> 
> a print\_r() of the request show :
> 
> Array  
> (  
> [index] =\> myIndex  
> [body] =\> Array  
> (  
> [query] =\> Array  
> (  
> [function\_score] =\> Array  
> (  
> [query] =\> Array  
> (  
> [terms] =\> Array  
> (  
> [categoryName] =\> Array  
> (  
> [0] =\> toto  
> [1] =\> tutu  
> )
> 
> ```
> )
> 
> )
> 
> [functions] => Array
> (
> [filter] => Array
> (
> [terms] => Array
> (
> [tags] => Array
> (
> [0] => truc
> [1] => bidule
> )
> 
> )
> 
> )
> 
> [boost_factor] => 2
> )
> 
> )
> 
> )
> 
> [from] => 0
> [size] => 10
> 
> ```
> 
> )
> 
> --\> function\_score: malformed query, expected a START\_OBJECT while  
> parsing functions but got a FIELD\_NAME];
> 
> I know the _function\_score_ as a special syntax combining arrays and  
> objects (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/_search_operations.html)  
> )
> 
> but i didn' found the correct syntax using the official php client  
> ...examples are quite light in this case so any help will be appreciated 🙂
> 
> regards,  
> chris

--  
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/472f7408-b518-47c7-8e92-c3262584347c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/472f7408-b518-47c7-8e92-c3262584347c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![chris\_8](https://avatars.discourse-cdn.com/v4/letter/c/fbc32d/32.png) [@chris\_8](https://discuss.elastic.co/u/chris_8)\
**Post date:** [May 15, 2014, 3:34pm UTC](https://discuss.elastic.co/t/elasticsearch-php-and-function-score/17377/3 "2014-05-15T15:34:11Z")

</div>

i've found my problem...

an intermediate array was missing (under [functions] see below) in the  
query construction with elasticsearch-php :

[function\_score] =\>

Array  
(

```
                    [query] => Array
                        (
                            [terms] => Array
                                (
                                    [category] => Array
                                        (
                                            [0] => toto
                                            [1] => tutu
                                        )

                                )

                        )

                    [functions] => Array
                        (
                            [0] => Array
                                (
                                    [filter] => Array
                                        (
                                            [terms] => Array
                                                (
                                                    [tags] => Array
                                                        (
                                                            [0] => truc
                                                        )

                                                )

                                        )

                                    [boost_factor] => 2
                                )

                        )

                )

```

Le mardi 6 mai 2014 17:51:13 UTC+2, chris a écrit :

> hi !
> 
> the query below is working but when i use the php official client but i  
> still have the exception : _malformed query, expected a START\_OBJECT_
> 
> "query": {  
> "function\_score": {  
> "query": {  
> "terms": {  
> "categoryName": [  
> "toto",  
> "tutu"  
> ],  
> "minimum\_match": 1  
> }  
> },  
> "functions": [  
> {  
> "filter": {  
> "terms": {  
> "tags": [  
> "truc",  
> "bidule"  
> ]  
> }  
> },  
> "boost\_factor": 2  
> }  
> ]  
> }  
> }
> 
> in my script the query is divided  
> both $category and $boost are arrays of values
> 
> // function\_score --\> init main query  
> $functionScoreQuery = array( 'query' =\> array(  
> 'terms' =\> array(  
> 'categoryName' =\> $category  
> )  
> ));
> 
> // function\_score functions --\> init boost  
> $functionScoreFunctions = array( 'functions' =\> array(  
> 'filter' =\> array(  
> 'terms' =\> array(  
> 'tags' =\> $boost  
> )  
> ),  
> 'boost\_factor' =\> 2  
> ));
> 
> $defaultSubQuery['function\_score'] = $functionScoreQuery;  
> if (($boost)) {  
> $defaultSubQuery['function\_score'] +=  
> $functionScoreFunctions;  
> }
> 
> a print\_r() of the request show :
> 
> Array  
> (  
> [index] =\> myIndex  
> [body] =\> Array  
> (  
> [query] =\> Array  
> (  
> [function\_score] =\> Array  
> (  
> [query] =\> Array  
> (  
> [terms] =\> Array  
> (  
> [categoryName] =\> Array  
> (  
> [0] =\> toto  
> [1] =\> tutu  
> )
> 
> ```
> )
> 
> )
> 
> [functions] => Array
> (
> [filter] => Array
> (
> [terms] => Array
> (
> [tags] => Array
> (
> [0] => truc
> [1] => bidule
> )
> 
> )
> 
> )
> 
> [boost_factor] => 2
> )
> 
> )
> 
> )
> 
> [from] => 0
> [size] => 10
> 
> ```
> 
> )
> 
> --\> function\_score: malformed query, expected a START\_OBJECT while  
> parsing functions but got a FIELD\_NAME];
> 
> I know the _function\_score_ as a special syntax combining arrays and  
> objects (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/_search_operations.html)  
> )
> 
> but i didn' found the correct syntax using the official php client  
> ...examples are quite light in this case so any help will be appreciated 🙂
> 
> regards,  
> chris

--  
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/ec9cd160-e919-412a-908a-10ae783babd0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ec9cd160-e919-412a-908a-10ae783babd0%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:29am UTC](https://discuss.elastic.co/t/elasticsearch-php-and-function-score/17377/4 "2017-07-06T01:29:07Z")

</div>


