Module: FieldQuery

Included in:
SearchQuery
Defined in:
app/queries/field_query.rb

Instance Method Summary collapse

Instance Method Details

#build_search_queryObject

Builds the query based on input to search fields



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/queries/field_query.rb', line 3

def build_search_query
  # If searching by date
  if get_field_type == "Date"
    queryhash = {range: { @fieldnames[0] => {gte: @input[:start_date], lte: @input[:end_date]}}}
    
  # If searching by any other field
  elsif @fieldnames[0] != nil
    # Exclude fields that shouldn't be searchable from query
    fields_to_search = @fieldnames[0] == "_all" ? @all_searchable_fields : [@fieldnames[0]]
    
    queryhash = {
      simple_query_string: {
        query: @input[:searchterm],
        fields: fields_to_search,
        default_operator: "AND",
        flags: "AND|OR|PHRASE|PREFIX|NOT|FUZZY|SLOP|NEAR"
      }}
  else
    queryhash = {}
  end

  return queryhash
end

#combine_search_and_facet_queries(queryhash, filterhash) ⇒ Object

Combine search and facet queries based on if it is search and facets, just search, just facets



41
42
43
44
45
46
47
48
49
50
# File 'app/queries/field_query.rb', line 41

def combine_search_and_facet_queries(queryhash, filterhash)
  if !@filter_by.empty? && !queryhash.empty?
    fullhash = { filtered: { query: queryhash, filter: filterhash }}
  elsif queryhash.empty?
    fullhash = { filtered: { filter: filterhash}}
  else
    fullhash = queryhash
  end
  return fullhash
end

#get_field_typeObject

Gets the type of a field



28
29
30
31
32
33
34
35
36
37
38
# File 'app/queries/field_query.rb', line 28

def get_field_type
  field_type = ""

  # Go through only the appropriate fields for query
  use_all_or_some("field_info", @all_field_info).each do |f|
    if @input[:field].to_s == f["Field Name"]
      field_type = f["Type"]
    end
  end
  return field_type
end