Elastic Search query_string includes terms not queried

Course Queries Syllabus Queries 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

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?

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.