0

I want to download a dataset from elastic in R. Everything works so far.

library(jsonlite)

filter <- paste('"',
            "type: type1",
            '"', 
          sep = "")

q <- paste(
  '{
  "query": {
    "query_string" : {
      "query" : ', filter,
    '}
   }
  }'
)

My Problem is that I want to filter a new type: /tests/category/task1

I tried:

filter <- paste('"',
               "type:/tests/category/task1",
               '"',
          sep = "")

That gave me the error

Fehler: 400 - all shards failed
ES stack trace:
type: query_shard_exception
reason: Failed to parse query [type:/tests/category/task1]

Then I tried:

filter <- paste('"',
               "type: \"/tests/category/task1\" ",
               '"',
          sep = "")

Producing the Error:

Fehler in check_inputs(body) : 
lexical error: probable comment found in input text, comments are not enabled.
ternal-response  type: "/tests/category/task1
     (right here) ------^ 

V03:

filter <- paste('"',
               "type: \"\/tests\/category\/task1\" ",
               '"',
          sep = "")

Error:

Fehler: '\/' ist eine unbekannte Escape-Sequenz in der Zeichenkette beginnend mit ""type: \"\/

V04:

filter <- paste('"',
               "type: \"\\/tests\\/category\\/task1\" ",
               '"',
          sep = "")

Error:

Fehler in check_inputs(body) : lexical error: invalid char in json text.
           type: "\/tests\/category\/task1"
(right here) ------^

Does anyone know how to handle the / in the query?

Update1:

@hrbrmstr This is the exact code and error

library(jsonlite)
library(elastic)
library(httr)

set_config(config(ssl_verifypeer = FALSE, ssl_verifyhost = FALSE))
connect(
  es_host = my_host,
  es_port = my_port,
  es_transport_schema = "https",
  es_user = my_user,
  es_pwd = my_pwd,
  errors = "complete"
)

mk_query <- function(typ) {
  list(
    query = list(
      query_string = list(
        query = list(
          type = unbox(typ)
        )
      )
    )
  ) -> qry
  toJSON(qry, pretty=TRUE)
}

q <- mk_query("type1")

res <- Search(
  index = my_index,
  size = 500,
  time_scroll = "1m",
  body = q,
  raw = FALSE
)

Error:

Fehler: 400 - [query_string] unknown token [START_OBJECT] after [query]
ES stack trace:

  type: parsing_exception
  reason: [query_string] unknown token [START_OBJECT] after [query]
  line: 1
  col: 50
Lale
  • 1
  • 3

1 Answers1

0

Let jsonlite do the heavy lifting:

library(jsonlite)

mk_query <- function(typ) {
  list(
    query = list(
      query_string = list(
        query = list(
          type = unbox(typ)
        )
      )
    )
  ) -> qry
  toJSON(qry, pretty=TRUE)
}

mk_query("type1")
## {
##   "query": {
##     "query_string": {
##       "query": {
##         "type": "type1"
##       }
##     }
##   }
## }

mk_query("/tests/category/task1")
## {
##   "query": {
##     "query_string": {
##       "query": {
##         "type": "/tests/category/task1"
##       }
##     }
##   }
## }
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • I tried your suggestion but R gives me the following error (for both type = type1 and type = /tests/category/task1) `Fehler: 400 - [query_string] unknown token [START_OBJECT] after [query] ES stack trace: type: parsing_exception reason: [query_string] unknown token [START_OBJECT] after [query] line: 1 col: 50 ` I compared the output of mk_query to my old query (that worked for type = type1) and found that I had no braces before and after type. How can I suppress them? – Lale Dec 19 '17 at 14:31
  • You need to add _exactly_ what you typed and the error message to the question. it's hard to triage code/errors in comments – hrbrmstr Dec 19 '17 at 16:33
  • 1
    @Lale `elastic` maintainer here. if you aren't already, make sure to connect to Elasticsearch with verbose errors like `connect(errors = "complete")`. – sckott Dec 19 '17 at 22:30
  • @ hrbrmstr I added the full code and error to my question (see **Update 1**) @ sckott: I already had – Lale Dec 20 '17 at 12:13