Elasticsearch must some values and must not all others
so I have an index with the following mapping:
{
"tests": {
"mappings": {
"test": {
"properties": {
"arr": {
"properties": {
"name": {
"type": "long"
}
}
}
}
}
}
}
I have 3 documents with the following values for the field "arr":
arr: [{name: 1}, {name: 2}, {name: 3}]
arr: [{name: 1}, {name: 2}, {name: 4}]
arr: [{name: 1}, {name: 2}]
I would like to search in a way that I could find the documents in which all name values are in an array of name. However, the document doesn't need to have all the values of the array, and if it has one value that is not in the array, then the document is not good.
For example:
- If my array of names is
[1,2]
, then I only want document 3, but I have all of them - If my array of names is
[1,2,3]
, then I want document 1 and 3, but I only have document 1 withmust
and all of them withshould
- If my array of names is
[1,2,4]
, then I want document 2 and 3, but I only have document 2 withmust
and all of them withshould
- If my array of names is
[1]
, then I want no document, but I have all of them
Here, the arrays are small, in my project the arrays in the documents are much more bigger and the array of comparison as well.
So, to be specific, I need to search in a way that:
- All the names in the document are in the array of names
- The document does not need to have ALL the values in the array of name
Thank you in advance
arrays node.js elasticsearch search
add a comment |
so I have an index with the following mapping:
{
"tests": {
"mappings": {
"test": {
"properties": {
"arr": {
"properties": {
"name": {
"type": "long"
}
}
}
}
}
}
}
I have 3 documents with the following values for the field "arr":
arr: [{name: 1}, {name: 2}, {name: 3}]
arr: [{name: 1}, {name: 2}, {name: 4}]
arr: [{name: 1}, {name: 2}]
I would like to search in a way that I could find the documents in which all name values are in an array of name. However, the document doesn't need to have all the values of the array, and if it has one value that is not in the array, then the document is not good.
For example:
- If my array of names is
[1,2]
, then I only want document 3, but I have all of them - If my array of names is
[1,2,3]
, then I want document 1 and 3, but I only have document 1 withmust
and all of them withshould
- If my array of names is
[1,2,4]
, then I want document 2 and 3, but I only have document 2 withmust
and all of them withshould
- If my array of names is
[1]
, then I want no document, but I have all of them
Here, the arrays are small, in my project the arrays in the documents are much more bigger and the array of comparison as well.
So, to be specific, I need to search in a way that:
- All the names in the document are in the array of names
- The document does not need to have ALL the values in the array of name
Thank you in advance
arrays node.js elasticsearch search
Do you have the control of your mapping like could you add an additional field? Justone
to be precise.
– Kamal
Nov 24 '18 at 18:16
Yes, I can do whatever I want with it :)
– Coodie_d
Nov 24 '18 at 18:17
Sorry, I've updated my answer. Let me know if it helps!
– Kamal
Nov 24 '18 at 18:46
add a comment |
so I have an index with the following mapping:
{
"tests": {
"mappings": {
"test": {
"properties": {
"arr": {
"properties": {
"name": {
"type": "long"
}
}
}
}
}
}
}
I have 3 documents with the following values for the field "arr":
arr: [{name: 1}, {name: 2}, {name: 3}]
arr: [{name: 1}, {name: 2}, {name: 4}]
arr: [{name: 1}, {name: 2}]
I would like to search in a way that I could find the documents in which all name values are in an array of name. However, the document doesn't need to have all the values of the array, and if it has one value that is not in the array, then the document is not good.
For example:
- If my array of names is
[1,2]
, then I only want document 3, but I have all of them - If my array of names is
[1,2,3]
, then I want document 1 and 3, but I only have document 1 withmust
and all of them withshould
- If my array of names is
[1,2,4]
, then I want document 2 and 3, but I only have document 2 withmust
and all of them withshould
- If my array of names is
[1]
, then I want no document, but I have all of them
Here, the arrays are small, in my project the arrays in the documents are much more bigger and the array of comparison as well.
So, to be specific, I need to search in a way that:
- All the names in the document are in the array of names
- The document does not need to have ALL the values in the array of name
Thank you in advance
arrays node.js elasticsearch search
so I have an index with the following mapping:
{
"tests": {
"mappings": {
"test": {
"properties": {
"arr": {
"properties": {
"name": {
"type": "long"
}
}
}
}
}
}
}
I have 3 documents with the following values for the field "arr":
arr: [{name: 1}, {name: 2}, {name: 3}]
arr: [{name: 1}, {name: 2}, {name: 4}]
arr: [{name: 1}, {name: 2}]
I would like to search in a way that I could find the documents in which all name values are in an array of name. However, the document doesn't need to have all the values of the array, and if it has one value that is not in the array, then the document is not good.
For example:
- If my array of names is
[1,2]
, then I only want document 3, but I have all of them - If my array of names is
[1,2,3]
, then I want document 1 and 3, but I only have document 1 withmust
and all of them withshould
- If my array of names is
[1,2,4]
, then I want document 2 and 3, but I only have document 2 withmust
and all of them withshould
- If my array of names is
[1]
, then I want no document, but I have all of them
Here, the arrays are small, in my project the arrays in the documents are much more bigger and the array of comparison as well.
So, to be specific, I need to search in a way that:
- All the names in the document are in the array of names
- The document does not need to have ALL the values in the array of name
Thank you in advance
arrays node.js elasticsearch search
arrays node.js elasticsearch search
asked Nov 24 '18 at 13:21
Coodie_dCoodie_d
206
206
Do you have the control of your mapping like could you add an additional field? Justone
to be precise.
– Kamal
Nov 24 '18 at 18:16
Yes, I can do whatever I want with it :)
– Coodie_d
Nov 24 '18 at 18:17
Sorry, I've updated my answer. Let me know if it helps!
– Kamal
Nov 24 '18 at 18:46
add a comment |
Do you have the control of your mapping like could you add an additional field? Justone
to be precise.
– Kamal
Nov 24 '18 at 18:16
Yes, I can do whatever I want with it :)
– Coodie_d
Nov 24 '18 at 18:17
Sorry, I've updated my answer. Let me know if it helps!
– Kamal
Nov 24 '18 at 18:46
Do you have the control of your mapping like could you add an additional field? Just
one
to be precise.– Kamal
Nov 24 '18 at 18:16
Do you have the control of your mapping like could you add an additional field? Just
one
to be precise.– Kamal
Nov 24 '18 at 18:16
Yes, I can do whatever I want with it :)
– Coodie_d
Nov 24 '18 at 18:17
Yes, I can do whatever I want with it :)
– Coodie_d
Nov 24 '18 at 18:17
Sorry, I've updated my answer. Let me know if it helps!
– Kamal
Nov 24 '18 at 18:46
Sorry, I've updated my answer. Let me know if it helps!
– Kamal
Nov 24 '18 at 18:46
add a comment |
1 Answer
1
active
oldest
votes
Using Scripting (No Mapping Change)
No mapping changes are required to what you already have.
I've come up with the below script. I believe the core logic is in the script which I think is self-explanatory.
POST test_index_arr/_search
{
"query":{
"bool": {
"filter": {
"script": {
"script": {
"source" : """
List myList = doc['arr.name'];
int tempVal = myList.size();
for(int i=0; i<params.ids.size(); i++){
long temp = params.ids.get(i);
if(myList.contains(temp))
{
tempVal = tempVal - 1;
}
if(tempVal==0){
break;
}
}
if(tempVal==0)
return true;
else
return false;
""",
"lang" : "painless",
"params": {
"ids": [1,2,4]
}
}
}
}
}
}
}
So what the scripting does is, it checks if a document has all its numbers in its arr
present in the input, if so it would return the document.
In the above query, the part "ids": [1,2,4]
acts as input. You need to add/remove/update values here this depending on your requirement.
Hope it helps!
1
This is a good solution, but for small arrays, with few elements in arrays. In your terms query, if I have a document with names"1/4"
and want to make it work, I have to add"1/4"
to the terms query. For small arrays it worked, but with more then 20 or 50 items in array and my documents, I have to do all the possibilities to make it work, is that really good?
– Coodie_d
Nov 24 '18 at 19:05
@ArrowLightning Valid point. I've come up with another solution where no mapping changes are required. Could you please check and let me know if it helps.
– Kamal
Nov 24 '18 at 21:10
I already thought about script before, I was wondering if there were other options but script works fine!
– Coodie_d
Nov 25 '18 at 12:10
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%2f53458569%2felasticsearch-must-some-values-and-must-not-all-others%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
Using Scripting (No Mapping Change)
No mapping changes are required to what you already have.
I've come up with the below script. I believe the core logic is in the script which I think is self-explanatory.
POST test_index_arr/_search
{
"query":{
"bool": {
"filter": {
"script": {
"script": {
"source" : """
List myList = doc['arr.name'];
int tempVal = myList.size();
for(int i=0; i<params.ids.size(); i++){
long temp = params.ids.get(i);
if(myList.contains(temp))
{
tempVal = tempVal - 1;
}
if(tempVal==0){
break;
}
}
if(tempVal==0)
return true;
else
return false;
""",
"lang" : "painless",
"params": {
"ids": [1,2,4]
}
}
}
}
}
}
}
So what the scripting does is, it checks if a document has all its numbers in its arr
present in the input, if so it would return the document.
In the above query, the part "ids": [1,2,4]
acts as input. You need to add/remove/update values here this depending on your requirement.
Hope it helps!
1
This is a good solution, but for small arrays, with few elements in arrays. In your terms query, if I have a document with names"1/4"
and want to make it work, I have to add"1/4"
to the terms query. For small arrays it worked, but with more then 20 or 50 items in array and my documents, I have to do all the possibilities to make it work, is that really good?
– Coodie_d
Nov 24 '18 at 19:05
@ArrowLightning Valid point. I've come up with another solution where no mapping changes are required. Could you please check and let me know if it helps.
– Kamal
Nov 24 '18 at 21:10
I already thought about script before, I was wondering if there were other options but script works fine!
– Coodie_d
Nov 25 '18 at 12:10
add a comment |
Using Scripting (No Mapping Change)
No mapping changes are required to what you already have.
I've come up with the below script. I believe the core logic is in the script which I think is self-explanatory.
POST test_index_arr/_search
{
"query":{
"bool": {
"filter": {
"script": {
"script": {
"source" : """
List myList = doc['arr.name'];
int tempVal = myList.size();
for(int i=0; i<params.ids.size(); i++){
long temp = params.ids.get(i);
if(myList.contains(temp))
{
tempVal = tempVal - 1;
}
if(tempVal==0){
break;
}
}
if(tempVal==0)
return true;
else
return false;
""",
"lang" : "painless",
"params": {
"ids": [1,2,4]
}
}
}
}
}
}
}
So what the scripting does is, it checks if a document has all its numbers in its arr
present in the input, if so it would return the document.
In the above query, the part "ids": [1,2,4]
acts as input. You need to add/remove/update values here this depending on your requirement.
Hope it helps!
1
This is a good solution, but for small arrays, with few elements in arrays. In your terms query, if I have a document with names"1/4"
and want to make it work, I have to add"1/4"
to the terms query. For small arrays it worked, but with more then 20 or 50 items in array and my documents, I have to do all the possibilities to make it work, is that really good?
– Coodie_d
Nov 24 '18 at 19:05
@ArrowLightning Valid point. I've come up with another solution where no mapping changes are required. Could you please check and let me know if it helps.
– Kamal
Nov 24 '18 at 21:10
I already thought about script before, I was wondering if there were other options but script works fine!
– Coodie_d
Nov 25 '18 at 12:10
add a comment |
Using Scripting (No Mapping Change)
No mapping changes are required to what you already have.
I've come up with the below script. I believe the core logic is in the script which I think is self-explanatory.
POST test_index_arr/_search
{
"query":{
"bool": {
"filter": {
"script": {
"script": {
"source" : """
List myList = doc['arr.name'];
int tempVal = myList.size();
for(int i=0; i<params.ids.size(); i++){
long temp = params.ids.get(i);
if(myList.contains(temp))
{
tempVal = tempVal - 1;
}
if(tempVal==0){
break;
}
}
if(tempVal==0)
return true;
else
return false;
""",
"lang" : "painless",
"params": {
"ids": [1,2,4]
}
}
}
}
}
}
}
So what the scripting does is, it checks if a document has all its numbers in its arr
present in the input, if so it would return the document.
In the above query, the part "ids": [1,2,4]
acts as input. You need to add/remove/update values here this depending on your requirement.
Hope it helps!
Using Scripting (No Mapping Change)
No mapping changes are required to what you already have.
I've come up with the below script. I believe the core logic is in the script which I think is self-explanatory.
POST test_index_arr/_search
{
"query":{
"bool": {
"filter": {
"script": {
"script": {
"source" : """
List myList = doc['arr.name'];
int tempVal = myList.size();
for(int i=0; i<params.ids.size(); i++){
long temp = params.ids.get(i);
if(myList.contains(temp))
{
tempVal = tempVal - 1;
}
if(tempVal==0){
break;
}
}
if(tempVal==0)
return true;
else
return false;
""",
"lang" : "painless",
"params": {
"ids": [1,2,4]
}
}
}
}
}
}
}
So what the scripting does is, it checks if a document has all its numbers in its arr
present in the input, if so it would return the document.
In the above query, the part "ids": [1,2,4]
acts as input. You need to add/remove/update values here this depending on your requirement.
Hope it helps!
edited Nov 25 '18 at 6:47
answered Nov 24 '18 at 18:27
KamalKamal
1,7531920
1,7531920
1
This is a good solution, but for small arrays, with few elements in arrays. In your terms query, if I have a document with names"1/4"
and want to make it work, I have to add"1/4"
to the terms query. For small arrays it worked, but with more then 20 or 50 items in array and my documents, I have to do all the possibilities to make it work, is that really good?
– Coodie_d
Nov 24 '18 at 19:05
@ArrowLightning Valid point. I've come up with another solution where no mapping changes are required. Could you please check and let me know if it helps.
– Kamal
Nov 24 '18 at 21:10
I already thought about script before, I was wondering if there were other options but script works fine!
– Coodie_d
Nov 25 '18 at 12:10
add a comment |
1
This is a good solution, but for small arrays, with few elements in arrays. In your terms query, if I have a document with names"1/4"
and want to make it work, I have to add"1/4"
to the terms query. For small arrays it worked, but with more then 20 or 50 items in array and my documents, I have to do all the possibilities to make it work, is that really good?
– Coodie_d
Nov 24 '18 at 19:05
@ArrowLightning Valid point. I've come up with another solution where no mapping changes are required. Could you please check and let me know if it helps.
– Kamal
Nov 24 '18 at 21:10
I already thought about script before, I was wondering if there were other options but script works fine!
– Coodie_d
Nov 25 '18 at 12:10
1
1
This is a good solution, but for small arrays, with few elements in arrays. In your terms query, if I have a document with names
"1/4"
and want to make it work, I have to add "1/4"
to the terms query. For small arrays it worked, but with more then 20 or 50 items in array and my documents, I have to do all the possibilities to make it work, is that really good?– Coodie_d
Nov 24 '18 at 19:05
This is a good solution, but for small arrays, with few elements in arrays. In your terms query, if I have a document with names
"1/4"
and want to make it work, I have to add "1/4"
to the terms query. For small arrays it worked, but with more then 20 or 50 items in array and my documents, I have to do all the possibilities to make it work, is that really good?– Coodie_d
Nov 24 '18 at 19:05
@ArrowLightning Valid point. I've come up with another solution where no mapping changes are required. Could you please check and let me know if it helps.
– Kamal
Nov 24 '18 at 21:10
@ArrowLightning Valid point. I've come up with another solution where no mapping changes are required. Could you please check and let me know if it helps.
– Kamal
Nov 24 '18 at 21:10
I already thought about script before, I was wondering if there were other options but script works fine!
– Coodie_d
Nov 25 '18 at 12:10
I already thought about script before, I was wondering if there were other options but script works fine!
– Coodie_d
Nov 25 '18 at 12:10
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%2f53458569%2felasticsearch-must-some-values-and-must-not-all-others%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
Do you have the control of your mapping like could you add an additional field? Just
one
to be precise.– Kamal
Nov 24 '18 at 18:16
Yes, I can do whatever I want with it :)
– Coodie_d
Nov 24 '18 at 18:17
Sorry, I've updated my answer. Let me know if it helps!
– Kamal
Nov 24 '18 at 18:46