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();
Related
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.
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.
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()
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
I'm using Play framework 1.2.5 and Play-Morphia module.
I want to know if there's a way to update many objects at one Morphia query. I've found this example at https://github.com/greenlaw110/play-morphia/blob/master/documentation/manual/crud.textile, but it seems that I can't use "in" operation in norder to find all the objects which I hold in a list of their IDs.
I'm trying to update the paidInvoiceDocNum filed in each of the objects which their IDs are in the list "itemsIds". This is what I've tried so far:
String q = TransactionItem.find().field("id").in(itemsIds).toString();
TransactionItem.o().set("paidInvoiceDocNum", String.valueOf(docNumber)).update(q);
Without the .toString() it doesn't work either.
Any suggestions?
After long time of experimenting with Play-Morphia, I've found the way to do this update and here it is:
Datastore ds = TransactionItem.ds();
UpdateOperations<TransactionItem> op = ds.createUpdateOperations(TransactionItem.class).set("paidInvoiceDocNum", String.valueOf(docNumber));
Query<TransactionItem> q = (Query<TransactionItem>)TransactionItem.q().filter("id in", itemsIds).getMorphiaQuery();
ds.update(q, op);
Hope It will help...
Can you try this?
TransactionItem.o().set("paidInvoiceDocNum", docNumber).update("id in", itemsIds);
BTW, what's your morphia version. Keep in mind Play has close the updates to modules. Use this to get the latest morphia plugin version: https://gist.github.com/greenlaw110/2868365