Full Text Search of Indexed JSON Documents
This section presents the steps to execute a simple but complete example, without security. Although in a production setting, both the Oracle NoSQL Database and the Elasticsearch cluster should generally be run on separate nodes, for simplicity, these steps are executed on a single node. Additionally, if you already have an Elasticsearch version 2 cluster running in your environment, then feel free to use that cluster in place of the Elasticsearch single-node cluster used below. Note finally, that you may have to change some of the tokens (directory locations, version numbers, etc.) to suit your particular environment.
-
Download, install, and run Elasticsearch, version 2.
Download the tar file https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.4.6/elasticsearch-2.4.6.tar.gz and place it under the directory
/opt/es.cd /opt/es tar xzvf elasticsearch-2.4.6.tar.gz ln -s elasticsearch-2.4.6 elasticsearch export JAVA_HOME=/usr/lib/jvm/java8 /opt/es/elasticsearch/bin/elasticsearch -Dnetwork.host=localhost --cluster.name kv-es-cluster --node.name localhostNote: Elasticsearch version 2 requires Java 8. Thus, you should install Java 8 and set the
JAVA_HOMEenvironment to point to the Java 8’s home directory. -
Use KVLite to deploy an Oracle NoSQL Database store named
kvstore.Assuming that you have installed Oracle NoSQL Database under the directory
/opt/ondb, and that you have write permission for your system’s/tmpdirectory, execute the following command from a command line:java -jar /opt/ondb/kv/lib/kvstore.jar kvlite -root /tmp/kvroot -host localhost -port 5000 -store kvstore -secure-config disable -
Start the Oracle NoSQL Database Admin CLI.
From a separate command window, execute the command:
java -jar /opt/ondb/kv/lib/kvstore.jar runadmin -host localhost -port 5000 -store kvstore -
Install a file containing the JSON documents to load.
Under a directory such as
~/examples/es/docs, create a file namedsenator-info.jsonand populate it with one or more JSON documents like those shown in the example file presented inSample: Array of JSON Documents. Be sure to format the file you create with the same format shown inSample: Array of JSON Documents. -
Compile and execute the
LoadJsonExampleprogram (or similar).Under a directory such as
~/examples/es/src, create the sub-directoryes/table, and then create a file namedLoadJsonExample.javaunder the directory~/examples/es/src/es/table. After creating the file~/examples/es/src/es/table/LoadJsonExample.java, add the source code presented inThe LoadJsonExample Program Source (or source with similar functionality).Once the
LoadJsonExample.javaprogram is created, execute the following from a separate command window:cd ~/examples/es/src javac -classpath /opt/ondb/kv/lib/kvstore.jar:src examples/es/table/LoadJsonExample.java java -classpath /opt/ondb/kv/lib/kvstore.jar:src es.table.LoadJsonExample -store kvstore -host localhost -port 5000 -file ~/examples/es/docs/senator-info.json -table exampleJsonTableNote: The source code for the
LoadJsonExampleprogram that is presented in The LoadJsonExample Program Source is only intended to provide a convenient mechanism for loading non-trivial JSON content into an Oracle NoSQL table. You should feel free to write your own program to provide similar functionality. -
Create a Text Index on the JSON data loaded into the NoSQL table.
After verifying that the table has been successfully created and populated with the desired table data, execute the following from the Admin CLI:
kv-> plan register-es -clustername kv-es-cluster -host localhost -port 9200 -secure false -wait kv-> execute 'CREATE FULLTEXT INDEX jsonTxtIndex ON exampleJsonTable ( jsonField.current{"type":"boolean"}, jsonField.party{"type":"string","analyzer":"standard"}, jsonField.duties.committe{"type":"string"}, jsonField.contrib{"type":"double"})'; -
Execute Full Text Search queries against data indexed in Elasticsearch.
To first verify that the desired index has been created in Elasticsearch as expected, execute the following from a command line:
curl -X GET 'http://localhost:9200/_cat/indices' yellow open ondb.kvstore._checkpoint ... yellow open ondb.kvstore.examplejsontable.jsontxtindex ...Note that Elasticsearch reports the status of each index is yellow. This occurs here because the Elasticsearch cluster was deployed as a single-node cluster.
To examine the mapping that Oracle NoSQL constructs for Elasticsearch, execute:
curl -X GET 'http://localhost:9200/ondb.kvstore.examplejsontable.jsontxtindex/_mapping?pretty'To display all documents from the
exampleJsonTablethat were indexed in Elasticsearch, execute:curl -X GET 'http://localhost:9200/ondb.kvstore.examplejsontable.jsontxtindex/_search?pretty'Finally, to find all current democratic senators with contributions totaling between 5 million and 15 million dollars, who are members of either the “Progressive” caucus or the “Human Rights” caucus, execute the following command:
curl -X GET 'http://localhost:9200/ondb.kvstore.examplejsontable.jsontxtindex/_search?pretty' '-d {query":{"bool":{ "must":{"match":{"jsonField.party":"Democrat"}}, "must":{"match":"jsonField.current":"true"}}, "must":{"range":{"jsonField.contrib":{"gte":"5000000.00","lte":15000000.00"}}}, "must":"match":{"jsonField.duties.caucus":"Progressive Human Rights"}}}}}'