The above example uses GSON to convert the Part object into JSON, and illustrates how to correctly format dates to be accepted by Elastic Search. The prepareIndex() method takes the name of the index and the name of the document, then the sets the source of the document to the JSON. The response that is return will contain the unique String id of the newly indexed document.

To get the specific document in the above example, we have supplied the Elastic Search id of the document to the prepareGet() and parse the source of the document that is returned in the GetResponse object. Really straight-forward!

Another fairly straight-forward operation – Simply cal the prepareSearch() method with a .setQuery() to match all entries of the given document type. The resulting SearchResponse object will contain the “hits”. The hits will contain a source which can be extracted as a String and parsed using a JSON parser such as GSON.

The above example demonstrates how to search for a specific document where the field “PartNumber” starts with the specific search term. This is achieved using the query builder “prefixQuery()” supplying both the field name and the search term to search for. Buy default, when indexing a new document, elastic search will pass the document through an “analyser” which will create “terms” for each of the fields so the can be searched. For example: The value “One-234” will result in 2 terms being created: “one” and “234. Elastic search has split the value by the dash and lowercased the values. For this reason, searching for the field value “One-234” or “one-” would not actually find a document containing a field with the value “One-234”. It would find the document if you search for “On” or “23” for example. Note that with the prefixQuery() builder using the the text “34” wouldn’t find the document.

There are a large number of built in query builders, and you can also create custom analyser’s. Or remove the analysers entirely in the settings.

Going back to the example above – given that SearchReponse contains a number of “hits”, you will notice that you can get the list of hits, iterate on them, and parse the source using GSON.