Class SqlSearchEngine
java.lang.Object
com.codename1.backend.search.SqlSearchEngine
- All Implemented Interfaces:
SearchEngine, Closeable, AutoCloseable
Full-text search in the application's own SQL database, for a project that does not run a search cluster.
Documents go into one table, cn1_search_documents, created when the engine
opens, with every string of a document joined into a body column:
- PostgreSQL matches with
websearch_to_tsquery('simple', ...)over a GIN index and ranks withts_rank. Thesimpleconfiguration lowercases but does not stem, so "shoe" does not find "shoes"; it is the one configuration that behaves the same for every language. - MySQL and MariaDB match every word with a
FULLTEXTindex in boolean mode. InnoDB does not index words shorter thaninnodb_ft_min_token_size(3) or its default stop words, so those are matched as whole words in memory over what the index found. - SQLite narrows with
LIKE, one condition per word, then requires each word as a whole word, and scores by how often the words occur. Correct, and linear in the size of the index.
Matching, as on a search cluster, requires every word. Filters, field
restrictions, sorting, facets and paging are then applied in memory over at
most MAX_CANDIDATES matches; past that SearchResult.isTotalExact() is
false, and a filter or a sort sees only those candidates (the best scored,
or the first by id for an empty query), so a selective filter over a larger
index belongs in a search cluster. Highlights are not produced.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe most matches read from the database for one query. -
Constructor Summary
ConstructorsConstructorDescriptionSqlSearchEngine(DataSource data, boolean ownsData) An engine overdata, creating its table if needed. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Releases connections.booleanRemoves a document; returns whether there was one.voiddeleteIndex(String index) Removes an index and every document in it; a missing one is not an error.The document stored underid, or null.voidAdds a document underid, replacing any already there.voidMakes every write so far visible toSearchEngine.search(String, SearchQuery).search(String index, SearchQuery q) Runs a query.
-
Field Details
-
MAX_CANDIDATES
public static final int MAX_CANDIDATESThe most matches read from the database for one query.- See Also:
-
-
Constructor Details
-
SqlSearchEngine
An engine overdata, creating its table if needed.ownsDatacloses the data source along with the engine.- Throws:
IOException
-
-
Method Details
-
index
Description copied from interface:SearchEngineAdds a document underid, replacing any already there.- Specified by:
indexin interfaceSearchEngine- Throws:
IOException
-
delete
Description copied from interface:SearchEngineRemoves a document; returns whether there was one.- Specified by:
deletein interfaceSearchEngine- Throws:
IOException
-
get
Description copied from interface:SearchEngineThe document stored underid, or null.- Specified by:
getin interfaceSearchEngine- Throws:
IOException
-
search
Description copied from interface:SearchEngineRuns a query.- Specified by:
searchin interfaceSearchEngine- Throws:
IOException
-
refresh
Description copied from interface:SearchEngineMakes every write so far visible toSearchEngine.search(String, SearchQuery). Search clusters publish writes about once a second; tests and read-your-write flows call this.- Specified by:
refreshin interfaceSearchEngine
-
deleteIndex
Description copied from interface:SearchEngineRemoves an index and every document in it; a missing one is not an error.- Specified by:
deleteIndexin interfaceSearchEngine- Throws:
IOException
-
close
public void close()Description copied from interface:SearchEngineReleases connections. An engine fromSearchEngines.current()is closed by the server and must not be closed by the caller.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceSearchEngine
-