Elasticsearch searching Java API - java

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());

Related

ElasticSearch get distinct all values of one field with Java High Level Client 7.x

I want to get all values of a field from elasticsearch using java high level client.
I'm using following code:
SearchRequest searchRequest = new SearchRequest(index);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery(field, "*"));
searchRequest.source(sourceBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
I want to get the values DISTINCT (like select distinct field from table), if it's possible.

In Java, how do you query the nested field in elasticsearch according to ID?

ScoreMode mode=ScoreMode.Total;
SearchRequestBuilder searchRequestBuilder =
client.prepareSearch()
.setIndices("commons")
.setTypes("webpage")
//.fields("id")
.setSize(10000)
.setQuery(QueryBuilders
.nestedQuery("imgSource",
QueryBuilders.boolQuery()
.must(
QueryBuilders.matchQuery(
"id","48ca52e0d733c5093c08aa4df0b073f7")
),mode));
this is my code,but result is null,So I don't know if I made a mistake...
As per java api for nested queries, Your code snippet should look something like below:
Suppose I want to query name field inside nested document child, then code snippet will be:
ScoreMode mode=ScoreMode.Total;
SearchRequestBuilder searchRequestBuilder =
client.prepareSearch()
.setIndices(INDEX_NAME)
.setTypes(TYPE_NAME)
.setQuery(QueryBuilders
.nestedQuery("child",
QueryBuilders.boolQuery()
.must(
QueryBuilders.matchQuery(
"child.name","CHILD_NAME")
),mode));
So, your match query is incorrect, it should be imgSource.id

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

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 aggregation 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 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).

Categories

Resources