12
$\begingroup$

Steps to install and run ElasticSearch:

  1. install Java (64-bit): https://www.java.com/en/download/manual.jsp

  2. download ElasticSearch (https://www.elastic.co/start) and extract archive in C:\elasticsearch

  3. in Command Prompt: C:\elasticsearch\bin\elasticsearch.bat

How we can connect to ElasticSearch from Mathematica, put data in index, make queries?

$\endgroup$
2
  • 1
    $\begingroup$ Could you expand the question part of this Q/A a bit so it looks more like an actual question? $\endgroup$ Commented Oct 1, 2019 at 18:38
  • 1
    $\begingroup$ Hello @b3m2a1! See my edits. Feel free to edit my question and answer. $\endgroup$ Commented Oct 1, 2019 at 18:45

1 Answer 1

13
$\begingroup$

Create the new index test and put the data in it:

MapIndexed[
 URLExecute@HTTPRequest[
    <|
     "Domain" -> "localhost", "Port" -> 9200,
     "Path" -> {"test", "external", ToString[First@#2]}
     |>,
    <|
     Method -> "PUT",
     "Body" -> ExportString[<|"Text" -> #1|>, "PythonExpression", CharacterEncoding -> "Unicode"],
     "ContentType" -> "application/json"
     |>
    ] &,
 (*DATA*){"abc", "def", "ghi"}
 ]

{{"_index" -> "test", "_type" -> "external", "_id" -> "1", "_version" -> 1, "result" -> "created", "_shards" -> {"total" -> 2, "successful" -> 1, "failed" -> 0}, "_seq_no" -> 0, "_primary_term" -> 1}, {"_index" -> "test", "_type" -> "external", "_id" -> "2", "_version" -> 1, "result" -> "created", "_shards" -> {"total" -> 2, "successful" -> 1, "failed" -> 0}, "_seq_no" -> 1, "_primary_term" -> 1}, {"_index" -> "test", "_type" -> "external", "_id" -> "3", "_version" -> 1, "result" -> "created", "_shards" -> {"total" -> 2, "successful" -> 1, "failed" -> 0}, "_seq_no" -> 2, "_primary_term" -> 1}}

Search:

URLExecute@HTTPRequest[
  <|
   "Domain" -> "localhost", "Port" -> 9200,
   "Path" -> {"test", "_search"},
   "Query" -> <|"q" -> "Text: g*"|>
   |>,
  <|Method -> "GET"|>
  ]

{"took" -> 1, "timed_out" -> False, "_shards" -> {"total" -> 1, "successful" -> 1, "skipped" -> 0, "failed" -> 0}, "hits" -> {"total" -> {"value" -> 1, "relation" -> "eq"}, "max_score" -> 1., "hits" -> {{"_index" -> "test", "_type" -> "external", "_id" -> "3", "_score" -> 1., "_source" -> {"Text" -> "ghi"}}}}}

Delete document from index:

URLExecute@HTTPRequest[
  <|
   "Domain" -> "localhost", "Port" -> 9200,
   "Path" -> {"test", "external", "3"}
   |>,
  <|Method -> "DELETE"|>
  ]

{"_index" -> "test", "_type" -> "external", "_id" -> "3", "_version" -> 2, "result" -> "deleted", "_shards" -> {"total" -> 2, "successful" -> 1, "failed" -> 0}, "_seq_no" -> 3, "_primary_term" -> 1}

Search again:

URLExecute@HTTPRequest[
  <|
   "Domain" -> "localhost", "Port" -> 9200,
   "Path" -> {"test", "_search"},
   "Query" -> <|"q" -> "Text: g*"|>
   |>,
  <|Method -> "GET"|>
  ]

{"took" -> 2, "timed_out" -> False, "_shards" -> {"total" -> 1, "successful" -> 1, "skipped" -> 0, "failed" -> 0}, "hits" -> {"total" -> {"value" -> 0, "relation" -> "eq"}, "max_score" -> Null, "hits" -> {}}}

Delete index:

URLExecute@HTTPRequest[
  <|
   "Domain" -> "localhost", "Port" -> 9200,
   "Path" -> {"test"}
   |>,
  <|"Method" -> "DELETE"|>
  ]

{"acknowledged" -> True}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.