Alexander
Alexander

Reputation: 1378

How to create a search using multiple variables and to make them not mandatory

I have this code :

let jobs = await client.search({
      index: 'something',
      type: 'doc',
      body: {
        query: {
          bool: {
            must: [
              {
                match: {
                  title: `test`
                }
              },
              {
                match: {
                  desc: `test`
                }
              }
            ]
          }
        }
      }
    });

for me to make a search I need to add a title and a desc , but I wish to search even if one is not present, meaning I want either title or desc, does anyone know the correct syntax?

Upvotes: 0

Views: 26

Answers (1)

Alexander
Alexander

Reputation: 1378

Ok I found the answer, instead of must we can write should, and it will work:

let jobs = await client.search({
      index: 'something',
      type: 'doc',
      body: {
        query: {
          bool: {
            should: [
              {
                match: {
                  title: `test`
                }
              },
              {
                match: {
                  desc: `test`
                }
              }
            ]
          }
        }
      }
    });

Upvotes: 1

Related Questions