Michael Born
Web developer at Ortus Solutions
Users expect search to be:
An open-source distributed search server built on Lucene
{
"properties": {
"primaryTitle": "The Fellowship Of The Ring",
"titleType": "movie",
"runtime_mins": "178",
"startYear" : "2001",
"budget" : "93,000,000",
"box_office_gross": "887,800,000"
}
}
{
"_index": "flicks",
"_type": "_doc",
"_id": "1lH95XABhTaGTn6QUVJH",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1043003,
"_primary_term": 3
}
A collection of documents which are tokenized (indexed) on insert in preparation for search.
Breaking a text string down into small, self-contained search terms.
the quick brown fox jumps over the lazy dog
quick
brown
fox
jumps
over
the
lazy
dog
A definition of fields and field types for a specific index.
{
"primaryTitle" : "text",
"titleType" : "keyword",
"runtime_mins" : "integer",
"startYear" : "integer",
"budget" : "integer",
"box_office_gross" : "integer"
}
A single record within an index
{
"primaryTitle": "The Fellowship Of The Ring",
"titleType": "movie",
"runtime_mins": "178",
"startYear" : "2001",
"budget" : "93,000,000",
"box_office_gross": "887,800,000"
}
A CFML wrapper for the ElasticSearch API which provides an easy way to:
docker run -d \
-p 9200:9200 \
-e "discovery.type=single-node" \
--name="myApp_ES" \
elasticsearch:7.5.1
box install cbelasticsearch
// config/Coldbox.cfc
moduleSettings = {
"cbElasticsearch" : {
"hosts" : [ {
"serverProtocol" : "http",
"serverName" : "127.0.0.1",
"serverPort" : "9200"
} ],
"defaultIndex" = "reviews"
}
}
component {
/**
* Initialize the ElasticSearch index on app load/reinit
*/
void function afterConfigurationLoad( event, interceptData ){
// create index...
}
}
function buildMyIndex() {
getInstance( "IndexBuilder@cbElasticsearch" )
.new(
"reviews",
{
"_doc" = {
"_all" = { "enabled" = false },
"properties" = {
"title" = { "type" = "text" },
"authorName" = { "type" = "integer" },
"publishedDate" = { "type" = "text" },
"stars" = { "type" = "keyword" },
"content" = { "type" = "text" }
}
}
}
).save();
}
function createBookIndex() {
getInstance( "IndexBuilder" )
.new( "books", getInstance( "MappingBuilder@cbElasticSearch" )
.create( function( mapping ) {
mapping.text( "title" );
mapping.object( "author", function( mapping ) {
mapping.text( "firstname" );
mapping.text( "lastname" );
} );
} );
} );
}
function insertReview() {
var bookReview = {
"title" : "'Phantom': Lost in Hyperspace",
"authorName" : "Rita Kempley",
"publishedDate" : DateFormat( "1999-05-19" ),
"stars" : "1",
"content" : "The Empire strikes out..."
};
var document = getInstance( "Document@cbElasticsearch" )
.new(
index = "reviews",
type = "_doc",
properties = bookReview
);
document.save();
}
function searchByTitle( required string title ) {
var startDate = createDate( "2018", "12", "31" );
var endDate = createDate( "2020", "04", "08" );
var search = getInstance( "SearchBuilder@cbElasticSearch" )
.new(
index = "news",
type = "_doc"
)
.match( "title", arguments.title )
.dateMatch( "publishedDate", startDate, endDate )
.sort( "publishedDate", "desc" );
return search.execute();
}
var result = getInstance( "SearchBuilder@cbElasticSearch" )
.new(
index = "reviews",
type = "_doc"
)
.match( "content", "star wars" )
.execute();
var result = getInstance( "SearchBuilder@cbElasticSearch" )
.new(
index = "reviews",
type = "_doc"
)
.term( "stars", 5 )
.execute();
var result = getInstance( "SearchBuilder@cbElasticSearch" )
.new(
index = "reviews",
type = "_doc"
)
.mustMatch( "author", "Luis Majano" )
.mustMatch( "content", "star wars" )
.execute();
var result = getInstance( "SearchBuilder@cbElasticSearch" )
.new(
index = "reviews",
type = "_doc"
)
.shouldMatch( "author", "Luis Majano" )
.shouldMatch( "author", "Jon Clausen" )
.execute();
By Michael Born