Module: Auth::Search::Main

Defined in:
lib/auth/search/main.rb

Class Method Summary collapse

Class Method Details

.es_six_base_ngram_query(search_on_field, highlight_fields = nil) ⇒ Object

this def, returns a hash with the structure for the basic ngram query. the query_string is left blank, and you should merge this in through any def that wants to perform an ngram query. param search_on_field : the field on which we are going to do the n_Gram query. Most of the times this should default to _all_fields @return



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/auth/search/main.rb', line 8

def self.es_six_base_ngram_query(search_on_field,highlight_fields=nil)
	
	search_on_field ||= :tags
	highlight_fields ||= {
		fields: {
			model_fields: {}
		}
	}

	qc = {
		body: {
			query: {
				bool: {
					must: {
						match: {
							
						}
					},
					filter: {
						match_all:{

						}
					}
				}
			},
			highlight: highlight_fields
		}
	}

	qc[:body][:query][:bool][:must][:match][search_on_field] = {
			query: "",
			operator: "and"
	}	

	qc

end

.es_six_finalize_search_query_clause(args) ⇒ Object

searches all indices, for the search string. @param : This is expected to contain the following: this def will use the #base_ngram_query hash and merge in a filter for the resource_id. ‘Public’ Resources

if the public field is present, don't add any resource_id filter.
if the public field is not present, then add the resource_id filter if the resource_id is provided.

@return : returns a query clause(hash)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/auth/search/main.rb', line 57

def self.es_six_finalize_search_query_clause(args)

	search_on_field = args[:search_field] || :tags
	
	args = args.deep_symbolize_keys
	
	return [] unless args[:query_string]
	
	query = es_six_base_ngram_query(search_on_field)
	
	query[:size] = args[:size] || 5
	
	query[:body][:query][:bool][:must][:match][search_on_field][:query] = args[:query_string]


	if args[:resource_id]
		## if a resource id is provided, show all public records + those records which have this resource id as the owner.
		query[:body][:query][:bool][:filter] = {
				
					bool: {
						should: [
							{
								bool: {
									must: [
										{
											term: {
												public: "no"
											}
										},
										{
											term: {
												resource_id: args[:resource_id]
											}
										}
									]
								}
							},
							{
								term: {
									public: "yes"
								}
							}
						]
					}
				
			}
	else
		## if its not provided, and there is no admin in the args, then only show the public records.
		unless args[:resource_is_admin]
			query[:body][:query][:bool][:filter] = {
				bool: 
				{
					must: [
						{
							term: {
								public: "yes"
							}
						}
					]
				}
			} 
		end
	end

	query

end

.search(args) ⇒ Object

delegates the building of the query to finalize_search_query_clause. @args : must have a field called query_string @return response: an array of mongoid search result objects.



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/auth/search/main.rb', line 129

def self.search(args)	
	puts "args are:"
	puts args.to_s
	query = es_six_finalize_search_query_clause(args)
	puts "query is:"
	puts JSON.pretty_generate(query)
	res = Mongoid::Elasticsearch.search(query,{:wrapper => :load}).results
	puts "----------------- OUTPUTTING RESULTS ---------------"
	res.each do |res|
		puts res.to_s
	end
	res
end