How to ensure all words are matched across multiple bool.should.clauses?


(Joon Ho Cho) #1

I want to make sure all words are matched by at least one clause under bool.should query.

Here's my data structure:

person = {
  username,
  names: [{first, last}], // nested
  addresses: [{city, state}], // nested
}

Lets say we have a person in db:

{
  names: [{first: "john", last: "doe"}],
  addresses: [{city: "san francisco", state: "ca"}],
}

User types "john doe" in search box and is matched since we have a matching data.
User types "john doe san francisco" and is still matched, now with HIGHER score.
User types "john doe san diego" and should not be matched because diego is not matched.

How can I achieve this?

Here's my current query which does not guarantee all words are matched:

query: {
  bool: {
    should: [
      {match: {username: text}},
      {
        nested: {
          path: 'names',
          query: {
            multi_match: {
              query: text,
              type: 'cross_fields',
              fields: [
                'names.first',
                'names.last',
              ],
            },
          },
        },
      },
      {
        nested: {
          path: 'addresses',
          query: {
            multi_match: {
              query: text,
              type: 'cross_fields',
              fields: [
                'addresses.city',
                'addresses.state',
              ],
            },
          },
        },
      },
    ],
  },
},

(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.