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

Related

Springboot + Hibernate with ElasticSearch : Getting no results

I want to enable search for a bunch of fields for one of my entities. Therefore, I added hibernate search to my spring boot project. When I load data into the database, I can see that Elasticsearch contains that data as expected in the index running
curl localhost:9200/myindex/_search?pretty
I can run queries like
curl localhost:9200/myindex/_search?pretty&q=name:test
and receive the expected results.
I would like to give consumers of my API the option to run arbitrary queries like "name:test" against the index, so that
curl "localhost:8086/myentity/search/querySearch?query=name:test"
would return the same results as before in the direct query.
Here's what I am trying but whatever I do, I get 0 results:
public List<MyEntity> querySearch(String queryString) {
QueryParser queryParser = new MultiFieldQueryParser(ALL_FIELDS, new SimpleAnalyzer());
queryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
org.apache.lucene.search.Query query = queryParser.parse(QueryParser.escape(queryString));
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(this.entityManager);
javax.persistence.Query persistenceQuery =
fullTextEntityManager.createFullTextQuery(query, MyEntity.class);
return persistenceQuery.getResultList();
}
By calling QueryParser.escape(queryString), you remove the meaning of operators such as :. So if the user enters name:test, you will end up looking for documents that contain name:test (literally), instead of looking for documents whose name field contains test.
Remove that escape and everything should work as you want.
By the way, you're essentially using Lucene to parse a query that will then be sent to Elasticsearch. An easier solution would be to send the query to Elasticsearch directly, especially if you do not need to prevent users from accessing some fields.
public List<Training> querySearch(String queryString) {
FullTextEntityManager fullTextEm = Search.getFullTextEntityManager(this.entityManager);
QueryDescriptor query = ElasticsearchQueries.fromQueryString(queryString);
javax.persistence.Query persistenceQuery = fullTextEm.createFullTextQuery(query, Training.class);
return persistenceQuery.getResultList();
}
See https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#_queries

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

AEM querybuilder class return

I am attempting to use AEM's querybuilder API to do a generic search. The problem I am currently having is creating pagination with the querybuilder. While using the ResultPage class in AEM with SearchResult to getNextPage(). I am getting the result
com.day.cq.search.impl.result.ResultPageImpl#541bd4bf. How would I convert this into a URL? I already am using the offset and result total with the querybuilder but cannot find any more documentation to put me in the right direction.
queryBuilder=resource.getResourceResolver().adaptTo(QueryBuilder.class);
//creating query based on the Query Description
Query query=queryBuilder.createQuery(PredicateGroup.create(map),session);
//Getting and storing the Results
List Pages1 = searchRes.getResultPages();
ResultPage nextpage = searchRes.getNextPage();
ResultPage lastpage = searchRes.getPreviousPage();
for (Hit hit:searchRes.getHits()){
String path1=hit.getPath();
String title1=hit.getTitle();
String excerpt1=hit.getExcerpt();
You can't. The ResultPage objects have information about your search results that you got from the QueryBuilder. You can use the information to build your own pagination and search result page(s).
As the SearchResult object contains only the results from the current page, you can use the ResultPage to easily get index and start of all the other pages that you could request from the QueryBuilder, based on the settings you used for your query, for example:
query.setStart(start);
query.setHitsPerPage(hitsPerPage);
But there is no connection to the frontend...

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

How to get facet value for all fields using Java API

I'm new to Elasticsearch and tried to query some sample documents. I issued the following query using the Java API. This query fetched me the correct result. It returned the names of all categories. Now I want the count of all names of a category. Could you explain me how to do that? I'm sorry for my bad English.
SearchResponse sr = client.prepareSearch()
.addField("Category")
.setQuery(QueryBuilders.matchAllQuery())
.addFacet(FacetBuilders.termsFacet("f")
.field("Category"))
.execute()
.actionGet();
Look at the Count API to count your results if you not want to get the result set (matched documents) but only the count.
If you want to get the result set, you get the result size in the response for every filter or query request too.

Categories

Resources