18252

ElasticSearch and searching on multiple fields in PHP

Question:

I am using the latest version of <a href="https://github.com/elasticsearch/elasticsearch-php" rel="nofollow">elasticsearch-php</a> 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 <em>$countries</em> and/or <em>$category</em> can only have one element.

Answer1:

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.

Recommend

  • Error when I try to parse my json
  • JAR file: Could not find main class
  • Diagonal of polygon is inside or outside?
  • Generic Types Return in Typescript
  • StreamWriter won't flush to NetworkStream
  • Deploy same Javascript webapp build to different environments
  • Setting color in a row of a Jtable
  • How to solve “The hierarchy of the type is inconsistent” error in Java Eclipse?
  • Repainting JPanel after button click
  • Recycler View Inside Recycler View not Scrolling
  • Apple Mach - O Linker Error.No such file or directory Swifty Json
  • Can't zip RDDs with unequal numbers of partitions
  • Dispose not working as expected in DirectorySearcher
  • How to add new index numbers to the upsampled data while preserving the orginal indices one
  • Scrolling News Ticker Jquery - Issues
  • Is it possible to run an application built on sql server 2008 to run with 2005
  • Use neo4j server instead of embedded mode
  • Automapper missing type map configuration or unsupported mapping
  • Boost binary serialization doesn't work occasionally. The parsed data is corrupted sometimes
  • Negated scanset in fscanf and EOF
  • Using extern @class in order to add a category?
  • integration of function in modelica
  • “A GKScore must specify a leaderboard.”
  • Are there any libraries for Python to simulate keyboard action?
  • Get current user from inside the model in Sails
  • Telegram bot API - Inline bot getting Error 400 while trying to answer inline query
  • Create function that can pass a parameter without making a new component
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • How can we prepend rows to a react native list-view?
  • How can I tell a form not to dispose a particular control when it closes?
  • Hash Code in SQL Server?
  • Pass Dictionary to Javascript array
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • Read text file that is not in the main package in a runnable jar
  • ORA-29908: missing primary invocation for ancillary operator
  • Rearranging Cells in UITableView Bug & Saving Changes
  • KeystoneJS: Relationships in Admin UI not updating
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check