Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOC]Search templates loop works wrong #5054

Open
1 of 4 tasks
SonaGharibyan opened this issue Sep 20, 2023 · 7 comments
Open
1 of 4 tasks

[DOC]Search templates loop works wrong #5054

SonaGharibyan opened this issue Sep 20, 2023 · 7 comments
Assignees
Labels
1 - Backlog Issue: The issue is unassigned or assigned but not started Needs SME Waiting on input from subject matter expert search-and-query Issue: Issues related to search and query features Sev2 High-medium priority. Upcoming release or incorrect information.

Comments

@SonaGharibyan
Copy link

SonaGharibyan commented Sep 20, 2023

What do you want to do?

  • Request a change to existing documentation
  • Add new documentation
  • Report a technical problem with the documentation
  • Other

Tell us about your request. Provide a summary of the request and all versions that are affected.
Having issues with using search templates loops https://opensearch.org/docs/latest/search-plugins/search-template/#loops
I have this query

{
   "source":{
    "query":{
      "bool":{
         "filter":[
            {
               "terms":{
                  "field":["{{#names}}","{{.}}","{{/names}}","3"]
               }
            }
         ]
      }
   }
},
   "params":{
      "names":["1", "2"]
   }
}

which should return items where "field" equals to "1", "2" or "3". But it also return items where "field" is to empty string ("").

Is there another way to destruct "names" array?
pls update docs accordingly

What other resources are available? Provide links to related issues, POCs, steps for testing, etc.
Found similar question just from Elastic Search -> https://stackoverflow.com/questions/67590223/how-to-pass-array-to-elasticsearch-search-template-using-mustache

@Naarcha-AWS Naarcha-AWS added 1 - Backlog Issue: The issue is unassigned or assigned but not started Needs SME Waiting on input from subject matter expert search-and-query Issue: Issues related to search and query features and removed untriaged labels Oct 16, 2023
@Naarcha-AWS Naarcha-AWS added the Sev2 High-medium priority. Upcoming release or incorrect information. label Oct 16, 2023
@kolchfa-aws
Copy link
Collaborator

@SonaGharibyan Can you try passing 1 and 2 as strings:
"params":{ "names":["1", "2"] }
And also passing the whole query as a string in the "source" parameter. (Note the format in this section).

@SonaGharibyan
Copy link
Author

@kolchfa-aws Yes sorry regarding "params" it was a typo I've updated it. And I've also tried to pass "source" as a string parameter and it also returns all the values where "team" filed (in this example) is empty string ("").

{
  "source": "{\"query\":{\"terms\":{\"team\":[\"{{#myTeams}}\",\"{{.}}\",\"{{/myTeams}}\"]}}}",
  "params": {
    "myTeams":  ["Team A", "Team C"]
  }
}

@kolchfa-aws
Copy link
Collaborator

kolchfa-aws commented Nov 2, 2023

@SonaGharibyan Mustache inserts a comma after every array element so you're getting a match on a phantom empty element produced by a comma after the last term. To avoid this problem, run the query as follows:

GET _search/template
{
  "source": "{\"query\":{\"terms\":{\"team\":{{#toJson}}myTeams{{/toJson}}}}}",
  "params": {
    "myTeams":  ["Team A", "Team C"]
  }
}

@SonaGharibyan
Copy link
Author

@kolchfa-aws but I also need to add extra elements like "3" here

{
   "source":{
    "query":{
      "bool":{
         "filter":[
            {
               "terms":{
                  "field":["{{#names}}","{{.}}","{{/names}}","3"]
               }
            }
         ]
      }
   }
},
   "params":{
      "names":["1", "2"]
   }
}

@kolchfa-aws
Copy link
Collaborator

Can you just include "3" as an element of the names array?

@SonaGharibyan
Copy link
Author

@kolchfa-aws no, in my case this two data are coming from different places and I don't have access to names array.

@kolchfa-aws
Copy link
Collaborator

@opensearch-project/opensearch-core Could someone take a look at this issue please? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 - Backlog Issue: The issue is unassigned or assigned but not started Needs SME Waiting on input from subject matter expert search-and-query Issue: Issues related to search and query features Sev2 High-medium priority. Upcoming release or incorrect information.
Projects
None yet
Development

No branches or pull requests

3 participants