Step 6: Hybrid queries
In this step, we will not only query data using vector similarity but also use the metadata field to filter the results.
When submitting a query, you can optionally add a filter text field to the query. This field is compatible with the Lucene query syntax (also known as OpenSearch/ElasticSearch query syntax).
note
Filters are only evaluated against item metadata fields. They are not evaluated against the item's value or vector fields.
Filtering by salary
Let's add a salary filter to the query. This filter will only return items with a salary greater than 12,000:
- JavaScript
- Python
  const results = await client.query({
      databaseId,
      query: {
          vector: [0.5, 0.5, 0.5, 0.5],
      },
      filter: 'salary:>12000'
  });
  console.log(results);
  results = await client.query(QueryInput(
      database_id=databaseId,
      query=QueryTypeVector([0.5, 0.5, 0.5, 0.5]),
      filter='salary:>12000'
  ))
  print(results)
Now, let's query for salary between 10,000 and 15,000:
- JavaScript
- Python
  const results = await client.query({
      databaseId,
      query: {
          vector: [0.5, 0.5, 0.5, 0.5],
      },
      filter: 'salary:[10000 TO 15000]'
  });
  console.log(results);
  results = await client.query(QueryInput(
      database_id=databaseId,
      query=QueryTypeVector([0.5, 0.5, 0.5, 0.5]),
      filter='salary:[10000 TO 15000]'
  ))
  print(results)
Filtering by tags
You can also filter by tags. Let's filter by the tag javascript:
- JavaScript
- Python
  const results = await client.query({
      databaseId,
      query: {
          vector: [0.5, 0.5, 0.5, 0.5],
      },
      filter: 'tags:javascript'
  });
  console.log(results);
  results = await client.query(QueryInput(
      database_id=databaseId,
      query=QueryTypeVector([0.5, 0.5, 0.5, 0.5]),
      filter='tags:javascript'
  ))
  print(results)
Combining filters
You can combine filters using boolean operators. Let's combine the salary and tag filters:
- JavaScript
- Python
  const results = await client.query({
      databaseId,
      query: {
          vector: [0.5, 0.5, 0.5, 0.5],
      },
      filter: 'salary:[15000 TO 15000] AND tags:javascript'
  });
  console.log(results);
  results = await client.query(QueryInput(
      database_id=databaseId,
      query=QueryTypeVector([0.5, 0.5, 0.5, 0.5]),
      filter='salary:[15000 TO 15000] AND tags:javascript'
  ))
  print(results)