Alexander
Alexander

Reputation: 1378

searching with elasticsearch js with multiple fields

Hi guys I have this code :

let test = await client.search({
      index: 'test',
      type: 'doc',
      body: {
        query: {
          match: {
            title: 'something',
          }
        }
      }
    });

this code is searching by 1 query which is title: 'something' , but I want to change it to search with multiple keys, for example:

 let test = await client.search({
          index: 'test',
          type: 'doc',
          body: {
            query: {
              match: {
                title: 'something',
                desc: 'some Qualifications'
              }
            }
          }
        });

but this code doesn't work and I can't find anything that will work like that, can anyone help?

Upvotes: 2

Views: 1960

Answers (1)

Val
Val

Reputation: 217274

You need to combine all the match queries using a bool/must query, like this:

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

Upvotes: 6

Related Questions