Elasticsearch-php and function_score


(chris-8) #1

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
)

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 :slight_smile:

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/68c9c8b4-96f4-46a0-bf45-6f29693991a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Stef) #2

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 (
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 :slight_smile:

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/472f7408-b518-47c7-8e92-c3262584347c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(chris-8) #3

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 (
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 :slight_smile:

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/ec9cd160-e919-412a-908a-10ae783babd0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #4