Modulo operator in Elasticsearch - elasticsearch

How can we write a basic condition to use modulo operator in Elasticsearch?
like
user.created modulo 2 = 0

Unfortunately the example from Andrei Stefan with Groovy syntax is not working on my instances.
Here's the official "expression" syntax you can use as filter that can maybe work more widely:
{
"script": {
"script": {
"source": "(doc['transactionAmount'].value % 2) == value",
"lang": "expression",
"params": {
"value": 0
}
}
}
}

I'm assuming you'd want to do this in a script in Elasticsearch. If so, it would be the same as the Groovy modulo operator:
{
"query": {
"filtered": {
"filter": {
"script": {
"script": "doc['user.created'].value % 2 == 0",
"lang": "groovy"
}
}
}
}
}

Related

ElasticSearch: Combining bool and script_score in a single query

I have an existing elastic bool query. I've added a dense vector field to the index and would like to search it all in one query. The compound query part of the Elastic docs seems to imply you can do this, but I can't make it work (I get a runtime error) and haven't been able to find any examples. Here's a simplified version of what I'm trying.
localQuery = {
'bool':
'should': [
{
"match_phrase": {
"field1": {
"query": query,
"boost": 10
}
}
},
{
"match_phrase": {
"field2": {
"query": query,
"boost": 6
}
}
},
{
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "cosineSimilarity(params.element_desc_vector,
'description_vec') + 1.0",
"params": {"element_desc_vector": queryList}
}
}
}
]
}
I'd appreciate any suggestions, pointers to examples or even a flat "no you can't do that".
Thanks
Howard
Trying to do the same, I eventually found you could access the score from within the script. So you could add the score returned in the "should" clause to that of the cosine similarity.
Also I put the bool clause inside the script_score and not vice-versa.
local_query = {
"script_score": {
"query": {
"bool": {
"should": [
{
"match_phrase": {
"field1": {
"query": query,
"boost": 10
}
}
},
{
"match_phrase": {
"field2": {
"query": query,
"boost": 6
}
}
}
]
}
},
"script": {
"source": "(cosineSimilarity(params.element_desc_vector, 'description_vec') + 1.0) + _score",
"params": {
"element_desc_vector": queryList
}
}
}
}

how to check for key exist in elastic-search painless parameters?

How to check for key exists in painless script map parameters.
In below query check a.toString() key exist in params
I've tried everything but didn't get it to work.
Please help me
mapping :
"id": {
"type": "long"
}
query:
{
"query":{
"bool":{
"filter":[
{
"script": {
"script": {
"lang": "painless",
"params": {
"29232":2541,
"minDistance": 0
},
"source": "def a=doc['id'].getValue();double distance=params[a.toString()]; return distance <= 1000 && distance >= params['minDistance']"
}
}
}
]
}
}
}
The params is just a Java Map object. So, the following checks if the key exists in the params and exits early with a false if it does not exist.
GET test/_search
{
"query":{
"bool":{
"filter":[
{
"script": {
"script": {
"lang": "painless",
"params": {
"29232":2541,
"minDistance": 0
},
"source": """
def a=doc['id'].getValue();
if (!params.containsKey(a.toString())) {
return false;
}
double distance=params[a.toString()];
return distance <= 1000 && distance >= params['minDistance']
"""
}
}
}
]
}
}
}

query by time difference beetween dates in elasticsearch

Say I have index like this:
{"id": "12345678", "start": 1541999620214, "end": 1541999620222 }
How do I query for documents with end-start > 10? and now-start < 60000
Simply like this:
{
"query": {
"script": {
"script": {
"source": "doc.end.value - doc.start.value < 10"
}
}
}
}
and
{
"query": {
"script": {
"script": {
"source": "System.currentTimeMillis() - doc.start.value < 60000"
}
}
}
}
As a performance optimization, though, you could store the end - start information inside your document at indexing time so you don't have to resort to scripting and your query would simply become:
{
"query": {
"range": {
"diff": {
"lt": 10
}
}
}
}

ElasticSearch Script Fields accessable in sort

Is it possible to access a script_field inside a sort?
I have
search_body["script_fields"] = {
"test": {
"script": {
"lang": "painless",
"inline": skill_rating_algorithm.replace("\n", ""),
"params" : {
"param1": {....}
}
}
}
}
And as sort:
search_body["sort"] = {
"_script": {
"type": "string",
"order": "desc",
"script": {
"lang": "painless",
"inline": "def c = params._fields['test']; return c;",
}
},
"number_of_years_of_experience": {"order": "desc"},
"value.raw": {"order": "asc"},
}
But this only return errors.
I could do a sorting script but i need the value returned in the document as a separate key.
My questions is:
Can i access the script_field "test" inside the sort? (More script_fields will come and i will need different ordering)

Is there are a way to filter by _score in elasticsearch?

Is there a way to do following post_filter query valid?
I've tried different ones: _score, score, doc.score, doc._score, _source.score etc. nothing is working.
I believe there should be some solution that allows you to filter in the post_filter by the score.
{
"body": {
"post_filter": {
"script": {
"script": "_score > 100 && _score < 200"
}
},
"query": {
"function_score": {
"functions": [{
"script_score": {
"script": "doc['price'].value * 100"
}
}]
}
}
},
"index": ["items"],
"type": []
}
Similar question on the ES group: http://elasticsearch-users.115913.n3.nabble.com/Filter-on-score-td4044742.html

Resources