11
$\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$
  • 1
    $\begingroup$ Could you expand the question part of this Q/A a bit so it looks more like an actual question? $\endgroup$ – b3m2a1 Oct 1 '19 at 18:38
  • 1
    $\begingroup$ Hello @b3m2a1! See my edits. Feel free to edit my question and answer. $\endgroup$ – Alexey Golyshev Oct 1 '19 at 18:45
12
$\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, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.