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

Nested bool query with must #162

Closed
petoknm opened this issue Jan 24, 2018 · 2 comments · Fixed by #163
Closed

Nested bool query with must #162

petoknm opened this issue Jan 24, 2018 · 2 comments · Fixed by #163
Labels

Comments

@petoknm
Copy link

petoknm commented Jan 24, 2018

I have a really difficult time trying to build this type of query

{
    "query": {
        "bool": {
            "must": {
                "bool" : { "should": [
                      { "match": { "title": "Elasticsearch" }},
                      { "match": { "title": "Solr" }} ] }
            },
            "must": { "match": { "authors": "clinton gormely" }},
            "must_not": { "match": {"authors": "radu gheorge" }}
        }
    }
}

Because when constructing the nested bool with a should inside, the library puts an extra query key there. So the output of this

bodybuilder()
        .query('match', 'message', 'this is a test')
	.query('bool', b => b.orQuery('match', 'm', 'asd').orQuery('match', 'm', 'dsa'))
        .build()

is

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "message": "this is a test"
          }
        },
        {
          "bool": {
            "query": {
              "bool": {
                "should": [
                  {
                    "match": {
                      "m": "asd"
                    }
                  },
                  {
                    "match": {
                      "m": "dsa"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

Which is not a valid query. But changing the query to a filter works fine:

bodybuilder()
	.query('match', 'message', 'this is a test')
	.filter('bool', b => b.orFilter('match', 'm', 'asd').orFilter('match', 'm', 'dsa'))
        .build()

with the following output

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "match": {
                "m": "asd"
              }
            },
            {
              "match": {
                "m": "dsa"
              }
            }
          ]
        }
      },
      "must": {
        "match": {
          "message": "this is a test"
        }
      }
    }
  }
}

Is this a bug or am I just missing something?
Thanks

@ferronrsmith
Copy link
Collaborator

bodybuilder()
        .query('bool', b => b.orQuery('match', 'title', 'Solr').orQuery('match', 'title', 'Elasticsearch'))
	.query('match', 'authors', 'clinton gormely')
        .notQuery('match', 'authors', 'radu gheorge').build()

=>

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "query": {
              "bool": {
                "should": [
                  {
                    "match": {
                      "title": "Solr"
                    }
                  },
                  {
                    "match": {
                      "title": "Elasticsearch"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "match": {
            "authors": "clinton gormely"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "authors": "radu gheorge"
          }
        }
      ]
    }
  }
}

Definitely seems like a bug

@danpaz
Copy link
Owner

danpaz commented Jan 25, 2018

Agree it's a bug and likely a duplicate of #142

ferronrsmith added a commit that referenced this issue Jan 29, 2018
bodybuilder now generates valid nested queries

fixes #142
fixes #162
ferronrsmith added a commit that referenced this issue Jan 29, 2018
bodybuilder now generates valid nested queries

fixes #142
fixes #162
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants