Class: ActiveDocument::MarkLogicSearchOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/ActiveDocument/mark_logic_search_options.rb

Overview

MarkLogicSearchOptions allow you to control exactly how the ActiveDocument::Finder#search method behaves and what additional information may be returned in the ActiveDocument::SearchResults object

Attributes

  • return_facets - if true then facet information is returned in the resultant ActiveDocument::SearchResults object. Default is true

  • value_constraints - Used for creating searches on the value of an element this is a #Hash of value constraint names to their options. e.g. search_options_object.value_constraints = => “wits.nctc.gov”, “element” => “Region”

  • word_constraints - Used for creating searches for a word within an element value. This is a #Hash of word constaint names to their options this is a #Hash of value constraint names to their options. e.g. search_options_object.word_constraints = => “wits.nctc.gov”, “element” => “Region”

  • range_constraints - Used for searching for a range of values, also for creating facets or doing co-occurents. This is a #hash of range_constraint names to their options e.g. search_options_object.range_constraints[“Facility Type”] = => “wits.nctc.gov”, “element” => “FacilityType”, “type” => “xs:string”, “collation” => “marklogic.com/collation/”

  • directory_constraint - Used for specifying that the search should only executed for this directory, to the depth specified in directory_depth

  • searchable_expression - An expression to be searched. Whatever expression is specified is returned from the search. This is provided as a hash where the key is the element name and the value is the element’s namespace. If there is no namespace then nil or “” should be passed as the value. eg search_options_object.searchable_expression = “namespace” or search_options_object.searchable_expression = “” if there is no namespace for element

Defined Under Namespace

Classes: AttributeConstraint, ComputedBucket

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMarkLogicSearchOptions

Returns a new instance of MarkLogicSearchOptions.



39
40
41
42
43
44
45
46
47
# File 'lib/ActiveDocument/mark_logic_search_options.rb', line 39

def initialize
  @return_facets = true;
  @value_constraints = Hash.new
  @word_constraints = Hash.new
  @attribute_constraints = Hash.new
  @range_constraints = Hash.new
  @searchable_expression = Hash.new
  @directory_depth = 1
end

Instance Attribute Details

#attribute_constraintsObject

Returns the value of attribute attribute_constraints.



37
38
39
# File 'lib/ActiveDocument/mark_logic_search_options.rb', line 37

def attribute_constraints
  @attribute_constraints
end

#directory_constraintObject

Returns the value of attribute directory_constraint.



37
38
39
# File 'lib/ActiveDocument/mark_logic_search_options.rb', line 37

def directory_constraint
  @directory_constraint
end

#directory_depthObject

Returns the value of attribute directory_depth.



37
38
39
# File 'lib/ActiveDocument/mark_logic_search_options.rb', line 37

def directory_depth
  @directory_depth
end

#range_constraintsObject

Returns the value of attribute range_constraints.



37
38
39
# File 'lib/ActiveDocument/mark_logic_search_options.rb', line 37

def range_constraints
  @range_constraints
end

#return_facetsObject

Returns the value of attribute return_facets.



37
38
39
# File 'lib/ActiveDocument/mark_logic_search_options.rb', line 37

def return_facets
  @return_facets
end

#searchable_expressionObject

Returns the value of attribute searchable_expression.



37
38
39
# File 'lib/ActiveDocument/mark_logic_search_options.rb', line 37

def searchable_expression
  @searchable_expression
end

#value_constraintsObject

Returns the value of attribute value_constraints.



37
38
39
# File 'lib/ActiveDocument/mark_logic_search_options.rb', line 37

def value_constraints
  @value_constraints
end

#word_constraintsObject

Returns the value of attribute word_constraints.



37
38
39
# File 'lib/ActiveDocument/mark_logic_search_options.rb', line 37

def word_constraints
  @word_constraints
end

Instance Method Details

#to_sObject

outputs the object in correctly formatted XML suitable for use in a search



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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ActiveDocument/mark_logic_search_options.rb', line 50

def to_s
  constraints = String.new
  additional_query = String.new
  searchable_path = String.new

  @value_constraints.each do |key, value|
    constraints << <<-XML
    <constraint name="#{key.gsub(/\s/, '_')}">
      <value>
        <element ns="#{value["namespace"]}" name="#{value["element"]}"/>
      </value>
    </constraint>
    XML
  end

  @word_constraints.each do |key, value|
    constraints << <<-XML
    <constraint name="#{key.gsub(/\s/, '_')}">
      <word>
        <element ns="#{value["namespace"]}" name="#{value["element"]}"/>
      </word>
    </constraint>
    XML
  end

  @attribute_constraints.each do |constraint_name, attribute_constraint|
    constraints << <<-XML
    <constraint name="#{constraint_name.gsub(/\s/, '_')}">
      <word>
        <attribute ns="#{attribute_constraint.attribute_namespace unless attribute_constraint.attribute_namespace.nil?}" name="#{attribute_constraint.attribute}"/>
      <element ns="#{attribute_constraint.element_namespace unless attribute_constraint.element_namespace.nil?}" name="#{attribute_constraint.element}"/>
      </word>
    </constraint>
    XML
  end

  @range_constraints.each do |key, value|
    constraints << <<-XML
    <constraint name="#{key.gsub(/\s/, '_')}">
      <range type="#{value["type"]}"
    XML
    if value.has_key?("collation")
      constraints << "collation=\"#{value["collation"]}\""
    end

    constraints << <<-XML
        >
        <element ns="#{value["namespace"]}" name="#{value["element"]}"/>
    XML

    if value.has_key?("computed_buckets")
      value["computed_buckets"].each do |computed_bucket|
        constraints << computed_bucket.to_s if computed_bucket.instance_of?(ActiveDocument::MarkLogicSearchOptions::ComputedBucket)
      end
    end
    constraints << "</range></constraint>"
  end

  # serialize the additional query information
  if @directory_constraint
    additional_query = "<additional-query>{cts:directory-query(\"#{directory_constraint}\", \"#{directory_depth}\")}</additional-query>"
  end

  # serialize the searchable_expression
  if @searchable_expression.size > 0
    searchable_path << "<searchable-expression"
    searchable_path << "  xmlns:a=\"#{@searchable_expression.keys[0]}\"" unless @searchable_expression.keys[0].nil? or @searchable_expression.keys[0].empty?
    searchable_path << '>/'
    searchable_path << "a:" unless @searchable_expression.keys[0].nil? or @searchable_expression.keys[0].empty?
    searchable_path << "#{@searchable_expression.values[0]}</searchable-expression>"
  end

  value = "<options xmlns=\"http://marklogic.com/appservices/search\">"
  unless additional_query.empty?
    value << additional_query
  end

  # add in constraints
  unless constraints.empty?
    value << constraints
  end
  value << "<return-facets>#{@return_facets}</return-facets>"

  # output the searchable expression
  unless searchable_path.empty?
    value << searchable_path
  end
  # close the options node
  value << "</options>"

end