dimanche 24 juillet 2016

Elastic Search query_string includes terms not queried

I am new with Elastic Search, but have been sitting with this problem for a while now:

What I am trying to achieve is a filtered search with plenty of grouped checkboxes (that are dynamically extracted), such as a group for:

  • grade (8-12),
  • meta data tags with unique ID's (plenty of them),
  • syllabus (currently just two),
  • year, etc.

The code I currently have (not all fields included yet):

$query = Contribute::searchByQuery(["query"=> ["query_string"=> ["query"=> "(grade=>'12' OR '10') AND (syllabus=>'IEB') AND (meta_data=>'1')"]]], null, null, $paginate, $page);

The problem I am having, however, is that this would, for example, match a row with the following values:

  • id==1 (from meta_data=>1???), syllabus==IEB (fair enough), grade==11 (why?), meta_data==1 (fair enough)
  • id==17, syllabus==IEB (fair enough), grade==9 (why), meta_data==3 (why?)
  • and then all the correct rows seem to be included as well

The solution (which is not feasible, since meta data may expand to thousands of unique ID's) is adding NOT to all other existing terms:

$query = Contribute::searchByQuery(["query"=> ["query_string"=> ["query"=> "(grade=>'12' OR '10' NOT '8' NOT '9' NOT '11') AND (syllabus=>'IEB' NOT 'DOE') AND (meta_data=>'1' NOT '2' NOT '3' NOT '4' etc etc etc.)"]]], null, null, $paginate, $page);

So the question is, how do I exclude any terms, NOT specified, by default? Currently they just include themselves. In a simpler way than the second piece of code where I have to explicitly add what I don't want.

Btw, I build these queries from the user selection, so it is not as static as in these examples. Also, I find it pointless to do a database checkup to see what terms were not included, to be added to the query. Isn't the point of Elastic search to avoid doing db queries?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2aasR2d
via IFTTT

Aucun commentaire:

Enregistrer un commentaire