Module: Auth::Search::Main

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

Class Method Summary collapse

Class Method Details

.base_ngram_queryObject

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. @return



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/auth/search/main.rb', line 7

def self.base_ngram_query
  {
    body: {
      query: {
        filtered: 
        {
          query: {
            match: {
              _all: {
                query: "",
                operator: "and"
              }
            }
          },
          filter: {
            match_all:{

            }
          }
        }
      }
    }
  }
end

.search(args) ⇒ Object

@return response: an array of mongoid search result objects.



44
45
46
47
48
49
50
51
52
53
54
55
56
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
# File 'lib/auth/search/main.rb', line 44

def self.search(args) 
  args = args.deep_symbolize_keys
  return [] unless args[:query_string]
  query = base_ngram_query
  
  ## set all the required values.
  query[:size] = args[:size] || 20
  
  query[:body][:query][:filtered][:query][:match][:_all][:query] = args[:query_string]
  if args[:resource_id]
    query[:body][:query][:filtered][:filter] = {
        
          bool: {
            should: [
              {
                bool: {
                  must: [
                    {
                      term: {
                        public: "no"
                      }
                    },
                    {
                      term: {
                        resource_id: args[:resource_id]
                      }
                    }
                  ]
                }
              },
              {
                term: {
                  public: "yes"
                }
              }
            ]
          }
        
      }
  else
    ## if a resource id is not provided then
    ## it means that the resource is admin,
    ## and they should be able to access and see
    ## any resource
    ## so in that case we leave the query
    ## as is.
  end

  #puts JSON.pretty_generate(query)
  Mongoid::Elasticsearch.search(query,{:wrapper => :load}).results
end