Reputation: 313
I am new to elastic search and wanted to solve an use case with below problem
I have some documents as below format
{
"member_id":"number"
"e_id":"number",
"c_id":"number",
"salary":"float"
}
In my Java application, I will get a String input and need to find the matching document's member_id. The algorithm to find the document is as below
STEP 1: Take input string as "keyInput".
STEP 2: Concat "e_id","c_id" and ceil("salary") as "concatedData"
STEP 3: if "concatedData" equals to "keyInput" THEN return the "member_id" of the matching document.
STEP 2 need to be performed in Elasticsearch.
Can anyone help to figure out if this kind of actions can be performed on ES or not, if Yes then how?
Thanks in Advance.
Upvotes: 3
Views: 16755
Reputation: 7874
There are two approaches to solve this.
Suggested Mapping:
"mappings": {
"_doc": {
"properties": {
"member_id": {
"type": "integer"
},
"e_id": {
"type": "integer"
},
"c_id": {
"type": "integer"
},
"salary": {
"type": "float"
}
}
}
}
Query:
{
"query": {
"bool": {
"filter": {
"script": {
"script": {
"source": "String.valueOf(doc['e_id'].value) + doc['c_id'].value + Math.ceil(doc['salary'].value).intValue() == params.inp",
"lang": "painless",
"params": {
"inp": "12345678"
}
}
}
}
}
}
}
Note: Scripting should be avoided as it cause performance hit. I would strongly recommend the second approach below.
For this all you need to do is add a new field with datatype as keyword
to store the concatenated value. Then use term query
to filter the documents accordingly.
Upvotes: 4