elasticsearch | template query | java API - java

I was trying to implement the feature of template query. Refer to the last section of http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-template.html
I added a query template using sense. Now the need is through JAVA API of elasticsearch, I need to execute this query template and store the result in SearchResponse. However I am not able to find any API related to query Template. The only class file which is available is TemplateQueryBuilder. This class constructs the template query perfectly but I am not sure of which method to be called from Client in order to pass the object of TemplateQueryBuilder.
Help in this is appreciated.

Here is how to do it :
SearchRequestBuilder request = client;.prepareSearch();
request.setTemplateName(templateName);
request.setTemplateType(ScriptService.ScriptType.INDEXED);
request.setTemplateParams(templateParams);
SearchResponse response = request.get();
You just need to parse the response object then ..
refer to: http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/search.html#java-search-template

Note that with API version 2.X, as request.setTemplateX methods are deprecated, you should use a different approach.
Either you can use request.setTemplate(Template template) which is similar to the accepted answer, or you can go with the more generic QueryBuilders approach:
QueryBuilder qb = QueryBuilders.templateQuery(
"templateName",
ScriptService.ScriptType.FILE,
templateParams);
More to read: https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/java-specialized-queries.html#java-query-dsl-template-query

Related

What is equivalent of cts:element-query in MarkLogic Java API

I have a MarkLogic query written in XQuery and I would like to convert it to Java API using StructuredQueryBuilder. Unfortunately, I can't find Java equivalent for cts:element-query. Can you please show me how to implement it in Java?
The query that I want to convert:
cts:element-query(fn:QName("http://www.example.com/2009/pfi2","content"), cts:word-query("florists", ("case-insensitive","lang=en"), 4.5), ())
The StructuredQueryBuilder.containerQuery() method constructs a search:container-query in the Search API. On the enode, the REST API converts the search:container-query to cts:element-query() or cts:json-property-query() or cts:json-property-scope-query() as appropriate.
For more detail, see:
http://docs.marklogic.com/javadoc/client/com/marklogic/client/query/StructuredQueryBuilder.html#containerQuery-com.marklogic.client.query.StructuredQueryBuilder.ContainerIndex-com.marklogic.client.query.StructuredQueryDefinition-
http://docs.marklogic.com/guide/search-dev/structured-query#id_87231
The other way to provide the query in the Java API is to serialize the cts:element-query() as JSON or XML to learn the query structure and then use a DOM to construct the query and pass the query as a RawCtsQueryDefinition payload.
For that approach, see:
http://docs.marklogic.com/guide/java/searches#id_45762
http://docs.marklogic.com/javadoc/client/com/marklogic/client/query/RawCtsQueryDefinition.html
Hoping that helps,

Datastore query with IN operator

The new flexible environment datastore interface does not seem to support IN operation when running a query. I hope that I'm wrong, and if so, how can one use an IN operator in the new Java interface of Datastore?
A query like - WHERE color IN('RED', 'BLACK'), it is not supported by the Datastore (server side). Same is the case with OR operator (e.g. WHERE color='RED' OR color='BLACK'). Some client APIs have added this functionality by splitting the query into multiple and then merging the results from each query. The new google-cloud-java API does not support this yet. For now, you would have to run multiple queries for each value in the IN clause and merge the results.
Here’s an example from the documentation:
If you want to set more than one filter on a query, you must use CompositeFilter, which requires at least two filters.
Filter tooShortFilter = new FilterPredicate("height", FilterOperator.LESS_THAN, minHeight);
Filter tooTallFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN, maxHeight);
Filter heightOutOfRangeFilter = CompositeFilterOperator.or(tooShortFilter, tooTallFilter);
Query q = new Query("Person").setFilter(heightOutOfRangeFilter);
You can also use .and(). The code here is for Java 7. For Java 8 you can find a corresponding code in the documentation referenced above. I hope that helps.
Now to IN. While I have not tried it myself recently, the current documentation states that it can still be used as an operator. According to it, something like the code below should work:
Filter propertyFilter = new FilterPredicate("height", FilterOperator.IN, minHeights);
Query q = new Query("Person").setFilter(propertyFilter);
Alternatively, you could use Google GQL. It will allow you to write SQL-like syntax, in which you can use in(...).
I tried using the repository query methods, but I got an error informing that it is not supported.
Only solved for me using the #Query annotation;
Example:
#Query("select * from UserGroup where name IN #names")
List<Company> findAllByName(List<String> names);

How to build multi match query with type set to "phrase_prefix" using Elastic Java Api instead of pure REST

According to the documentation of multi match query (https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html) it is possible to search multi properties by prefix. All you must do is to set parameter "type" to "phrase_prefix". Unfortunately I cannot find that option in Elastic Java Api (https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.6/multimatch.html). I tried something like:
QueryBuilder builder = QueryBuilders
.multiMatchQuery("query", "property1", "property2");
but cannot find where to set "type" parameter. I know pure rest is the solution but I'm limited to java api.
I'm using version 5.2.2 of org.elasticsearch.client:transport.
You have to add .type(MatchQueryBuilder.Type.PHRASE_PREFIX) to your builder.
Example:
QueryBuilder builder = QueryBuilders
.multiMatchQuery("query", "property1", "property2")
.type(MatchQuery.Type.PHRASE_PREFIX)

SearchRequestBuilder: is there a way to ask to return all fields?

I am using the Java API to query elasticsearch. I am building the query this way:
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index)
.setQuery(query)
.addFieldDataField("location")
.addField("time")
.addField("name");
SearchResponse resp = searchRequestBuilder.get();
for (SearchHit hit : resp.getHits().getHits()) {
GeoPoint point = hit.field("location").getValue();
// ... etc
}
Is there a way to build the request and just get all the fields without specifying them explicitly? Seeing as the HTTP interface doesn't require you to do that.
I'm not sure which version of ES and java client you're on, but according to this javadoc: http://javadoc.kyubu.de/elasticsearch/HEAD/org/elasticsearch/action/search/SearchRequestBuilder.html#addFields(java.lang.String...), If you don't specify any fields, the full _source of the document will be returned.
Another thing I'll note is that the javadoc specifies the fields must be stored (unless you use addFieldDataField). Make sure they're not marked as ignored in your mapping.

Search twitter by language

In twitter by using lang operator you can search for retrieving tweets from a particular language: For example, the following search will return tweets only in english:
https://twitter.com/search?q=lang:en
Is there a way to achieve this using twitter api, especially using twitter4j. I know there is a language attribute for search but it also requires at least one keyword to make a search, I would like to search by a particular language without giving any keyword query. The following command in java driver returns the following error:
Query searchQuery = new Query() //
.count(resultsPerPage) //
.since(since)//
.lang("en");
400:The request was invalid. An accompanying error message will explain why. This is the status code will be returned during version 1.0 rate limiting(https://dev.twitter.com/pages/rate-limiting). In API v1.1, a request without authentication is considered invalid and you will get this response.
message - Query parameters are missing
code - 25
Cheers
You must pass the same do you use in the twitter search, your query will be like:
new Query("lang:en")
Using twitter4j,
Query query = new Query();
query.setLang("en");
query.setQuery(searchQuery);

Categories