Elasticsearch aggregation using Java api - java

Hi I am trying to do query on elastic search by following the sql query and I want to implement same logic using Java API
select dttime, avg(cpu) from table cpustats where server="X" and dttime="Y" group by dttime,cpu
Now I have the following Java code but it does not return expected output
SearchResponse response = client.prepareSearch("cpuindex")
.setTypes("cputype")
.setQuery(QueryBuilders.matchAllQuery())
.addAggregation(AggregationBuilders.terms("cpu_agg")
.field("cpu").size(100))
.execute().actionGet();
Please guide I am new to Elastic search. Thanks in advance.

I think this will help.
SearchResponse response=
client.prepareSearch("your_index_name_here").setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.andFilter(
FilterBuilders.termFilter("server","x"),
FilterBuilders.termFilter("dt_time","x")
))).addAggregation(
AggregationBuilders.terms("dt_timeaggs").field("dt_time").size(100).subAggregation(
AggregationBuilders.terms("cpu_aggs").field("cpu").size(100)
)
).setSize(0).get();
please verify.

since elastic search 2.3, FilterBuilders class has been removed from JavaAPI. you can use
QueryBuilder qb = QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("_all", "JPMORGAN"))
.must(QueryBuilders.matchQuery(field, value)) ;
instead, and set it to
.setQuery(qb).

Related

Elasticsearch using Java api

Hi I am trying to do query on elastic search by following the sql query and I want to implement same logic using Java API
select * from log , web where l.loghost = w.webhost and #datetime between '2016-05-20' AND '2016-05-25'
log and web are different types, and indices are set to logstash-log-* and logstash-web*, #timestamp format looks like "2016-05-20T17:14:01.037Z"
Now I have the following Java code but i don't know how to set between two dates ,so it does not return expected output
SearchResponse response = client.prepareSearch("logstash-log-*","logstash-web-*")
.setTypes("log","web")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setFetchSource(new String[]{"*"}, null)
.setQuery(QueryBuilders.queryStringQuery("1.2.3.4").field("*_host"))// Query
.execute()
.actionGet();
Please guide I am new to Elastic search. Thanks in advance.
You need to combine a range query with your query_string query inside a bool/filter query:
QueryStringQueryBuilder qs = QueryBuilders.queryStringQuery("1.2.3.4").field("*_host");
RangeQueryBuilder range = QueryBuilders.rangeQuery("#timestamp")
.gte("2016-05-20T00:00:00.000Z")
.lte("2016-05-25T00:00:00.000Z");
and then
...
.setQuery(QueryBuilders.boolQuery().filter(qs).filter(range))
...

Get Aggregation of Mutliple Fileds in Elastic search

I am querying ElasticSearch using Java API. Now i am able to query aggregation in one field. Its code given below.
SearchResponse res = client.prepareSearch("myindex").addAggregation(AggregationBuilders.terms("aggs").field("item_type"))
.setQuery(QueryBuilders.queryStringQuery("item_name:dell"))
.setSize(10).execute().actionGet();
Here i need to add one more aggregation field along with item_type like brand etc.
How do i add more fields along with item_type.How do i do that with java API.
Add another aggregation at top level
SearchResponse res = client.prepareSearch("myindex").addAggregation(AggregationBuilders.terms("aggs").field("item_type")).addAggregation(AggregationBuilders.terms("aggs2).field("item_type2))
.setQuery(QueryBuilders.queryStringQuery("item_name:dell"))
.setSize(10).execute().actionGet();

What is the alternative to deprecated SearchType.SCAN in ElasticSearch >2.1?

ElasticSearch documentation mentions that the SCAN search type is deprecated, and users are advised to just specify a sort order by "_doc".
We switched to ES 2.2, and the old code still works:
SearchResponse scrollResp = esClient.prepareSearch(indexName)
.setSearchType(SearchType.SCAN)
.setTypes("File")
.setScroll(new TimeValue(ES_SCROLL_TIMEOUT_MILLIS))
.setQuery(query) .setSize(ES_BATCH_SIZE).execute().actionGet();
where 'query' is a boolean query.
however trying this for the same query:
SearchResponse scrollResp = esClient.prepareSearch(indexName)
.setSearchType(SearchType.DEFAULT)
.addSort("_doc", SortOrder.ASC)
.setTypes("File")
.setScroll(new TimeValue(ES_SCROLL_TIMEOUT_MILLIS))
.setQuery(query) .setSize(ES_BATCH_SIZE).execute().actionGet();
doesn't get any results.
As a workaround, the deprecated code still works, but I am curious to know how it should be done with the updated API.

Elasticsearch searching Java API

I have got a working query for ElasticSearch, but I have problems to execute the same query with the Java API of ElasticSearch.
How can I express the query below with the Java API of ElasticSearch?
http://localhost:9200/mongoindex/files/_search?q=anyword&fields=file.file
this type of query uses a query_string query. The java code should look like this:
SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client);
searchRequestBuilder.setIndices("mongoindex");
searchRequestBuilder.setTypes("files");
QueryStringQueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder("anyword");
queryStringQueryBuilder.field("file.file");
searchRequestBuilder.setQuery(queryStringQueryBuilder);
SearchResponse response = searchRequestBuilder.execute().actionGet();
Thank you for your responses!
Think the following would be better)
SearchRequestBuilder searchRequestBuilder = client.prepareSearch()
.setIndices("mongoindex")
.setTypes("files")
.setQuery(QueryBuilders.queryString("anyword"))
.addField("file.file");
SearchResponse response = searchRequestBuilder.execute().actionGet();
System.out.println(response.toString());

Elasticsearch java API fuzzy search with prefix_length

I hope to perform this query using elastic search using Java API. For the fuzzy query, how do I get the "prefix_length" argument?
Query:
{"query":{"bool":{"should":[
{"fuzzy":{"object.name":{"value":"appl", "max_expansions":"1", "prefix_length" : 3}}},
{"prefix":{"object.name":"appl"}},
{"term":{"object.name":"appl"}}
]}}}
Java API:
QueryBuilder result = QueryBuilders.boolQuery()
.minimumNumberShouldMatch(1)
.should(QueryBuilders.fuzzyQuery(Company.FIELD_NAME, query)) ...
The documentation here doesn't explain how to get the "prefix_length" argument. Can someone explain? Thanks!
Aha. Can add like this:
should(QueryBuilders.fuzzyQuery(Company.FIELD_NAME, query).maxExpansions(1).prefixLength(query.length() - 1)

Categories

Resources