elasticsearch 5.x - Java Api - Highlighting - java

How do i implement highlighting with the new 5.x Version of elasticsearch Java Api?
This is my queryBuilder:
SearchRequestBuilder searchRequestBuilder = this.client.prepareSearch...
In previous versions we could simply add a field to be highlighted:
searchRequestBuilder.addHighlightedField('name');
But this is not possible anymore.
Can anyone provide a quick example?

You have to use HighlightBuilder under package org.elasticsearch.search.fetch.subphase.highlight and pass it to SearchRequestBuilder's highlight(HighlightBuilder highlightBuilder) method

Related

ElasticSearch: Specifying types in bulk requests is deprecated

I am getting a warning:
"[types removal] Specifying types in bulk requests is deprecated."]
What do I do wrong? This is my code:
BulkRequest request = new BulkRequest();
for(Item item : items) {
IndexRequest indexRequest = new IndexRequest(INDEX_NAME, DOC_TYPE, item.getIdentifier());
indexRequest
.opType(DocWriteRequest.OpType.INDEX) // Index the source. If there an existing document with the id, it will be replaced.
.source(JsonUtility.toJson(item), XContentType.JSON);
request.add(indexRequest);
}
elastic.bulk(request, RequestOptions.DEFAULT);
The mapping type was removed in Elasticsearch 8 and is deprecated in Elasticsearch 7.
No Elasticsearch version is mentioned in your question, but you can read more about the schedule for removal of mapping types, and react accordingly.
I think you are working with a 7.X version and the problem is you create the IndexRequest are constructing the URL of the POST method to search in ElasticSearch, something close to:
http://localhost:9200/INDEX_NAME_identifier/DOC_TYPE/_search
Where identifier is the attribute used to discriminate in the search. In ElasticSearch 7 specifying types in search requests were deprecated and the URL should be something close to:
http://localhost:9200/INDEX_NAME_identifier/_search
Although it is hard to know because you don't specify the versions, I think the elasticsearch library in your code is older than 7.X, if you updated it to 7 probably the DOC_TYPE param disappears in the constructor.

Fuzzy suggestions in elasticsearch 6

I'm in the process of upgrading to elasticsearch 6.0 in my java webapp.
Before I was using CompletionSuggestionFuzzyBuilder to build suggestions but with the newest version this class is gone. I've found that I can create a MaMatchQueryBuilder that will use fuzziness to get results :
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("user", "kimchy");
matchQueryBuilder.fuzziness(Fuzziness.AUTO);
matchQueryBuilder.prefixLength(3);
matchQueryBuilder.maxExpansions(10);
but I can't use it with suggestions. So I'm looking for a way to use fuzzy suggestions.
I think I've found the way to do that. Here is a solution :
SearchResponse suggestRequestBuilder = elasticClient.prepareSearch(index)
.suggest(new SuggestBuilder()
.addSuggestion(
"suggestionsFuzzy",
SuggestBuilders.completionSuggestion("myField")
.prefix(suggestRequest, Fuzziness.AUTO).size(10)
)
).get();

Parsing_Exception [match] query does not support [auto_generate_synonyms_phrase_query]

Below given is my code:
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("field", "value to search"));
Search search = new Search.Builder(searchSourceBuilder.toString()).build();
JestResult result = jestc.execute(search);
While running this code it gives exception:
{"error":{"root_cause":[{"type":"parsing_exception","reason":"[match] query does not support [auto_generate_synonyms_phrase_query]","line":1,"col":213}],"type":"parsing_exception","reason":"[match] query does not support [auto_generate_synonyms_phrase_query]","line":1,"col":213},"status":400}
Need help to resolve this issue....
Tried match_all query and it worked. No idea why this is failing.
I am using "org.elasticsearch" and "org.elasticsearch.client" of 6.1.1 version and jest version is 5.3.3. Not sure if it matters.
Thanks in advance
Found the problem. I was using the 6.1.1 API version, but my elastic nodes were on 5.x which doesn't support the auto_generate_synonyms_phrase_query parameter.
I moved "org.elasticsearch" and "org.elasticsearch.client" to version 5.6.0 and this fixed it for me. There are certain methods such as QueryBuilders.matchQuery(...) in the 6.1.1 client that add the new parameter and hence break backwards compatibility. The elasticsearch nodes where I am performing the query are on 5.x so they don't understand this new parameter.

Elasticsearch Java API from 2.x to 5.x issues

I've updated to elasticsearch java library version 5.2.0.
In 2.x,
I was using SearchRequestBuilder.addField() in order to add a field to the search request. Nevertheless, It seems to be replaced. I've written the available methods intellisense is showing me. Which of them do I need to pick?
addDocValueField
addFieldDataField
addScriptField
addStoredField
storedFields
fields
SearchRequestBuilder.setNoFields is also removed. Which would be the alternative?
Currently, I'm calling scripts from Java using this code. Is there any more elegant way to call it in 5.x Java API?
Code:
return AggregationBuilders
.terms(this.getName())
.field(this.getName())
.script(new Script(
ScriptType.FILE,
"painless",
"year",
ImmutableMap.of("field", this.getName())
)
);
As you can see I setting field as script parameter. Nevertheless, I don't quite understand how to get it from script code.
Thanks.
When in doubt, go to the source
use setFetchSource(String[] includes, String[] excludes) instead
use setFetchSource(false) instead
if you need to execute this script for each document, you can use addScriptField()

Is there a way to select WorkItemLinks with the TFS Java API?

I'd like to use the TFS Java API to run WIQL queries on the workitemlinks. I know how to fetch WorkItems via the WorkItemClient, but it seems to be missing the functionality to fetch WorkItemLinks.
On the other hand, client.supportsLinkQueries() returns true. So: Is there a way to run workitemlinks queries with the TFS Java API?
After some more digging around, I found that you can create a link query and run it like this:
WorkItemLinkInfo[] infos = client.createQuery("select * from workitemlinks").runLinkQuery()
Seems like they forgot to add the convenience method to the WorkItemClient.

Categories