Search tips and tricks

In order to unleash the true power of our search engine, we've provided you the following tips and tricks to help you narrow down exactly what you are looking for.

This list has been compiled from the official Lucene query parser syntax page.

1. Terms

A query is broken up into terms and operators. There are two types of terms: Single Terms and Phrases. A Single Term is a single word such as "pledge" or "allegiance". A Phrase is a group of words surrounded by double quotes such as "pledge allegiance". Multiple terms can be combined together with Boolean operators to form a more complex query (see below).

2. Wildcard searches

Lucene supports single and multiple character wildcard searches within single terms (not within phrase queries).To perform a single character wildcard search use the "?" symbol. To perform a multiple character wildcard search use the "*" symbol.
The single character wildcard search looks for terms that match that with the single character replaced. For example, to search for "near" or "neat" you can use the search "nea?" (without quotes).
Multiple character wildcard searches looks for 0 or more characters. For example, to search for test, tests or testers, you can use the search "test*" (without quotes) You can also use the wildcard searches in the middle of a term, such as in "te*t".
Note: You cannot use a * or ? symbol as the first character of a search.

3. Fuzzy searching

Lucene supports fuzzy searches based on the Levenshtein Distance, or Edit Distance algorithm. To do a fuzzy search use the tilde, "~", symbol at the end of a Single word Term. For example to search for a term similar in spelling to "swore" use the fuzzy search: "swore~". This search will find terms like swore, snore, score, etc.
Furthermore, an optional parameter can specify the required similarity. The value is between 0 and 1, with a value closer to 1 only terms with a higher similarity will be matched. For example:"swore~0.7" will yield only results that are close to the original query, such as swore, score, etc.
A search query "swore~0.3" will yield results that are slightly similar to the original query, such as swore, stone, swords, etc.
The default that is used if the parameter is not given is 0.5.

4. Proximity Searches

Lucene supports finding words are a within a specific distance away. To do a proximity search use the tilde, "~", symbol at the end of a Phrase. For example to search for a "pledge" and "migration" within 10 words of each other in a document use the search: "pledge migration"~10. This will yield results in which the words pledge and migration are separated by no more then 10 words. Very helpful if you only know the context of what you are searching for.

5. Boosting a term

Lucene provides the relevance level of matching documents based on the terms found. To boost a term use the caret ("^") symbol with a boost factor (a number) at the end of the term you are searching. The higher the boost factor, the more relevant the term will be. Boosting allows you to control the relevance of a document by boosting its term. For example, if you are searching for

    pledge hijrah
and you want the term "pledge" to be more relevant, boost it using the ^ symbol along with the boost factor next to the term. You would type:
    pledge^4 hijrah
This will make results with the term pledge appear more relevant. You can also boost Phrase Terms as in the example:
    "pledge of allegiance"^5 requested
By default, the boost factor is 1. Although the boost factor must be positive, it can be less than 1 (e.g. 0.2)

6. Boolean operators

Boolean operators allow terms to be combined through logic operators. Lucene supports AND, "+", OR, NOT and "-" as Boolean operators (Note: Boolean operators must be ALL CAPS).


The "OR" Operator

The OR operator is the default conjunction operator. This means that if there is no Boolean operator between two terms, the OR operator is used. The OR operator links two terms and finds a matching document if either of the terms exist in a document. This is equivalent to a union using sets. The symbol || can be used in place of the word OR.

To search for documents that contain either "pledge allegiance" or just "pledge" use the query:

    "pledge allegiance" pledge
or
    "pledge allegiance" OR pledge

The "AND" Operator

The AND operator matches documents where both terms exist anywhere in the text of a single document. This is equivalent to an intersection using sets. The symbol && can be used in place of the word AND.

To search for documents that contain "pledge of Allegiance" and "second time" use the query:

    "pledge of Allegiance" AND "second time"

The "+" Operator

The "+" or required operator requires that the term after the "+" symbol exist somewhere in a the field of a single document.

To search for documents that must contain "pledge" and may contain "allegiance" use the query:

    +pledge allegiance

The "NOT" Operator

The NOT operator excludes documents that contain the term after NOT. This is equivalent to a difference using sets. The symbol ! can be used in place of the word NOT.

To search for documents that contain "pledge allegiance" but not "second time" use the query:

    "pledge of Allegiance" NOT "second time"

Note: The NOT operator cannot be used with just one term. For example, the following search will return no results:

    NOT "pledge allegiance"

The "-" Operator

The "-" or prohibit operator excludes documents that contain the term after the "-" symbol.


To search for documents that contain "pledge allegiance" but not "allegiance to the Messenger" use the query:

    "pledge allegiance"-"allegiance to the Messenger"

7. Grouping

Lucene supports using parentheses to group clauses to form sub queries. This can be very useful if you want to control the boolean logic for a query.

To search for either "pledge" or "allegiance" and "prayer" use the query:

    (pledge OR allegiance) AND prayer
This eliminates any confusion and makes sure you that prayer must exist and either term pledge or allegiance may exist.