ropensci / elastic

R client for the Elasticsearch HTTP API
https://docs.ropensci.org/elastic
Other
245 stars 58 forks source link

Searching phrases #222

Closed fissehab closed 5 years ago

fissehab commented 6 years ago

I want to search exact phrases. How can I do it? Basically, I want to use something like below

"query": { "query_string" : { "type": "phrase", "query" : "(cloud computing) OR (supply chain)" } }

sckott commented 6 years ago

does this do what you want? https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-match-query-phrase.html

fissehab commented 6 years ago

Thanks. But how can I use that in R to search any of the phrases shown above?

sckott commented 6 years ago
library(elastic)
connect()
shakespeare <- system.file("examples", "shakespeare_data_.json", package = "elastic")
invisible(docs_bulk(shakespeare))

body_match <- '{
  "query": {
    "match" : {
      "text_entry": "the men"
    }
  }
}'
body_match_phrase <- '{
  "query": {
    "match_phrase" : {
      "text_entry": "the men"
    }
  }
}'
Search("shakespeare", body = body_match)$hits$total
Search("shakespeare", body = body_match_phrase)$hits$total
fissehab commented 6 years ago

I have many phrases and also the phrases are dynamic. They are search input on a shiny app . Using query string looks easier to implement if it is possible to search phrases. But with the above approach you provided, how can I search many phrases? By just repeating the "match_phrase" for each phrase?

sckott commented 6 years ago

i'm not sure. i'd google/search stackoverflow. or ask on the elasticsearch discussion forum https://discuss.elastic.co/

sckott commented 5 years ago

hopefully you've found an answer, it sort of falls out of scope of this package as it's more of a how to for Elasticsearch database itself