Elasticsearch array match against another - inverse array search - AND subset


(Rajeshetty) #1

I have 2 models

  • User profile + activities

     "user_profile_summary": {
     	"id": {"include_in_all": false,"index": "not_analyzed","type": "string"},
     	"first_name": {"include_in_all": false,"index": "not_analyzed","type": "string"},
     	"last_name": {"include_in_all": false,"index": "not_analyzed","type": "string"},
     	"location": {"include_in_all": false,"index": "not_analyzed","type": "string"},
     	 user_activities": {
     		"type": "nested",
     		"properties": {
     			"activity_id":{ "index": "not_analyzed", "type": "string"},
     			"activity_score":{ "index": "not_analyzed", "type": "float"}
     			}
     		}	
     
     }
    
  • Activities Search

      "activities_search": {
     		"id": {"include_in_all": false,"index": "no","type": "string"},		
     		"created_at": {"include_in_all": false,"index": "not_analyzed","type": "date"},
     		"name":{ "include_in_all": false,"index": "not_analyzed", "type": "string"},
     		"description":{ "include_in_all": false,"index": "not_analyzed", "type": "string"},
     		"required_activities":{"include_in_all": false,"index": "not_analyzed", "type": "string"},
     		 must_have_one_activities": {
     			"type": "nested",
     			"properties": {
     				"activity_ids":{ "index": "not_analyzed", "type": "string"}
     				}
     			}			
     	}
    

required_activities is an array

["activity-id1", "activity-id2"]

must_have_one_activities is an array of maps

	[
		{
			"activity_ids" : ["activity-id3", "activity-id4"]
		},
		{
			"activity_ids" : ["activity-id5", "activity-id6"]
		},
	]

There are 2 types of search .

  • Finding users that match activities based on activities_search model - thats simple as applying MUST terms . e.g find all the users where users have all required_activities and at least one from each of the must_have_one_activities.

I'm trying to figure out how to perform following search ?

  • Second search is other inverse where I need to find all activities that match given user's activities against all activities_search model. e.g. find all activities_searches where current user's activities match again required_activities (MUST have ALL the ones listed in required_activities) and must_have_one_activities (MUST have at least the ones listed in must_have_one_activities).

Its almost like finding user activities subset in required_activities array where user MUST have all activities required_activities array.

Any idea how to perform this inverse search ?,


(system) #2