Creating dynamic elastic search query in php
I have to create the following elastic search query in php:
$parameters = [
'index' => 'myindex',
'from' => 0,
'size' => 60,
'body' => [
'query' => [
'bool' => [
'filter' => [[
'bool' => [
'must' => [
$myvariable,
['nested' => [
'path' => 'sh',
'query' => [
'bool' => [
'filter' => [
['term' => ['sh.keyword' => 'PK']]
]
]
]
]
]
]
]
]],
]
],
"sort" => [
["score" => ["mode" => "avg"]]
]
]
];
Where $myvariable should be:
['bool'=>
['should'=>[
['term'=> ['id'=> {$id1}]],
['term'=> ['id'=> {$id2}]],
],
'minimum_should_match'=> 1
]
]
I do not want to hardcode the values as it completely depends on the user whether she/he will choose the mentioned values in drop down or not.
What I tried?
I tried to create the inner part, that is the term part:
$countofids = count($combined_array['ids']);
if ($countofids != 0) {
for ($i = 0; $i < $countofids ; $i++) {
if ($i == 0) {
$myvariable= array('term' => array('id' => $combined_array['ids'][$i]));
} else {
array_push($myvariable, $combined_array['ids'][$i]);
}
}
}
But, it does not work as expected. I get:
['term'=> ['id'=> {$id1}]]
But, further, I do not get the required result. Moreover, I don't know how to separate them by comma.
Can anyone provide a better solution?
php

add a comment |
I have to create the following elastic search query in php:
$parameters = [
'index' => 'myindex',
'from' => 0,
'size' => 60,
'body' => [
'query' => [
'bool' => [
'filter' => [[
'bool' => [
'must' => [
$myvariable,
['nested' => [
'path' => 'sh',
'query' => [
'bool' => [
'filter' => [
['term' => ['sh.keyword' => 'PK']]
]
]
]
]
]
]
]
]],
]
],
"sort" => [
["score" => ["mode" => "avg"]]
]
]
];
Where $myvariable should be:
['bool'=>
['should'=>[
['term'=> ['id'=> {$id1}]],
['term'=> ['id'=> {$id2}]],
],
'minimum_should_match'=> 1
]
]
I do not want to hardcode the values as it completely depends on the user whether she/he will choose the mentioned values in drop down or not.
What I tried?
I tried to create the inner part, that is the term part:
$countofids = count($combined_array['ids']);
if ($countofids != 0) {
for ($i = 0; $i < $countofids ; $i++) {
if ($i == 0) {
$myvariable= array('term' => array('id' => $combined_array['ids'][$i]));
} else {
array_push($myvariable, $combined_array['ids'][$i]);
}
}
}
But, it does not work as expected. I get:
['term'=> ['id'=> {$id1}]]
But, further, I do not get the required result. Moreover, I don't know how to separate them by comma.
Can anyone provide a better solution?
php

add a comment |
I have to create the following elastic search query in php:
$parameters = [
'index' => 'myindex',
'from' => 0,
'size' => 60,
'body' => [
'query' => [
'bool' => [
'filter' => [[
'bool' => [
'must' => [
$myvariable,
['nested' => [
'path' => 'sh',
'query' => [
'bool' => [
'filter' => [
['term' => ['sh.keyword' => 'PK']]
]
]
]
]
]
]
]
]],
]
],
"sort" => [
["score" => ["mode" => "avg"]]
]
]
];
Where $myvariable should be:
['bool'=>
['should'=>[
['term'=> ['id'=> {$id1}]],
['term'=> ['id'=> {$id2}]],
],
'minimum_should_match'=> 1
]
]
I do not want to hardcode the values as it completely depends on the user whether she/he will choose the mentioned values in drop down or not.
What I tried?
I tried to create the inner part, that is the term part:
$countofids = count($combined_array['ids']);
if ($countofids != 0) {
for ($i = 0; $i < $countofids ; $i++) {
if ($i == 0) {
$myvariable= array('term' => array('id' => $combined_array['ids'][$i]));
} else {
array_push($myvariable, $combined_array['ids'][$i]);
}
}
}
But, it does not work as expected. I get:
['term'=> ['id'=> {$id1}]]
But, further, I do not get the required result. Moreover, I don't know how to separate them by comma.
Can anyone provide a better solution?
php

I have to create the following elastic search query in php:
$parameters = [
'index' => 'myindex',
'from' => 0,
'size' => 60,
'body' => [
'query' => [
'bool' => [
'filter' => [[
'bool' => [
'must' => [
$myvariable,
['nested' => [
'path' => 'sh',
'query' => [
'bool' => [
'filter' => [
['term' => ['sh.keyword' => 'PK']]
]
]
]
]
]
]
]
]],
]
],
"sort" => [
["score" => ["mode" => "avg"]]
]
]
];
Where $myvariable should be:
['bool'=>
['should'=>[
['term'=> ['id'=> {$id1}]],
['term'=> ['id'=> {$id2}]],
],
'minimum_should_match'=> 1
]
]
I do not want to hardcode the values as it completely depends on the user whether she/he will choose the mentioned values in drop down or not.
What I tried?
I tried to create the inner part, that is the term part:
$countofids = count($combined_array['ids']);
if ($countofids != 0) {
for ($i = 0; $i < $countofids ; $i++) {
if ($i == 0) {
$myvariable= array('term' => array('id' => $combined_array['ids'][$i]));
} else {
array_push($myvariable, $combined_array['ids'][$i]);
}
}
}
But, it does not work as expected. I get:
['term'=> ['id'=> {$id1}]]
But, further, I do not get the required result. Moreover, I don't know how to separate them by comma.
Can anyone provide a better solution?
php

php

edited Nov 19 '18 at 13:29
asked Nov 19 '18 at 13:21
Marium Malik
460216
460216
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I've reworked the code slightly to make it easier to read, but the idea is that you were just overwriting $myvariable
each time. I've included some test data for my own purposes and show what it's doing, but the main part is to replace the code you already have...
$combined_array = ['ids' => [1,2]];
if (count($combined_array['ids']) != 0) {
// Define array for terms
$myvariable = ;
foreach ( $combined_array['ids'] as $id ) {
// Add new term to existing list
$myvariable= array('term' => array('id' => $id));
}
// Combine data with main structure
$myvariable = [ 'bool' => ['should' => $myvariable,
'minimum_should_match'=> 1]
];
}
print_r($myvariable);
which gives...
Array
(
[bool] => Array
(
[should] => Array
(
[0] => Array
(
[term] => Array
(
[id] => 1
)
)
[1] => Array
(
[term] => Array
(
[id] => 2
)
)
)
)
)
If I've made any incorrect assumptions then let me know and I will sort them out.
Thanks for the response! How can I integrate ''minimum_should_match'=> 1'?
– Marium Malik
Nov 19 '18 at 14:09
I've added in the value, can you check it.
– Nigel Ren
Nov 19 '18 at 14:12
Yes, sorry, I saw the old one. Thanks! I will just check the result.
– Marium Malik
Nov 19 '18 at 14:12
Thanks a lot! It works as expected
– Marium Malik
Nov 19 '18 at 14:17
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%2f53375553%2fcreating-dynamic-elastic-search-query-in-php%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
I've reworked the code slightly to make it easier to read, but the idea is that you were just overwriting $myvariable
each time. I've included some test data for my own purposes and show what it's doing, but the main part is to replace the code you already have...
$combined_array = ['ids' => [1,2]];
if (count($combined_array['ids']) != 0) {
// Define array for terms
$myvariable = ;
foreach ( $combined_array['ids'] as $id ) {
// Add new term to existing list
$myvariable= array('term' => array('id' => $id));
}
// Combine data with main structure
$myvariable = [ 'bool' => ['should' => $myvariable,
'minimum_should_match'=> 1]
];
}
print_r($myvariable);
which gives...
Array
(
[bool] => Array
(
[should] => Array
(
[0] => Array
(
[term] => Array
(
[id] => 1
)
)
[1] => Array
(
[term] => Array
(
[id] => 2
)
)
)
)
)
If I've made any incorrect assumptions then let me know and I will sort them out.
Thanks for the response! How can I integrate ''minimum_should_match'=> 1'?
– Marium Malik
Nov 19 '18 at 14:09
I've added in the value, can you check it.
– Nigel Ren
Nov 19 '18 at 14:12
Yes, sorry, I saw the old one. Thanks! I will just check the result.
– Marium Malik
Nov 19 '18 at 14:12
Thanks a lot! It works as expected
– Marium Malik
Nov 19 '18 at 14:17
add a comment |
I've reworked the code slightly to make it easier to read, but the idea is that you were just overwriting $myvariable
each time. I've included some test data for my own purposes and show what it's doing, but the main part is to replace the code you already have...
$combined_array = ['ids' => [1,2]];
if (count($combined_array['ids']) != 0) {
// Define array for terms
$myvariable = ;
foreach ( $combined_array['ids'] as $id ) {
// Add new term to existing list
$myvariable= array('term' => array('id' => $id));
}
// Combine data with main structure
$myvariable = [ 'bool' => ['should' => $myvariable,
'minimum_should_match'=> 1]
];
}
print_r($myvariable);
which gives...
Array
(
[bool] => Array
(
[should] => Array
(
[0] => Array
(
[term] => Array
(
[id] => 1
)
)
[1] => Array
(
[term] => Array
(
[id] => 2
)
)
)
)
)
If I've made any incorrect assumptions then let me know and I will sort them out.
Thanks for the response! How can I integrate ''minimum_should_match'=> 1'?
– Marium Malik
Nov 19 '18 at 14:09
I've added in the value, can you check it.
– Nigel Ren
Nov 19 '18 at 14:12
Yes, sorry, I saw the old one. Thanks! I will just check the result.
– Marium Malik
Nov 19 '18 at 14:12
Thanks a lot! It works as expected
– Marium Malik
Nov 19 '18 at 14:17
add a comment |
I've reworked the code slightly to make it easier to read, but the idea is that you were just overwriting $myvariable
each time. I've included some test data for my own purposes and show what it's doing, but the main part is to replace the code you already have...
$combined_array = ['ids' => [1,2]];
if (count($combined_array['ids']) != 0) {
// Define array for terms
$myvariable = ;
foreach ( $combined_array['ids'] as $id ) {
// Add new term to existing list
$myvariable= array('term' => array('id' => $id));
}
// Combine data with main structure
$myvariable = [ 'bool' => ['should' => $myvariable,
'minimum_should_match'=> 1]
];
}
print_r($myvariable);
which gives...
Array
(
[bool] => Array
(
[should] => Array
(
[0] => Array
(
[term] => Array
(
[id] => 1
)
)
[1] => Array
(
[term] => Array
(
[id] => 2
)
)
)
)
)
If I've made any incorrect assumptions then let me know and I will sort them out.
I've reworked the code slightly to make it easier to read, but the idea is that you were just overwriting $myvariable
each time. I've included some test data for my own purposes and show what it's doing, but the main part is to replace the code you already have...
$combined_array = ['ids' => [1,2]];
if (count($combined_array['ids']) != 0) {
// Define array for terms
$myvariable = ;
foreach ( $combined_array['ids'] as $id ) {
// Add new term to existing list
$myvariable= array('term' => array('id' => $id));
}
// Combine data with main structure
$myvariable = [ 'bool' => ['should' => $myvariable,
'minimum_should_match'=> 1]
];
}
print_r($myvariable);
which gives...
Array
(
[bool] => Array
(
[should] => Array
(
[0] => Array
(
[term] => Array
(
[id] => 1
)
)
[1] => Array
(
[term] => Array
(
[id] => 2
)
)
)
)
)
If I've made any incorrect assumptions then let me know and I will sort them out.
edited Nov 19 '18 at 14:11
answered Nov 19 '18 at 13:37
Nigel Ren
25.2k61832
25.2k61832
Thanks for the response! How can I integrate ''minimum_should_match'=> 1'?
– Marium Malik
Nov 19 '18 at 14:09
I've added in the value, can you check it.
– Nigel Ren
Nov 19 '18 at 14:12
Yes, sorry, I saw the old one. Thanks! I will just check the result.
– Marium Malik
Nov 19 '18 at 14:12
Thanks a lot! It works as expected
– Marium Malik
Nov 19 '18 at 14:17
add a comment |
Thanks for the response! How can I integrate ''minimum_should_match'=> 1'?
– Marium Malik
Nov 19 '18 at 14:09
I've added in the value, can you check it.
– Nigel Ren
Nov 19 '18 at 14:12
Yes, sorry, I saw the old one. Thanks! I will just check the result.
– Marium Malik
Nov 19 '18 at 14:12
Thanks a lot! It works as expected
– Marium Malik
Nov 19 '18 at 14:17
Thanks for the response! How can I integrate ''minimum_should_match'=> 1'?
– Marium Malik
Nov 19 '18 at 14:09
Thanks for the response! How can I integrate ''minimum_should_match'=> 1'?
– Marium Malik
Nov 19 '18 at 14:09
I've added in the value, can you check it.
– Nigel Ren
Nov 19 '18 at 14:12
I've added in the value, can you check it.
– Nigel Ren
Nov 19 '18 at 14:12
Yes, sorry, I saw the old one. Thanks! I will just check the result.
– Marium Malik
Nov 19 '18 at 14:12
Yes, sorry, I saw the old one. Thanks! I will just check the result.
– Marium Malik
Nov 19 '18 at 14:12
Thanks a lot! It works as expected
– Marium Malik
Nov 19 '18 at 14:17
Thanks a lot! It works as expected
– Marium Malik
Nov 19 '18 at 14:17
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53375553%2fcreating-dynamic-elastic-search-query-in-php%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