See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Elastic Search query_string includes terms not queried

Course Queries Syllabus Queries

Max. 2000 characters



( 4 months 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?

what's your interest