elasticsearch-php working with JSON arrays and objects

Source: Internet
Author: User
Tags vars

Working with JSON arrays and objects in PHP

The client has some confusing resources around JSON arrays and objects, and how to specify them in PHP. In particular, the problem is caused by empty objects and empty arrays. This article tells you some common patterns in the Elasticsearch JSON API and how to convert them into PHP representations.

Empty Object

The Elasticsearch API uses empty JSON objects in many places, which can cause problems in PHP. Unlike other languages, PHP does not provide a "short" indication of empty objects, so many developers do not know how to make an empty object.

Consider adding highlighting to your query:

  1. {
  2. "Query": {
  3. "Match": {
  4. " content": "quick brown fox"
  5. }
  6. },
  7. "Highlight": {
  8. "Fields": {
  9. "Content": {}//This empty object is the place that caused the problem
  10. }
  11. }
  12. }


The problem is that PHP automatically transforms "content" : {} into "content" : [], which is no longer valid for Elasticsearch DSLs. We need to tell PHP that an empty object is an object that is explicitly tried, not an array. If you want to define a query in PHP, you need to do this:

  1. $params [' body '] = Array (
  2. ' query ' = = Array (
  3. ' match ' = = Array (
  4. ' content ' = ' quick brown fox '
  5. )
  6. ),
  7. ' Highlight ' = Array (
  8. ' fields ' = = Array (
  9. ' content ' = (object) Array ()//We throw an empty array onto an object to represent an empty object, and JSON is now able to encode the correct
  10. )
  11. )
  12. );
  13. $results = $client->search ($params);

By using an empty array to convert to an object, we can force the Json_encode parser to properly output an empty object instead of an empty array. Sadly, this lengthy solution is the only one in PHP that can achieve the goal. Because PHP does not provide a "short" indication for empty objects.

Array Object

Another common pattern in the Elasticsearch DSL is an array object, for example, consider adding a sort to the query:

  1. {
  2. "Query": {
  3. "Match": { "content": "Quick brown Fox"}
  4. },
  5. "Sort": [ //"Sort" contains a JSON array object
  6. {"Time": {"order": "desc"}},
  7. {"popularity": {"order": "desc"}}
  8. ]
  9. }

This arrangement is very common, but in PHP it can be tricky to think that she needs a nested array, PHP's lengthy and difficult solution continues, in order to construct an array object, you do need an array of arrays:

  1. $params [' body '] = Array (
  2. ' query ' = = Array (
  3. ' match ' = = Array (
  4. ' content ' = ' quick brown fox '
  5. )
  6. ),
  7. ' sort ' = = array ( //This array is encoded as "sort": []
  8. Array (' time ' = = array (' order ' = ' desc ') '), //This array is encoded as {"time": {"order": "Desc"}}
  9. Array ('popularity ' = = Array (' order ' = ' desc ')) //This array is encoded as {"popularity": {"order": "desc "}}  
  10. )
  11. );
  12. $results = $client->search ($params);

If you are using the php5.4+ version, I strongly encourage you to use the short array syntax, which makes these nested arrays easier to read:

  1. $params [' body '] = [
  2. ' query ' = [
  3. ' Match ' = [
  4. ' content ' = ' quick brown fox '
  5. ]
  6. ],
  7. ' sort ' = [
  8. [' time' = [' order ' = ' desc ']],
  9. ['popularity ' = [' order ' = ' desc ']]
  10. ]
  11. ];
  12. $results = $client->search ($params);

Empty Array Object


Occasionally, you will encounter the need for the previous two modes, the integral query function is a good example, sometimes requires an array object, these objects may be empty JSON objects.

This query is given below:

  1. {
  2. "Query": {
  3. "Function_score": {
  4. "Functions": [
  5. {
  6. "Random_score": {}
  7. }
  8. ],
  9. "Boost_mode":"replace"
  10. }
  11. }
  12. }

We can use the following PHP code to generate:

    1. $params [' body '] = Array (
    2. ' query ' = = Array (
    3. ' function_score ' = Array (
    4. ' functions ' = = Array ( //This code: "Functions": []
    5. Array ( //This is encoded into an object in an array: {"Random_score": {}}
    6. ' random_score ' = (object) Array () //This is encoded into an empty JSON object: "Random_score": {}
    7. )
    8. )
    9. )
    10. )
    11. );
    12. $results = $client->search ($params);

elasticsearch-php working with JSON arrays and objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.