Check value exists in params array using ElasticSearch painless script
I am new to ElasticSearch. I am working on a project where I have to search listings which matches the input http://foo.bar/search?listing=123,456
. I have built an array which key and value are the same and representing the listing ids. I have tried to run the following example but it failed near contains. The script is built for painless language. However, I always get the runtime error.
I cannot get this to work, what could I be doing wrong?
'function_score' => [
'query' => $query,
'score_mode' => 'sum',
'functions' => [[
'script_score' => [
'script' => [
'params' => ['listing' => [123 => 123, 456 => 456]],
'source' => "(params.listing.contains(doc['id'].value) ? Math.pow(3, 3) : 0)",
],
],
]],
],
php elasticsearch elasticsearch-painless
add a comment |
I am new to ElasticSearch. I am working on a project where I have to search listings which matches the input http://foo.bar/search?listing=123,456
. I have built an array which key and value are the same and representing the listing ids. I have tried to run the following example but it failed near contains. The script is built for painless language. However, I always get the runtime error.
I cannot get this to work, what could I be doing wrong?
'function_score' => [
'query' => $query,
'score_mode' => 'sum',
'functions' => [[
'script_score' => [
'script' => [
'params' => ['listing' => [123 => 123, 456 => 456]],
'source' => "(params.listing.contains(doc['id'].value) ? Math.pow(3, 3) : 0)",
],
],
]],
],
php elasticsearch elasticsearch-painless
add a comment |
I am new to ElasticSearch. I am working on a project where I have to search listings which matches the input http://foo.bar/search?listing=123,456
. I have built an array which key and value are the same and representing the listing ids. I have tried to run the following example but it failed near contains. The script is built for painless language. However, I always get the runtime error.
I cannot get this to work, what could I be doing wrong?
'function_score' => [
'query' => $query,
'score_mode' => 'sum',
'functions' => [[
'script_score' => [
'script' => [
'params' => ['listing' => [123 => 123, 456 => 456]],
'source' => "(params.listing.contains(doc['id'].value) ? Math.pow(3, 3) : 0)",
],
],
]],
],
php elasticsearch elasticsearch-painless
I am new to ElasticSearch. I am working on a project where I have to search listings which matches the input http://foo.bar/search?listing=123,456
. I have built an array which key and value are the same and representing the listing ids. I have tried to run the following example but it failed near contains. The script is built for painless language. However, I always get the runtime error.
I cannot get this to work, what could I be doing wrong?
'function_score' => [
'query' => $query,
'score_mode' => 'sum',
'functions' => [[
'script_score' => [
'script' => [
'params' => ['listing' => [123 => 123, 456 => 456]],
'source' => "(params.listing.contains(doc['id'].value) ? Math.pow(3, 3) : 0)",
],
],
]],
],
php elasticsearch elasticsearch-painless
php elasticsearch elasticsearch-painless
asked Nov 26 '18 at 6:54
CEJCEJ
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can make use of the below query. You can check the script
part as how what you need has been implemented.
POST <your_index_name>/_search
{
"query": {
"function_score": {
"score_mode": "sum",
"query": {
"match_all": {}
},
"functions": [
{
"script_score": {
"script": {
"source": """
boolean allValues = false;
for(int i=0; i<params.value.size(); i++){
if(doc['id'].value.contains(params.value.get(i))){
allValues = true;
}else{
allValues = false;
break;
}
}
if(allValues){
return Math.pow(3,3)
} else {
return 0;
}
""",
"params":{
"value": ["123","456"]
}
}
}
}
]
}
}
}
What the query does is, it checks to see if all the values present in params i.e 123 & 456
is present in the id
field, if it does, it would go ahead and calculate score accordingly.
Not that id
field is a keyword
when I tested the query at my end.
You can use the query accordingly in your php script. Let me know if this helps!
Didn't work, gives me compile error. Whats inside contain in my case is a collection and not an object.
– CEJ
Nov 27 '18 at 5:34
@CEJ I know about that and looking at your script using a collection doesn't make sense if all you want is to verify if url contains a set of values. Wouldn't array be better off with it? And also did you try to run the above query from Kibana or Postman? I want to make sure that the above query runs in your machine using one of rest client tools. Also you can change fromsource
toinline
and give it a try if you end up with any compilation error mentioning aboutsource
– Kamal
Nov 27 '18 at 6:49
Hey @CEJ did the updated query help?
– Kamal
Dec 10 '18 at 14:46
it did not work, is there a way to stick with what I put using source
– CEJ
Dec 11 '18 at 22:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476036%2fcheck-value-exists-in-params-array-using-elasticsearch-painless-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can make use of the below query. You can check the script
part as how what you need has been implemented.
POST <your_index_name>/_search
{
"query": {
"function_score": {
"score_mode": "sum",
"query": {
"match_all": {}
},
"functions": [
{
"script_score": {
"script": {
"source": """
boolean allValues = false;
for(int i=0; i<params.value.size(); i++){
if(doc['id'].value.contains(params.value.get(i))){
allValues = true;
}else{
allValues = false;
break;
}
}
if(allValues){
return Math.pow(3,3)
} else {
return 0;
}
""",
"params":{
"value": ["123","456"]
}
}
}
}
]
}
}
}
What the query does is, it checks to see if all the values present in params i.e 123 & 456
is present in the id
field, if it does, it would go ahead and calculate score accordingly.
Not that id
field is a keyword
when I tested the query at my end.
You can use the query accordingly in your php script. Let me know if this helps!
Didn't work, gives me compile error. Whats inside contain in my case is a collection and not an object.
– CEJ
Nov 27 '18 at 5:34
@CEJ I know about that and looking at your script using a collection doesn't make sense if all you want is to verify if url contains a set of values. Wouldn't array be better off with it? And also did you try to run the above query from Kibana or Postman? I want to make sure that the above query runs in your machine using one of rest client tools. Also you can change fromsource
toinline
and give it a try if you end up with any compilation error mentioning aboutsource
– Kamal
Nov 27 '18 at 6:49
Hey @CEJ did the updated query help?
– Kamal
Dec 10 '18 at 14:46
it did not work, is there a way to stick with what I put using source
– CEJ
Dec 11 '18 at 22:22
add a comment |
You can make use of the below query. You can check the script
part as how what you need has been implemented.
POST <your_index_name>/_search
{
"query": {
"function_score": {
"score_mode": "sum",
"query": {
"match_all": {}
},
"functions": [
{
"script_score": {
"script": {
"source": """
boolean allValues = false;
for(int i=0; i<params.value.size(); i++){
if(doc['id'].value.contains(params.value.get(i))){
allValues = true;
}else{
allValues = false;
break;
}
}
if(allValues){
return Math.pow(3,3)
} else {
return 0;
}
""",
"params":{
"value": ["123","456"]
}
}
}
}
]
}
}
}
What the query does is, it checks to see if all the values present in params i.e 123 & 456
is present in the id
field, if it does, it would go ahead and calculate score accordingly.
Not that id
field is a keyword
when I tested the query at my end.
You can use the query accordingly in your php script. Let me know if this helps!
Didn't work, gives me compile error. Whats inside contain in my case is a collection and not an object.
– CEJ
Nov 27 '18 at 5:34
@CEJ I know about that and looking at your script using a collection doesn't make sense if all you want is to verify if url contains a set of values. Wouldn't array be better off with it? And also did you try to run the above query from Kibana or Postman? I want to make sure that the above query runs in your machine using one of rest client tools. Also you can change fromsource
toinline
and give it a try if you end up with any compilation error mentioning aboutsource
– Kamal
Nov 27 '18 at 6:49
Hey @CEJ did the updated query help?
– Kamal
Dec 10 '18 at 14:46
it did not work, is there a way to stick with what I put using source
– CEJ
Dec 11 '18 at 22:22
add a comment |
You can make use of the below query. You can check the script
part as how what you need has been implemented.
POST <your_index_name>/_search
{
"query": {
"function_score": {
"score_mode": "sum",
"query": {
"match_all": {}
},
"functions": [
{
"script_score": {
"script": {
"source": """
boolean allValues = false;
for(int i=0; i<params.value.size(); i++){
if(doc['id'].value.contains(params.value.get(i))){
allValues = true;
}else{
allValues = false;
break;
}
}
if(allValues){
return Math.pow(3,3)
} else {
return 0;
}
""",
"params":{
"value": ["123","456"]
}
}
}
}
]
}
}
}
What the query does is, it checks to see if all the values present in params i.e 123 & 456
is present in the id
field, if it does, it would go ahead and calculate score accordingly.
Not that id
field is a keyword
when I tested the query at my end.
You can use the query accordingly in your php script. Let me know if this helps!
You can make use of the below query. You can check the script
part as how what you need has been implemented.
POST <your_index_name>/_search
{
"query": {
"function_score": {
"score_mode": "sum",
"query": {
"match_all": {}
},
"functions": [
{
"script_score": {
"script": {
"source": """
boolean allValues = false;
for(int i=0; i<params.value.size(); i++){
if(doc['id'].value.contains(params.value.get(i))){
allValues = true;
}else{
allValues = false;
break;
}
}
if(allValues){
return Math.pow(3,3)
} else {
return 0;
}
""",
"params":{
"value": ["123","456"]
}
}
}
}
]
}
}
}
What the query does is, it checks to see if all the values present in params i.e 123 & 456
is present in the id
field, if it does, it would go ahead and calculate score accordingly.
Not that id
field is a keyword
when I tested the query at my end.
You can use the query accordingly in your php script. Let me know if this helps!
edited Nov 26 '18 at 8:58
answered Nov 26 '18 at 7:11
KamalKamal
1,6831920
1,6831920
Didn't work, gives me compile error. Whats inside contain in my case is a collection and not an object.
– CEJ
Nov 27 '18 at 5:34
@CEJ I know about that and looking at your script using a collection doesn't make sense if all you want is to verify if url contains a set of values. Wouldn't array be better off with it? And also did you try to run the above query from Kibana or Postman? I want to make sure that the above query runs in your machine using one of rest client tools. Also you can change fromsource
toinline
and give it a try if you end up with any compilation error mentioning aboutsource
– Kamal
Nov 27 '18 at 6:49
Hey @CEJ did the updated query help?
– Kamal
Dec 10 '18 at 14:46
it did not work, is there a way to stick with what I put using source
– CEJ
Dec 11 '18 at 22:22
add a comment |
Didn't work, gives me compile error. Whats inside contain in my case is a collection and not an object.
– CEJ
Nov 27 '18 at 5:34
@CEJ I know about that and looking at your script using a collection doesn't make sense if all you want is to verify if url contains a set of values. Wouldn't array be better off with it? And also did you try to run the above query from Kibana or Postman? I want to make sure that the above query runs in your machine using one of rest client tools. Also you can change fromsource
toinline
and give it a try if you end up with any compilation error mentioning aboutsource
– Kamal
Nov 27 '18 at 6:49
Hey @CEJ did the updated query help?
– Kamal
Dec 10 '18 at 14:46
it did not work, is there a way to stick with what I put using source
– CEJ
Dec 11 '18 at 22:22
Didn't work, gives me compile error. Whats inside contain in my case is a collection and not an object.
– CEJ
Nov 27 '18 at 5:34
Didn't work, gives me compile error. Whats inside contain in my case is a collection and not an object.
– CEJ
Nov 27 '18 at 5:34
@CEJ I know about that and looking at your script using a collection doesn't make sense if all you want is to verify if url contains a set of values. Wouldn't array be better off with it? And also did you try to run the above query from Kibana or Postman? I want to make sure that the above query runs in your machine using one of rest client tools. Also you can change from
source
to inline
and give it a try if you end up with any compilation error mentioning about source
– Kamal
Nov 27 '18 at 6:49
@CEJ I know about that and looking at your script using a collection doesn't make sense if all you want is to verify if url contains a set of values. Wouldn't array be better off with it? And also did you try to run the above query from Kibana or Postman? I want to make sure that the above query runs in your machine using one of rest client tools. Also you can change from
source
to inline
and give it a try if you end up with any compilation error mentioning about source
– Kamal
Nov 27 '18 at 6:49
Hey @CEJ did the updated query help?
– Kamal
Dec 10 '18 at 14:46
Hey @CEJ did the updated query help?
– Kamal
Dec 10 '18 at 14:46
it did not work, is there a way to stick with what I put using source
– CEJ
Dec 11 '18 at 22:22
it did not work, is there a way to stick with what I put using source
– CEJ
Dec 11 '18 at 22:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476036%2fcheck-value-exists-in-params-array-using-elasticsearch-painless-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown