Building in a loop
See original GitHub issueHi,
I am looking to construct the body in a loop and build all of it at once. Is it feasible.
Here is my code.
filters.forEach((filter) => {
switch (filter.type) {
case 'freetext':
if (filter.filteredValue) {
if (filter.condition === 'includes') {
bodybuilder().addQuery('match', filter.field, filter.filteredValue);
} else {
bodybuilder().notQuery('match', filter.field, filter.filteredValue);
}
}
break;
case 'singleselect':
if (filter.condition === 'includes') {
bodybuilder().addQuery('match', filter.field, filter.filteredValue);
} else {
bodybuilder().notQuery('match', filter.field, filter.filteredValue);
}
break;
case 'multiselect':
filter.filteredValue.forEach((value) => {
if (filter.condition === 'includes') {
bodybuilder().addFilter('term', filter.field, value);
} else {
bodybuilder().notFilter('term', filter.field, value);
}
});
break;
case 'search':
filter.filteredValue.forEach((field) => {
if (filter.splitFilteredValue) {
if (filter.condition === 'includes') {
bodybuilder().addFilter('term', filter.splitColumns[0], field.value.split()[0]);
bodybuilder().addFilter('term', filter.splitColumns[1], field.value.split()[1]);
} else {
bodybuilder().notFilter('term', filter.splitColumns[0], field.value.split()[0]);
bodybuilder().notFilter('term', filter.splitColumns[1], field.value.split()[1]);
}
} else {
if (filter.condition === 'includes') {
bodybuilder().addFilter('term', filter.field, field.value);
} else {
bodybuilder().notFilter('term', filter.field, field.value);
}
}
});
break;
case 'numberrange':
break;
case 'daterange':
break;
default:
}
});
console.log(bodybuilder().build());
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Build Loop App - LoopDocs - GitHub Pages
Watch in awe as you build your very own Loop app. FAQs. The FAQs for building Loop are in-line with the steps that...
Read more >Build A Loop - Swift Playgrounds - YouTube
... videos: http://www.buildingrainbows.comSolution for the " Build a Loop " activity in the Learn to Code chapter "World Building " in Apple'...
Read more >Starting Loop: Build | Loop and Learn
If you've decided to build Loop, the easiest method is to use the Build-Select script. This script works for master and FreeAPS versions...
Read more >Build a for loop from scratch | R - DataCamp
Here is an example of Build a for loop from scratch: This exercise will not introduce any new concepts on for loops.
Read more >The trust-building loop | Download Scientific Diagram
way of thinking about trust building is through the loop depicted in Fig. 2. This argues that two factors are important in getting...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@pachu4u that’s correct, I missed that detail in your code snippet. You need to operate on the same bodybuilder instance with every addQuery.
For the benefit of others
You will have to save the bodybuilder object first and then use the same for adding queries & filters. Like so
Correct approach:
bb=bodybuilder() bb.addQuery(‘match’, ‘message’, ‘this is a test’) bb.addQuery(‘match’, ‘message’, ‘this is a test’) bb.build()
Incorrect approach: bodybuilder().addQuery(‘match’, ‘message’, ‘this is a test’) bodybuilder().addQuery(‘match’, ‘message’, ‘this is a test’) bodybuilder().build()
Although I thought bodybuilder is a function it seems to be creating an object everytime.