elasticsearch skip completion suggester duplicates - elasticsearch

My elasticsearch current version is 6.0.1.
I'm using a completion suggester on my "suggest" field as follow:
GET my_index/_search
{
"suggest": {
"tag-suggest" : {
"prefix" : "black",
"completion" : {
"field" : "suggest",
"size" : 10,
"fuzzy" : {
"fuzziness" : 1
}
}
}
}
}
I'd like to skip duplicates in order to only retrieve unique suggestions.
According to elasticsearch documentation (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html) I can achieve that by turning the option "skip_duplicates" to true:
GET my_index/_search
{
"suggest": {
"tag-suggest" : {
"prefix" : "black",
"completion" : {
"field" : "suggest",
"skip_duplicates": true,
"size" : 10,
"fuzzy" : {
"fuzziness" : 1
}
}
}
}
}
Unfortunately I'm getting the following error:
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "[completion] unknown field [skip_duplicates], parser not found"
}
],
"type": "illegal_argument_exception",
"reason": "[completion] unknown field [skip_duplicates], parser not found"
},
"status": 400
}

Unfortunatelly skip_duplicates is not available in your version.
Please take a look here: https://www.elastic.co/guide/en/elasticsearch/reference/6.0/search-suggesters-completion.html
It was introduced in version 6.1: https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-suggesters-completion.html

Related

Elastic Search shows "Unknown key for a START_OBJECT" exception

I am sending the following query to elastic search in order to get data which are within the range of the values between the from and to:
{
"range" : {
"variables.value.long" : {
"from" : -1.0E19,
"to" : 9.1E18,
"include_lower" : true,
"include_upper" : true,
"boost" : 1.0
}.
}
}
Despite that elastic search throws the following error:
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "Unknown key for a START_OBJECT in [range].",
"line": 2,
"col": 13
}
],
"type": "parsing_exception",
"reason": "Unknown key for a START_OBJECT in [range].",
"line": 2,
"col": 13
},
"status": 400
}
Does anybody know what this error means and why I am getting it?
There is some lack of context here like your mappings or the full query you are running, but this is how a range query should look for your document.
Create index
PUT test_andromachiii
{
"mappings": {
"properties": {
"variables": {
"properties": {
"values": {
"properties": {
"long": {
"type": "double"
}
}
}
}
}
}
}
}
Index document
POST test_andromachiii/_doc
{
"variables": {
"values": {
"long": 9.1E18
}
}
}
Run Query
POST test_andromachiii/_search
{
"query": {
"range": {
"variables.values.long": {
"lte": -1.0E19,
"gte": 9.1E18,
"boost": 1
}
}
}
}
Note lte means lower or equals to, gte greater or equals to.
Response
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test_andromachiii",
"_type" : "_doc",
"_id" : "gtGj73cBbr4pOF0Is9my",
"_score" : 1.0,
"_source" : {
"variables" : {
"values" : {
"long" : 9.1E18
}
}
}
}
]
}
}
It looks like you're using version <0.90.4. If that's the case, simply wrap your range in a parent query object:
{
"query":{
"range":{
"variables.value.long":{
"from":-1.0E19,
"to":9.1E18,
"include_lower":true,
"include_upper":true,
"boost":1.0
}
}
}
}
If you're using any newer version than that, note that:
The from, to, include_lower and include_upper parameters have been deprecated in 0.90.4 in favour of gt, gte, lt, and lte.
This error is saying (somewhat cryptically) that you have a key range with an Object value, in a place where that key isn't recognised.
The specific cause here is that your range needs to be part of a higher query key such as (i.e.) the bool query, not part of the main.
Credit: https://discuss.elastic.co/t/unknown-key-for-a-start-object-in-should/140008/3

unknown query [filtered] when doing search against ES

I am new to ES, and I am using ES 7.10.1, I have following simple search request:
GET /megacorp/_doc/_search
{
"query":{
"filtered":{
"filter":{
"range":{
"age":{
"gt":30
}
}
},
"query":{
"match":{
"last_name":"smith"
}
}
}
}
}
When I run the above query(using query and filter) in the Kibana Dev Tools, an exception occurs as follows, I would ask how to fix this,thank.
{
"error" : {
"root_cause" : [
{
"type" : "parsing_exception",
"reason" : "unknown query [filtered]",
"line" : 3,
"col" : 14
}
],
"type" : "parsing_exception",
"reason" : "unknown query [filtered]",
"line" : 3,
"col" : 14,
"caused_by" : {
"type" : "named_object_not_found_exception",
"reason" : "[3:14] unknown field [filtered]"
}
},
"status" : 400
}
The filtered query has been deprecated. You should now use the boolean query. Modify your search query as -
{
"query": {
"bool": {
"must": {
"match": {
"last_name": "smith"
}
},
"filter": {
"range": {
"age": {
"gt": 30
}
}
}
}
}
}

For an elastic search index, how to get the documents where array field has length greater than 0?

In elastic search index, how to get the documents where array field has length greater than 0?
I tried following multiple syntaxes but didn't get any breakthrough. I got same error in all of the syntaxes.
GET http://{{host}}:{{elasticSearchPort}}/student_details/_search
Syntax 1:
{
"query": {
"bool": {
"filter": {
"script": {
"script": {
"source": "doc['enrolledCourses'].values.length > 0",
"lang": "painless"
}
}
}
}
}
}
Error:
"caused_by": {
"type": "illegal_argument_exception",
"reason": "No field found for [enrolledCourses] in mapping with types []"
}
Syntax 2:
{
"query": {
"bool": {
"filter": {
"script": {
"script": {
"source": "doc['enrolledCourses'].values.size() > 0",
"lang": "painless"
}
}
}
}
}
}
Error:
"caused_by": {
"type": "illegal_argument_exception",
"reason": "No field found for [enrolledCourses] in mapping with types []"
}
Syntax 3:
{
"query": {
"bool": {
"filter" : {
"script" : {
"script" : "doc['enrolledCourses'].values.size() > 0"
}
}
}
}
}
Error:
"caused_by": {
"type": "illegal_argument_exception",
"reason": "No field found for [enrolledCourses] in mapping with types []"
}
Syntax 4:
{
"query": {
"bool": {
"filter" : {
"script" : {
"script" : "doc['enrolledCourses'].values.length > 0"
}
}
}
}
}
Error:
"caused_by": {
"type": "illegal_argument_exception",
"reason": "No field found for [enrolledCourses] in mapping with types []"
}
Please help me in solving this.
I don't know what version of elastic you run, then all my test I'd running on latest 7.9.0 version of Elasticsearch.
I will use painless script for scripting.
I put to documents to index test:
PUT test/_doc/1
{
"name": "Vasia",
"enrolledCourses" : ["test1", "test2"]
}
PUT test/_doc/2
{
"name": "Petya"
}
How you can see one document contains enrolledCourses field and second not.
In painless you don't need use values field and you can take length directly, this is according to painless documentation. Then I skip using values operator in my script:
GET test/_search
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": {
"source": "doc['enrolledCourses'].length > 0",
"lang": "painless"
}
}
}
]
}
}
}
After running I'd received 2 different errors:
{
"type" : "script_exception",
"reason" : "runtime error",
"script_stack" : [
"org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:757)",
"org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:116)",
"org.elasticsearch.index.query.QueryShardContext.lambda$lookup$0(QueryShardContext.java:331)",
"org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:97)",
"org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:94)",
"java.base/java.security.AccessController.doPrivileged(AccessController.java:312)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:94)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
"doc['enrolledCourses'].length > 0",
" ^---- HERE"
]
}
and
{
"type" : "illegal_argument_exception",
"reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [enrolledCourses] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
}
Both of errors is pretty clear. First for document where field doesn't exists and second because Elasticsearch indexed string array field as default mapping type text.
Both of cases is very easy to fix by mapping enrolledCourses field as keyword.
In first case mapping will always provide empty field and in second keyword word be allow to run fielddata property.
PUT test
{
"settings": {
"number_of_replicas": 0
},
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"enrolledCourses": {
"type": "keyword"
}
}
}
}
Now I will receive right answer for query:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.0,
"_source" : {
"name" : "Vasia",
"enrolledCourses" : [
"test1",
"test2"
]
}
}
]
}
}

Error message - Unable to filter min_docs_count

EDIT:
Answer below
getting always following error when trying any aggregated query.
Tried googling and different aggregation constructs.
Elasticsearch API Hosted as "Logs Data Platform" by OVH.
Request
{
"aggs" : {
"servers" : {
"filter" : { "term": { "servertype": "1" } },
"aggs" : {
"avg_price" : { "avg" : { "field" : "serveramount" } }
}
}
}
}
Error response
{
"error": {
"root_cause": [
{
"type": "parse_exception",
"reason": "Unable to filter min_docs_count"
}
],
"type": "parse_exception",
"reason": "Unable to filter min_docs_count",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "[size] parameter cannot be negative, found [-1]"
}
},
"status": 400
}
Stupid me ... size=0 was missing in the query parameter.

ElasticSearch => How to updates with a partial document using update_by_query

I want to update the data in my index whose cname is wang.
My index code is as follows:
PUT index_c
{
"mappings": {
"_doc" : {
"properties" : {
"cid" : {
"type" : "keyword"
},
"cname" : {
"type" : "keyword"
},
"cage" : {
"type" : "short"
},
"chome" : {
"type" : "text"
}
}
}
}
}
And my update request is as follows:
POST index_c/_update_by_query
{
"query" : {
"match": {
"cname": "wang"
}
},
"doc" : {
"cage" : "100",
"chome" : "china"
}
}
But I got an error like this:
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "Unknown key for a START_OBJECT in [doc].",
"line": 1,
"col": 43
}
],
"type": "parsing_exception",
"reason": "Unknown key for a START_OBJECT in [doc].",
"line": 1,
"col": 43
},
"status": 400
}
So I want to know how to implement this when using "update_by_query"
I think this will work for you just replace the doc part with script. if inline shows deprecated for you then just use source instead
POST index_c/_update_by_query
{
"query" : {
"match": {
"cname": "wang"
}
},
"script" : {
"inline" : "ctx._source.cage='100'; ctx._source.chome= 'china';",
"lang" : "painless"
}
}

Resources