使用斜杠在R中查询json

问题描述 投票:0回答:1

我想从R中的弹性数据集中下载。到目前为止一切正常。

library(jsonlite)

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

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

我的问题是我想过滤一个新类型:/ tests / category / task1

我试过了:

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

这给了我错误

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

然后我尝试了:

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

产生错误:

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) ------^ 

P 03:

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

错误:

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

V04:

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

错误:

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

有谁知道如何处理查询中的/?

UPDATE1:

@hrbrmstr这是确切的代码和错误

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
)

错误:

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
json r jsonlite
1个回答
0
投票

jsonlite做繁重的工作:

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"
##       }
##     }
##   }
## }
© www.soinside.com 2019 - 2024. All rights reserved.