Get all results of elasticsearch without pagination - elasticsearch

Elastic-search has from/size parameter and this size is 10 by default. How to get all the results without pagination?

first get count of results . you can get it by count api. the put n into following method of QueryBuilder.
CountResponse response = client.prepareCount("test")
.setQuery(termQuery("_type", "type1"))
.execute()
.actionGet();
then call
n=response.getCount();
you can use setSize(n).
for non java use like this curl request.
curl -XGET 'http://localhost:9200/twitter/tweet/_count' -d '
{
"query" : {
"term" : { "user" : "kimchy" }
}
}'
more on this can be found on this link.
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-count.html

Related

How to send json data elastic search get request url

Following works fine
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query" : {
"query_string" : {
"query" : "chicag*",
"fields" : ["name"],
"_name":"myqry"
}
}
}
'
How can I create a browser url for this GET request ? I URLEncoded the json and tried -
localhost:9200/_search?data=%7B%0A%20%20%22query%22%20%3A%20%7B%0A%20%20%20%20%22query_string%22%20%3A%20%7B%0A%20%20%20%20%20%20%22query%22%20%3A%20%22chicag%2A%22%2C%0A%20%20%20%20%20%20%22fields%22%20%20%3A%20%5B%22name%22%5D%2C%0A%20%20%20%20%20%20%22_name%22%3A%22Chcagoooo%22%0A%20%20%20%20%7D%0A%20%20%7D
but it did not work
It is definitely possible to send your DSL query as a query string parameter using the source parameter like this:
localhost:9200/_search?pretty&source={"query":{"query_string":{"query":"chicag*","fields":["name"],"_name":"myqry"}}}&source_content_type=application/json
I am using multi-get API _mget. In my case, I am using the following URL to get all the documents in the specified range.
Refer: https://www.elastic.co/guide/en/elasticsearch/reference/7.10/docs-multi-get.html#mget-ids
This example displays documents with id 49 and 50.
http://localhost:9200/my_index/_mget?pretty&source={ "ids" : [49, 50] }&source_content_type=application/json

Cannot get only number of hits in elastic search

Im using _msearch api to send multiple queries to elastic.
I only need to know how many hits generates each query.
What I understood, you can use the size parameter by setting it to "0" in order to only get the count. However, I still get results with all the found documents. Here is my query:
{"index":"myindex","type":"things","from":0,,"size":0}
{"query":{"bool":{"must":[{"match_all":{}}],"must_not":[],{"match":
{"firstSearch":true}}]}}}, "size" : 0}
{"index":"myindex","type":"things","from":0,,"size":0}
{"query":{"bool":{"must":[{"match_all":{}}],"must_not":[],{"match":
{"secondSearch":true}}]}}}, "size" : 0}
Im using curl to get the results, this way:
curl -H "Content-Type: application/x-ndjson" -XGET localhost:9200/_msearch?pretty=1 --data-binary "#requests"; echo
Setting size as zero signifies that you are asking Elasticsearch to return all the documents which satisfies the query.
You can let Elasticsearch know that you do not need the documents by sending "_source" as false.
Example:
{
"query": {},
"_source": false,
}
You can use
GET /indexname/type/_count?
{ "query":
{ "match_all": {} }
}
please read more document: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html

Elasticsearch Multi Get working through curl, but no results are returned through Java API

I am running elasticsearch 2.3.4, but the syntax does not seem to have changed in 5.x.
Multiget over curl is working just fine. Here is what my curl looks like:
curl 'localhost:9200/_mget' -d '{
"docs" : [
{
"_index" : "logs-2017-04-30",
"_id" : "e72927c2-751c-4b33-86de-44a494abf78f"
}
]
}'
And when I want to pull the "message" field off that response, I use this request:
curl 'localhost:9200/_mget' -d '{
"docs" : [
{
"_index" : "logs-2017-04-30",
"_id" : "e72927c2-751c-4b33-86de-44a494abf78f",
"fields" : ["message"]
}
]
}'
Both of the above queries return the log and information that I am looking for.
But when I try to translate it to Java like this:
MultiGetRequestBuilder request = client.prepareMultiGet();
request.add("logs-2017-04-30", null, "e72927c2-751c-4b33-86de-44a494abf78f");
MultiGetResponse mGetResponse = request.get();
for (MultiGetItemResponse itemResponse : mGetResponse.getResponses()) {
GetResponse response = itemResponse.getResponse();
logger.debug("Outputing object: " + ToStringBuilder.reflectionToString(response));
}
I appear to be getting null objects back. When I try to grab the message field off the null-looking GetResponse object, nothing is there:
GetField field = response.getField("message"); <--- returns null
What am I doing wrong? doing a rest call to elasticsearch proves the log exists, but my Java call is wrong somehow.
The documentation page for the Java multi get completely skips over the extra syntax required to retrieve data beyond the _source field. Just like the REST API, doing a multi get with the minimum information required to locate a log gets very limited information about it. In order to get specific fields from a log in a multi get call through the Java API, you must pass in a MultiGetRequest.Item to the builder. This item needs to have the fields you want specified in it before you execute the request.
Here is the code change (broken into multiple lines for clarity) that results in the fields I want being present when I make the query:
MultiGetRequestBuilder request = client.prepareMultiGet();
MultiGetRequest.Item item = new MultiGetRequest.Item("logs-2017-04-30", "null", "e72927c2-751c-4b33-86de-44a494abf78f");
item.fields("message");
request.add(item);
MultiGetResponse mGetResponse = request.get();
Now I can ask for the field I specified earlier:
GetField field = response.getField("message");

Elasticsearch _query vs _search

I have a question which is not quite clear for me when reading the documentation.
What exactly is the difference between the _search and the _query Endpoint?
Thanks a lot!
Matthias
The _search API endpoint 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.
curl -XGET 'http://localhost:9200/twitter/tweet,user/_search?q=user:kimchy'
curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '{
"query" : {
"term" : { "user" : "kimchy" }
}
}
'
The _query endpoint, is for delete by query only (I think it only has handlers for DELETE, not POST, or GET).
curl -XDELETE 'http://localhost:9200/twitter/tweet/_query?q=user:kimchy'
You can learn more here:
Elasticsearch Doco

Register and call query in ElasticSearch

Is it possible to register query (like the percolate process) and call them by name to execute them.
I am building an application that will let the user save search query associated with a label. I would like to save the query generated by the filter in ES.
If I save the query in an index, I have to call ES first to retrieve the query, extract the field containing the query and then call ES again to execute it. Can I do it in one call ?
The other solution is to register queries (labels with _percolator with an identifier of the user:
/_percolate/transaction/user1_label1
{
"userId": "user1",
"query":{
"term":{"field1":"foo" }
}
}
and when there is a new document use the percolator in a non indexing mode (filtered per userId) to retrieve which query match, and then update the document by adding a field "label":["user1_label1", "user1_label2"] and finaly index the document. SO the labelling is done at indexing time.
What do you think ?
Thanks in advance.
Try Filter Aliases.
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{
"add" : {
"index" : "the_real_index",
"alias" : "user1",
"filter" : { "term" : { "field1" : "foo" } }
}
}
]
}'

Resources