<?xml version="1.0" encoding="UTF-8"?>
<wiki>
  <body>&lt;p&gt;The query language is simple, but supports several constructs to make more complex and fine-grained queries.&lt;/p&gt;
&lt;h3&gt;Keywords and phrases&lt;/h3&gt;
&lt;p&gt;The query language will separate keywords by whitespace, so &lt;code&gt;full moon&lt;/code&gt; will be parsed as a list of two keywords. To make it a single phrase, add double quotes: &lt;code&gt;&quot;full moon&quot;&lt;/code&gt;. Scoped search will look in all textual search fields for occurrences of every keyword; only records in which all keywords occur at least once will be returned. If a keyword is numeric (e.g. &lt;code&gt;33&lt;/code&gt; or &lt;code&gt;-4.31&lt;/code&gt;), numerical fields will be searched as well. A string that can be parsed as a date will also cause a search in date and datetime fields.&lt;/p&gt;
&lt;h3&gt;Logical operators&lt;/h3&gt;
&lt;p&gt;The query language supports the logical &lt;span class=&quot;caps&quot;&gt;AND&lt;/span&gt; OR and &lt;span class=&quot;caps&quot;&gt;NOT&lt;/span&gt; operators, including some alternatives using symbols: &amp;amp; and &amp;amp;&amp;amp; for the &lt;span class=&quot;caps&quot;&gt;AND&lt;/span&gt; operator, | and || for the OR operator, and &amp;#8211; and ! for the &lt;span class=&quot;caps&quot;&gt;NOT&lt;/span&gt; operator. Note that by default, the &lt;span class=&quot;caps&quot;&gt;AND&lt;/span&gt; operator is used to combine search keywords, so &lt;code&gt;full moon&lt;/code&gt; equals &lt;code&gt;full, moon&lt;/code&gt; and &lt;code&gt;full &amp;amp;&amp;amp; moon&lt;/code&gt;. Moreover, parenthesis are supported to structure you logic: &lt;br /&gt; &lt;code&gt;police (car || uniform), NOT(costume OR &quot;village people&quot;)&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;Comparison operators&lt;/h3&gt;
&lt;p&gt;The query language support some common operators to make a more fine-grained query. The operators semantics depend on the type of the field. Moreover, by providing a field, only the specified field will be searched for the given value. Some examples: &lt;code&gt;&amp;gt; 44&lt;/code&gt;, &lt;code&gt;username = root&lt;/code&gt;, &lt;code&gt;created &amp;lt; 2009-01-01&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Some notes:&lt;/p&gt;
&lt;table&gt;
	&lt;tr&gt;
		&lt;th&gt;Operator &lt;/th&gt;
		&lt;th&gt;Symbol &lt;/th&gt;
		&lt;th&gt;Notes &lt;/th&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt; = &lt;/td&gt;
		&lt;td&gt; :eq &lt;/td&gt;
		&lt;td&gt; A case sensitive equality comparison for text fields, a normal equality comparison for numerical and temporal fields &lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt; !=, &amp;lt;&amp;gt; &lt;/td&gt;
		&lt;td&gt; :ne &lt;/td&gt;
		&lt;td&gt; The inverse/negation of the = operator &lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt; ~ &lt;/td&gt;
		&lt;td&gt; :like &lt;/td&gt;
		&lt;td&gt; Case insensitive occurrence/contains search for textual fields (i.e. &lt;span class=&quot;caps&quot;&gt;LIKE&lt;/span&gt;/&lt;span class=&quot;caps&quot;&gt;ILIKE&lt;/span&gt;) &lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt; !~ &lt;/td&gt;
		&lt;td&gt; :unlike &lt;/td&gt;
		&lt;td&gt; The inverse/negation of the ~ operator (&lt;span class=&quot;caps&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;caps&quot;&gt;LIKE&lt;/span&gt;/&lt;span class=&quot;caps&quot;&gt;ILIKE&lt;/span&gt;) &lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt; &amp;gt; &lt;/td&gt;
		&lt;td&gt; :gt &lt;/td&gt;
		&lt;td&gt; Greater than. Only supported for numerical and temporal fields &lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt; &amp;lt; &lt;/td&gt;
		&lt;td&gt; :lt &lt;/td&gt;
		&lt;td&gt; Less than. Only supported for numerical and temporal fields &lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt; &amp;gt;= &lt;/td&gt;
		&lt;td&gt; :gte &lt;/td&gt;
		&lt;td&gt; Greater than or equals. Only supported for numerical and temporal fields &lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt; &amp;lt;= &lt;/td&gt;
		&lt;td&gt; :lte &lt;/td&gt;
		&lt;td&gt; Less than or equals. Only supported for numerical and temporal fields &lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;If no operator is given, the query builder will determine a default operator based on the field type, i.e. &lt;code&gt;~&lt;/code&gt; for textual fields, &lt;code&gt;=&lt;/code&gt; for other field types. The default operator for a field can be overridden in the [[search definition]].&lt;/p&gt;
&lt;p&gt;When using a date to search a datetime field, the query is converted to return more logical results, e.g. &lt;code&gt;created = 2009-01-01&lt;/code&gt; becomes &lt;code&gt;(created &amp;gt;= &quot;2009-01-01 00:00:00&quot; AND created &amp;lt; &quot;2009-01-02 00:00:00&quot;)&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;&lt;span class=&quot;caps&quot;&gt;NULL&lt;/span&gt; operators&lt;/h3&gt;
&lt;p&gt;It is possible to check if a field has any value or is set to the &lt;span class=&quot;caps&quot;&gt;NULL&lt;/span&gt; value, using the &lt;code&gt;set?&lt;/code&gt; and &lt;code&gt;null?&lt;/code&gt; operators, following the field name: &lt;code&gt;set? deleted_at&lt;/code&gt;, &lt;code&gt;null? error_messages&lt;/code&gt;. Note that this is only supported if the field is included in the [[search definition]].&lt;/p&gt;
&lt;h2&gt;&lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; query generation&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; scoped_search includes adapters for every &lt;span class=&quot;caps&quot;&gt;DBMS&lt;/span&gt; to make sure that the generated &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; will return the same records for every &lt;span class=&quot;caps&quot;&gt;DBMS&lt;/span&gt; type. Because of this, the generated &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; might differ among DBMSs.&lt;/p&gt;
&lt;p&gt;In these example, the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;description&lt;/code&gt; field of the Person model are being searched by calling &lt;code&gt;scoped_search :on =&amp;gt; [:name, :description]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Single keyword. &lt;code&gt; scoped_search&lt;/code&gt;:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;SELECT * FROM people 
 WHERE (name LIKE '%scoped_search%' OR description LIKE '% scoped_search%')
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Multiple keywords. &lt;code&gt;Willem Wes scoped_search&lt;/code&gt;:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;SELECT * FROM people
 WHERE (name LIKE '%Willem%' OR description LIKE '%Willem%')
   AND (name LIKE '%Wes%' OR description LIKE '%Wes%')
   AND (name LIKE '%scoped_search%' OR description LIKE '%scoped_search%')
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Phrases, using quotes, are dealt with like single keywords, e.g. &lt;code&gt;&quot;Great plugin&quot;&lt;/code&gt;:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;SELECT * FROM people 
 WHERE (name LIKE '%Great plugin%' OR description LIKE '%Great plugin%')
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;</body>
  <created-at type="datetime">2010-02-09T18:49:54-08:00</created-at>
  <id type="integer">125161</id>
  <permalink>query-language</permalink>
  <repository-id type="integer">37427</repository-id>
  <title>Query language</title>
  <updated-at type="datetime">2009-08-31T03:41:16-07:00</updated-at>
  <user-id type="integer">15870</user-id>
</wiki>
