Current Location: Home> Latest Articles> Build an Elasticsearch query field array

Build an Elasticsearch query field array

M66 2025-05-11

How to build an Elasticsearch query field array using PHP's array_column function?

In actual development, PHP provides a lot of convenience functions to process arrays, and the array_column function is one of them. array_column can help us extract a column in a two-dimensional array and return a new array containing the values ​​of that column. The array_column function is very useful when building an Elasticsearch query field array.

This article will introduce how to use array_column to build an Elasticsearch query field array, especially how to extract specific fields from the original data, so that we can further construct Elasticsearch query.

1. What is the array_column function?

array_column is a built-in function in PHP that extracts all values ​​of a specified column from a two-dimensional array and returns these values ​​as a new one-dimensional array. The basic syntax of this function is as follows:

 array_column(array $array, $column_key, $index_key = null): array
  • $array : The input multi-dimensional array.

  • $column_key : The key name of the column that needs to be extracted.

  • $index_key : Optional parameter that specifies the column used to index the result array (if not provided, the result array will automatically use numeric index).

2. Example: Building an Elasticsearch query field array

Suppose you have a two-dimensional array containing document data, each document contains multiple fields such as id , name , category , etc. You want to extract all category fields from it and build an array for use by Elasticsearch query.

2.1. Raw data

Assume the original data is as follows:

 $documents = [
    ['id' => 1, 'name' => 'Apple', 'category' => 'Fruit', 'url' => 'http://example.com/apple'],
    ['id' => 2, 'name' => 'Carrot', 'category' => 'Vegetable', 'url' => 'http://example.com/carrot'],
    ['id' => 3, 'name' => 'Banana', 'category' => 'Fruit', 'url' => 'http://example.com/banana'],
    ['id' => 4, 'name' => 'Spinach', 'category' => 'Vegetable', 'url' => 'http://example.com/spinach']
];

2.2. Extract category field using array_column

We can use the array_column function to extract all category fields from it and build an array for easy use in Elasticsearch query.

 $categories = array_column($documents, 'category');
print_r($categories);

Output result:

 Array
(
    [0] => Fruit
    [1] => Vegetable
    [2] => Fruit
    [3] => Vegetable
)

2.3. Build an Elasticsearch query field array

Suppose you need to build an Elasticsearch query based on the category field to find documents that belong to the Fruit category. You can do this:

 $searchTerm = 'Fruit';
$query = [
    'query' => [
        'terms' => [
            'category' => array_column(
                array_filter($documents, function($doc) use ($searchTerm) {
                    return $doc['category'] === $searchTerm;
                }),
                'id'
            )
        ]
    ]
];

// Print query content
print_r($query);

Suppose we want to query the document with the category field Fruit , array_filter will first filter out all documents with the category Fruit , then use array_column to extract the id field from it, and finally build an Elasticsearch query array.

The output query array may look like this:

 Array
(
    [query] => Array
        (
            [terms] => Array
                (
                    [category] => Array
                        (
                            [0] => 1
                            [1] => 3
                        )
                )
        )
)

3. Complete code example

 <?php

$documents = [
    ['id' => 1, 'name' => 'Apple', 'category' => 'Fruit', 'url' => 'http://example.com/apple'],
    ['id' => 2, 'name' => 'Carrot', 'category' => 'Vegetable', 'url' => 'http://example.com/carrot'],
    ['id' => 3, 'name' => 'Banana', 'category' => 'Fruit', 'url' => 'http://example.com/banana'],
    ['id' => 4, 'name' => 'Spinach', 'category' => 'Vegetable', 'url' => 'http://example.com/spinach']
];

// use array_column Extract all category Fields
$categories = array_column($documents, 'category');
print_r($categories);

// Build Elasticsearch 查询Fields数组
$searchTerm = 'Fruit';
$query = [
    'query' => [
        'terms' => [
            'category' => array_column(
                array_filter($documents, function($doc) use ($searchTerm) {
                    return $doc['category'] === $searchTerm;
                }),
                'id'
            )
        ]
    ]
];

// Print query content
print_r($query);

?>

4. Summary

The array_column function is very suitable for extracting data from a multidimensional array, especially when building Elasticsearch queries, which can efficiently extract the fields we need, helping us construct the correct query format.

If your data source contains a large amount of documents, using the array_column function can significantly improve the simplicity and readability of the code, and can also effectively avoid the hassle of manually traversing the array.

By combining other array operation functions (such as array_filter ) and array_column , we can flexibly build complex query field arrays for Elasticsearch to meet different query needs.

Hope this article helps you! If you have any questions or want to know more about PHP array operation skills, please feel free to ask questions!