ElasticSearch and searching on multiple fields in PHP

Marc Witteveen

I am using the latest version of elasticsearch-php as well as the latest version of MongoDB and ElasticSearch.

I need to do a search on multiple fields that can contain one or multiple values. Example:

country_code should be either NL, BE or DE AND category should contain be AA01, BB01, CC02 or ZZ11

I thought I would solve it as followed (PHP):

$countries = array(“NL”, “BE”, “DE”);
$category = array(“AA01”, “BB01”, “CC02”, “ZZ11”);

$searchParams['body']['query']['bool']['must']['terms']['country'] = $countries;
$searchParams['body']['query']['bool']['must']['terms']['categories'] = $category;
$searchParams['body']['query']['bool']['must']['terms']['minimum_should_match'] = 1;

But the result does not even come close the the data that I expect to get back.

Sometimes $countries and/or $category can only have one element.

Sammaye

It is becaue of how PHP arrays work, you are overwriting the terms query each time, instead try something along the lines of:

array(
    'body' => array('query' => 
    'bool' => array(
        'must' => array(
            array('terms' => array('country' => implode(' ', $countries))),
            array('terms' => array('category' => implode(' ', $category))),
        )
    )
))

minimum_should_match is useless with must clause of the query.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ElasticSearch and searching on multiple fields in PHP

From Dev

Elasticsearch not searching some fields

From Dev

awk searching for multiple fields

From Dev

Searching multiple types in elasticsearch

From Dev

Searching on Multiple Fields using BST

From Dev

Searching six fields multiple times

From Dev

searching on multiple values in php

From Dev

ElasticSearch group by multiple fields

From Dev

Elasticsearch match multiple fields

From Dev

Search on multiple fields with Elasticsearch

From Dev

Elasticsearch multiple fields autosuggestion

From Dev

Elasticsearch match multiple fields

From Dev

Elasticsearch multiple fields OR query

From Dev

live searching multiple database fields using linq

From Dev

Searching in multiple elasticsearch indexes using NEST

From Dev

Mysql, PHP, searching for multiple words

From Dev

ElasticSearch sort order for multiple fields

From Dev

Search on multiple fields with ElasticSearch with or, and operators

From Dev

ElasticSearch filtering on multiple fields (with aggregration)

From Dev

Elasticsearch sort based on multiple fields

From Dev

SQLite: More efficient query for searching multiple terms in multiple fields

From Dev

Elasticsearch - searching across multiple multiple types of a index and its different types

From Dev

PHP search with multiple fields

From Dev

PHP + PDO Pagination while Searching in multiple columns

From Dev

Searching multiple rows in MySQL database with PHP

From Dev

PHP - Searching for multiple rows with numbers/commas

From Dev

Need tip on searching through multiple fields [mysql optimization]

From Dev

Extbase Repository: Searching for term in multiple model fields with $query->like()

From Dev

Extbase Repository: Searching for term in multiple model fields with $query->like()

Related Related

  1. 1

    ElasticSearch and searching on multiple fields in PHP

  2. 2

    Elasticsearch not searching some fields

  3. 3

    awk searching for multiple fields

  4. 4

    Searching multiple types in elasticsearch

  5. 5

    Searching on Multiple Fields using BST

  6. 6

    Searching six fields multiple times

  7. 7

    searching on multiple values in php

  8. 8

    ElasticSearch group by multiple fields

  9. 9

    Elasticsearch match multiple fields

  10. 10

    Search on multiple fields with Elasticsearch

  11. 11

    Elasticsearch multiple fields autosuggestion

  12. 12

    Elasticsearch match multiple fields

  13. 13

    Elasticsearch multiple fields OR query

  14. 14

    live searching multiple database fields using linq

  15. 15

    Searching in multiple elasticsearch indexes using NEST

  16. 16

    Mysql, PHP, searching for multiple words

  17. 17

    ElasticSearch sort order for multiple fields

  18. 18

    Search on multiple fields with ElasticSearch with or, and operators

  19. 19

    ElasticSearch filtering on multiple fields (with aggregration)

  20. 20

    Elasticsearch sort based on multiple fields

  21. 21

    SQLite: More efficient query for searching multiple terms in multiple fields

  22. 22

    Elasticsearch - searching across multiple multiple types of a index and its different types

  23. 23

    PHP search with multiple fields

  24. 24

    PHP + PDO Pagination while Searching in multiple columns

  25. 25

    Searching multiple rows in MySQL database with PHP

  26. 26

    PHP - Searching for multiple rows with numbers/commas

  27. 27

    Need tip on searching through multiple fields [mysql optimization]

  28. 28

    Extbase Repository: Searching for term in multiple model fields with $query->like()

  29. 29

    Extbase Repository: Searching for term in multiple model fields with $query->like()

HotTag

Archive