Elastic search- search exact matching string using query UI - hadoop

Here is my JSON .
{
"id":100,
"name":"xxx",
"hobbies":["cricket","footbal","singing & dancing"]
}
I need to filter "singing & dancing" string from "others". Executed below query.
http://localhost:9200/employeed/data/_search?q={"query":{"query_string":{"query" : "hobbies:Singing & dancing"}}}
I am getting below exception.
"type": "illegal_argument_exception",
"reason": "request [employee/data/_search] contains unrecognized parameter: [ Singing\"}}}]"
Any help?

You're attempting to send a DSL query in a URI. The two don't mix, see here: https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-search.html
The search API allows you to execute a search query and get back search hits that match the query. The query can either be provided using a simple query string as a parameter, or using a request body.
You can either use the DSL and send it in your request body, or change your URL to just include the hobbies query_string you want to use. Like this:
http://localhost:9200/employeed/data/_search?q=hobbies%3A%5C%22Singing%20%26%20dancing%5C%22
The query_string portion is also URL encoded. I've also added quotes around your search value, otherwise it will OR them together. The unencoded version:
hobbies:\"Singing & dancing\"

Related

How to submit queries from the elastic cloud api console?

I'm new to the elastic-cloud interface. It allows to chooose operations get, post, put and del. I'm trying to submit queries, but I don't know the precise syntax. For instance:
tweet/_search?q=something
works, but:
tweet/_search?q={ "match_all": {} }
does not, returning a parser error. I have tried with double quotes, but it seems that then it searches for the query as a string.
The preferred way to test the search APIs are using the POST method, GET API in some case, gives even incorrect search results as it ignores the search and brings the top 10 search results for match_all query.
Elasticsearch supports both methods GET and POST to search but using the GET method which has payload information isn't common on modern app-severs, although Elasticsearch implemented it requires carefully crafting your queries.
Still, if you want to use the GET API, then for complex queries its better to send it as part of request body, I know it sounds weird to send a body to GET request but it works ๐Ÿ˜€ .

ElasticSearch: How to use filter_path parameter in POST body

So I can successfully do request like:
localhost:9200/filebeat-*/_count?filter_path=-_shards
{"query": {
"match_phrase" : {
"message" : "o hohoho"
}
}}
How I can move filter_path=-_shards into the request body to make it work?
According to the documentation code, still not possible in Elasticsearch 6.2:
All REST APIs accept a filter_path parameter that can be used to
reduce the response returned by Elasticsearch
and it's impossible to include it into the request body, that's just not supported (to be honest, I'm not sure if it will ever be supported).
However, for some scenarios, you could limit response returned by Elasticsearch by using source filtering (unfortunately, it's only applicable to returned fields of documents)

ElasticSearch API POST/PUT DeDupe

I am looking to use the Elastic Search RESTful API to send data to my ES instance. Here is some sample data:
[{"subject":"matt","predicate":"likes","object":"coffee","label":"1_10"}]
[{"subject":"james","predicate":"likes","object":"water","label":"1_10"}]
[{"subject":"leo","predicate":"likes","object":"liquor","label":"1_10"}]
[{"subject":"matt","predicate":"likes","object":"coffee","label":"1_10"}]
[{"subject":"matt","predicate":"likes","object":"coffee","label":"1_10"}]
My post call looks like:
"http://" + url + "/something/quads/
With the JSON payload.
I was looking at a put call and was trying the following:
"http://" + url + "/something/quads/_create
from this documentation: https://www.elastic.co/guide/en/elasticsearch/guide/current/create-doc.html
The problem is that it created the ID in ES as _create Is there something I am doing wrong?
If you use POST calls with URLs like /something/quads/ then ES will automatically generate IDs for your documents.
If, instead, you want to use PUT calls, you need to provide document IDs yourself in the URL /something/quads/123, /something/quads/456, etc.
In your second URL /something/quads/_create, you're missing the document ID. It should be /something/quads/123/_create. Check the docs you've linked to again and you'll see.
Also note that the difference between the two following commands
PUT /something/quads/123
PUT /something/quads/123/_create
is that the second will fail if a document with the ID 123 already exists. The first command, however, will always succeed and overwrite the document with ID 123 if one exists.

Importance of ? in ES query

how the query URL is formed in elastic search and why we have a ? as below.Does this ? have any importance.Sample =โ€ /kibana-int/dashboard/log-analytics-s?1431600189554
1431600189554 appears to be a timestamp (UNIX timestamp w/milliseconds), likely used to prevent caching of requests. It's not necessary for ElasticSearch (which will ignore it entirely), but it means the browser treats every request as a new URL and fetches new data.
The "?" states that the rest of the URL will be parameters. As an example it's often used with the parameter "pretty" in ElasticSearch, when you query a mapping (like http://localhost:8080//_mapping?pretty). The "pretty" argument forces the return to be formatted in a way that is eye-friendly.
As a side note, the "&" character is used to separate arguments. For exemple http://localhost:8080//?lastname=foo&firstname=bar

Can you do an elasticsearch geo_distance query (or a filter) as a uri request

I would like to run an elasticsearch query to find items within 10mi of a given point.
I know how to do it with a post, but I would like to use a get with everything in the uri.
I found the below example but it does not work.
http://localhost:9200/items/item/_search?{%22query%22:{%22filtered%22:{%22query%22:{%22match_all%22:{}},%22filter%22:{%22geo_distance%22:{%22distance%22:%220.1km%22,%22location%22:{%22lat%22:46.884106,%22lon%22:-71.377042}}}}}}
Any way to do this or am I stuck using a post?
The key is the source= parameter. Not to be confused with _source.
http://localhost:9200/items/item/_search?source={%22query%22:{%22filtered%22:{%22query%22:{%22match_all%22:{}},%22filter%22:{%22geo_distance%22:{%22distance%22:%220.1km%22,%22location%22:{%22lat%22:46.884106,%22lon%22:-71.377042}}}}}}
I had tried ?q= and a few other parameters listed on http://www.elasticsearch.org/guide/reference/api/search/uri-request/ with no luck (source is not listed).
I found http://www.elasticsearch.org/guide/reference/api/ and at the very bottom it says
request body in query string
For libraries that donโ€™t accept a request body for non-POST requests,
you can pass the request body as the source query string parameter
instead.
So structure your query/filter request, set it all on one line and send it into the source parameter.
Do not use the q= parameter with source= or it will conflict and break the query, however I tried size= and from= and they work with source just fine.

Resources