Chapter 4. Search
Everything you’ve learned so far about creating indexes is pretty useless if you don’t know how to use those indexes to find what you are looking for. After all, that’s what Ferret is for. This chapter covers everything you need to know about searching in Ferret. We’ll start with the basic search classes followed by the various types of query. We’ll then talk about the query parser and Ferret’s own query language—FQL. We’ll then cover some more advanced topics such as sorting, filtering, and highlighting.
Overview of Searching Classes
Ferret’s search API is about as simple as its indexing API. In fact,
if you are using the Index class, all
you have to know is the search_each()
method and a little bit of Ferret’s query language and you are set.
However, if you take the time to learn the rest of the search API, you’ll
discover a wealth of opportunities you didn’t even know existed.
The search API consists of the following classes:
IndexSearcherQueryQueryParserFilterSort
IndexSearcher
IndexSearcher, as the
name would suggest, is used to search indexes. You can
also use it to highlight and explain query results and read documents
from the index (as you would with IndexReader). To create an IndexSearcher, you need to supply it with an
IndexReader:
reader=IndexReader.new("path/to/index")searcher=Searcher.new(reader)
As usual, you can shortcut this by supplying it with a Directory or a filesystem path to the
index:
searcher=Searcher.new("path/to/index")
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access