Inferensys

Prompt

Combined AND/OR Filter Logic Prompt Template

A practical prompt playbook for extracting nested boolean filter structures from complex enterprise search queries that combine multiple facet types, operator precedence, and logical constraints.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
PROMPT PLAYBOOK

When to Use This Prompt

Determine if your search interface requires nested boolean filter logic instead of simpler single-facet extraction.

This prompt is designed for platform engineers and search relevance teams who need to convert complex natural language queries into structured boolean filter logic. When a user types 'documents about compliance from the legal or finance team published after January but not about GDPR,' the retrieval system needs a nested AND/OR/NOT filter structure that respects operator precedence and resolves conflicts between constraints. This prompt handles multi-facet queries where simple flat filters fail.

Use this prompt when your search interface must support compound constraints across date ranges, categorical facets, source filters, and exclusion patterns simultaneously. The ideal deployment context is an enterprise search or RAG system where users express multi-dimensional information needs in a single query. Required context includes a defined metadata schema with known field names, types, and valid values. Without this schema, the model cannot reliably map user terms to filter clauses. You should also have a clear operator precedence policy—whether AND binds tighter than OR, or vice versa—and a conflict resolution strategy for contradictory constraints like 'from marketing and from legal.'

Do not use this prompt for single-facet extraction or simple equality filters. If a user query only contains one type of constraint, such as a date range or a single source filter, a dedicated extraction prompt like the Date Range Extraction Prompt Template or Source Filter Generation Prompt Template will be simpler, cheaper, and more reliable. This prompt is also not appropriate when your search backend does not support nested boolean logic, or when latency budgets are too tight for an additional LLM call in the retrieval path. In those cases, consider pre-computed filter templates or rule-based extraction.

Before deploying this prompt, verify that your evaluation dataset includes queries with mixed AND/OR/NOT constraints, ambiguous operator scoping, and contradictory conditions. These edge cases are where flat filter extraction fails and nested boolean logic becomes necessary. If your production queries rarely combine multiple facet types, you may be over-engineering the filter layer. Start with single-facet extraction prompts and only introduce this combined logic prompt when user behavior data shows compound queries are common and retrieval quality degrades without them.

PRACTICAL GUARDRAILS

Use Case Fit

Where this prompt works and where it does not. Understand the operational boundaries before embedding this template into a production search pipeline.

01

Good Fit: Multi-Faceted Enterprise Search

Use when: Users combine categorical, temporal, and numerical constraints in a single query (e.g., 'docs from last quarter by the compliance team, excluding drafts'). Why: The prompt excels at generating nested boolean structures that a single vector search cannot resolve. Guardrail: Always validate the output against your actual metadata schema before executing the query.

02

Bad Fit: Simple Keyword Lookup

Avoid when: The user query is a single term or a straightforward phrase without explicit filtering intent. Risk: Overhead from complex boolean logic generation adds latency and potential for hallucinated constraints when none are needed. Guardrail: Pre-classify the query intent and route simple lookups directly to a basic search endpoint, bypassing this template.

03

Required Inputs

Do not invoke without: A natural language query containing multiple explicit constraints, and a strictly defined metadata schema (field names, types, valid values). Risk: Without a schema, the model will invent field names and operators, producing invalid filter clauses. Guardrail: Inject the allowed metadata schema directly into the prompt's [SCHEMA] placeholder on every call.

04

Operational Risk: Operator Precedence Errors

What to watch: The model generates logically ambiguous nesting where AND/OR precedence changes the result set (e.g., A OR B AND C). Risk: Retrieving the wrong documents silently, leading to incorrect answers downstream. Guardrail: Implement a post-generation linter that parses the JSON and enforces explicit nesting with unambiguous parenthetical grouping before execution.

05

Operational Risk: Filter Contradiction

What to watch: The model generates mutually exclusive filters that result in zero documents (e.g., status: draft AND status: published). Risk: Empty result sets that break the user experience or trigger unnecessary fallback logic. Guardrail: Add a pre-execution check that scans for contradictory constraints on the same field and either rewrites or surfaces a clarification request.

06

Operational Risk: Hallucinated Values

What to watch: The model extracts a plausible but non-existent filter value (e.g., a product category 'enterprise' when only 'business' exists). Risk: Retrieving zero results or, worse, matching on a similarly named but incorrect facet. Guardrail: Post-process extracted values against a canonical list of allowed facets. Drop or flag any value that doesn't match, and log for schema gap analysis.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A reusable prompt that translates multi-constraint natural language queries into nested boolean filter structures with explicit operator precedence and conflict resolution.

This template is designed for production search and RAG systems where users express complex filter logic in natural language—combining categories, date ranges, source restrictions, and numerical thresholds with explicit AND, OR, and NOT relationships. The prompt instructs the model to produce a structured boolean tree rather than a flat list of filters, preserving operator precedence and nesting that downstream search backends require. Use this when your application must handle queries like 'documents about pricing from the sales or marketing team, but not from Q3, and only for enterprise accounts.'

text
You are a search filter translator. Given a user's natural language query, extract all filter constraints and produce a nested boolean filter structure that preserves the logical relationships between constraints.

[INPUT]

[CONTEXT]
- Available metadata fields: [AVAILABLE_FIELDS]
- Field types and valid values: [FIELD_SCHEMA]
- Current date for relative time resolution: [ANCHOR_DATE]
- User's access scope (if applicable): [USER_SCOPE]

[OUTPUT_SCHEMA]
Return a JSON object with this structure:
{
  "original_query": "string",
  "filter_tree": {
    "operator": "AND" | "OR" | "NOT",
    "operands": [
      {
        "type": "constraint" | "subtree",
        "field": "string (for constraint type)",
        "operator": "eq" | "neq" | "gt" | "gte" | "lt" | "lte" | "in" | "not_in" | "between" | "contains",
        "value": "string | number | array | {min, max}",
        "confidence": 0.0-1.0
      },
      {
        "type": "subtree",
        "operator": "AND" | "OR" | "NOT",
        "operands": [...]
      }
    ]
  },
  "unresolved_terms": ["terms that couldn't be mapped to fields"],
  "warnings": ["operator precedence notes", "potential contradictions"]
}

[CONSTRAINTS]
1. Preserve explicit AND/OR/NOT relationships from the user's language.
2. When operator precedence is ambiguous, default to AND binding tighter than OR, and note this in warnings.
3. Map all date expressions to ISO 8601 ranges using [ANCHOR_DATE].
4. Only use fields present in [AVAILABLE_FIELDS]. Place unmapped constraints in unresolved_terms.
5. Flag detected contradictions in warnings (e.g., 'before 2020 AND after 2023').
6. Assign confidence scores: 1.0 for explicit constraints, lower for inferred or ambiguous ones.
7. For NOT constraints, ensure the scope of negation is clear in the tree structure.
8. Do not invent fields, values, or operators not supported by [FIELD_SCHEMA].

[EXAMPLES]
Query: "Reports about cloud costs from engineering or finance, but not drafts"
Output:
{
  "original_query": "Reports about cloud costs from engineering or finance, but not drafts",
  "filter_tree": {
    "operator": "AND",
    "operands": [
      {
        "type": "constraint",
        "field": "topic",
        "operator": "contains",
        "value": "cloud costs",
        "confidence": 0.9
      },
      {
        "type": "subtree",
        "operator": "OR",
        "operands": [
          {"type": "constraint", "field": "department", "operator": "eq", "value": "engineering", "confidence": 1.0},
          {"type": "constraint", "field": "department", "operator": "eq", "value": "finance", "confidence": 1.0}
        ]
      },
      {
        "type": "subtree",
        "operator": "NOT",
        "operands": [
          {"type": "constraint", "field": "status", "operator": "eq", "value": "draft", "confidence": 1.0}
        ]
      }
    ]
  },
  "unresolved_terms": [],
  "warnings": []
}

Query: "Enterprise deals over 100k from last quarter, excluding the APAC region"
Output:
{
  "original_query": "Enterprise deals over 100k from last quarter, excluding the APAC region",
  "filter_tree": {
    "operator": "AND",
    "operands": [
      {"type": "constraint", "field": "segment", "operator": "eq", "value": "enterprise", "confidence": 1.0},
      {"type": "constraint", "field": "deal_value", "operator": "gt", "value": 100000, "confidence": 1.0},
      {"type": "constraint", "field": "close_date", "operator": "between", "value": {"min": "2024-10-01", "max": "2024-12-31"}, "confidence": 0.95},
      {
        "type": "subtree",
        "operator": "NOT",
        "operands": [
          {"type": "constraint", "field": "region", "operator": "eq", "value": "APAC", "confidence": 1.0}
        ]
      }
    ]
  },
  "unresolved_terms": [],
  "warnings": ["'last quarter' resolved relative to anchor date 2025-01-15"]
}

Adapt this template by replacing the square-bracket placeholders with your application's runtime values. [AVAILABLE_FIELDS] should list every filterable metadata field in your search index. [FIELD_SCHEMA] must specify each field's data type and valid values—this prevents the model from hallucinating operators or values your backend cannot process. [ANCHOR_DATE] is critical for temporal queries; pass the current date in ISO format so relative expressions resolve consistently. [USER_SCOPE] is optional but recommended for enterprise deployments where access controls should constrain filter generation. The [EXAMPLES] section is deliberately included in the prompt to demonstrate the expected tree structure and confidence annotation pattern. Replace these with examples from your own domain for better in-domain accuracy.

Before deploying, validate that your application layer can consume the nested filter_tree structure. Most search backends require flattening or recursive traversal of boolean trees. Build a post-processing step that converts the tree into your backend's query DSL—whether Elasticsearch bool queries, Pinecone metadata filters, or PostgreSQL WHERE clauses. Add a validation check that rejects filter trees referencing unknown fields or unsupported operators before they reach your search index. For high-stakes search experiences, log unresolved_terms and warnings so your team can monitor extraction quality and expand field coverage over time.

IMPLEMENTATION TABLE

Prompt Variables

Required inputs for the Combined AND/OR Filter Logic Prompt Template. Each variable must be populated before the prompt is assembled and sent to the model. Missing or malformed inputs are the most common cause of production failures in nested boolean filter generation.

PlaceholderPurposeExampleValidation Notes

[USER_QUERY]

The raw natural language query containing multiple constraints that must be translated into nested boolean filter logic

Show me enterprise accounts in North America or Europe that signed contracts after Q1 2024 but exclude trial accounts

Must be non-empty string. Check for length > 0. If query contains only stop words or is under 10 characters, reject with clarification request

[FACET_SCHEMA]

JSON schema defining available filter fields, their types, allowed values, and supported operators for each field

{"region": {"type": "enum", "values": ["NA", "EMEA", "APAC", "LATAM"]}, "contract_date": {"type": "date", "operators": ["gt", "lt", "range"]}, "account_type": {"type": "enum", "values": ["enterprise", "mid_market", "trial", "partner"]}}

Must be valid JSON. Validate schema parse before prompt assembly. Each field must declare type and either allowed values or operator list. Missing operator declarations cause hallucinated filter syntax

[OPERATOR_PRECEDENCE_RULES]

Explicit rules defining how AND, OR, and NOT operators are nested and evaluated when multiple constraints combine

AND binds tighter than OR. NOT applies to the immediately following group. Parentheses required for OR groups inside AND clauses

Must be non-empty string. Rules must cover AND precedence, OR grouping, NOT scope, and explicit parenthesization requirements. Ambiguous precedence rules cause incorrect filter nesting in production

[OUTPUT_SCHEMA]

Target filter structure specification defining the expected JSON shape for the generated boolean filter

{"type": "object", "properties": {"bool": {"type": "object", "properties": {"must": {"type": "array"}, "should": {"type": "array"}, "must_not": {"type": "array"}, "filter": {"type": "array"}}}}, "required": ["bool"]}

Must be valid JSON Schema. Validate against JSON Schema spec before prompt assembly. Schema must define nesting rules for bool clauses. Missing required fields cause downstream search API rejections

[FIELD_ALIASES]

Mapping of colloquial or abbreviated field names from user queries to canonical schema field names

{"region": ["area", "territory", "geo", "location"], "contract_date": ["signed", "closed", "deal date", "won date"], "account_type": ["segment", "tier", "customer type"]}

Must be valid JSON object. Each canonical field must have non-empty alias array. Missing alias mappings cause extraction failures when users use non-canonical terminology. Test with 5 common synonym variations per field

[CONTRADICTION_RULES]

Instructions for detecting and resolving logically contradictory filter combinations before output generation

If must contain region=NA AND must_not contain region=NA, flag as contradiction. If should contains mutually exclusive enum values, preserve both with warning

Must be non-empty string. Rules must cover same-field contradictions, mutually exclusive enum values, and impossible range combinations. Missing contradiction handling produces filters that return zero results silently

[MAX_NESTING_DEPTH]

Integer limit on how many levels of nested boolean groups the model is allowed to generate

3

Must be positive integer between 1 and 5. Validate type and range before prompt assembly. Excessive nesting depth causes unparseable filters and search engine timeout. Default to 3 if not specified

[UNKNOWN_FIELD_POLICY]

Instruction for how the model should handle query constraints that reference fields not present in the facet schema

Omit unknown fields from output. Include omitted_fields array in response metadata with field names and original query text

Must be one of: omit, error, include_with_flag. Validate enum membership. Policy choice affects downstream behavior: omit silently drops constraints, error stops pipeline, include_with_flag passes through but risks search API rejection

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the combined AND/OR filter logic prompt into a production search pipeline with validation, retries, and safety checks.

The combined AND/OR filter prompt is not a standalone component. It sits between the user's natural language query and your search backend's structured filter API. In a typical RAG or hybrid search pipeline, the user query first hits this prompt to produce a structured filter object, which is then merged with the vector or keyword query before execution. The harness must validate the output against your backend's filter schema before any search runs—an invalid filter clause can silently return zero results or, worse, a full-table scan. Treat this prompt's output as untrusted user input until it passes schema validation, operator whitelisting, and field existence checks.

Start by defining a strict output schema for the filter object. The prompt should return a JSON structure with explicit AND, OR, and NOT grouping, where each leaf node contains a field, operator, and value. Your harness must validate this structure before forwarding it to Elasticsearch, OpenSearch, Pinecone, PostgreSQL, or any custom search API. Implement a validation layer that checks: (1) all field names exist in your search schema, (2) operators are from an allowed list (eq, neq, gt, gte, lt, lte, in, contains, exists), (3) value types match the field's expected type, (4) no contradictory clauses exist (e.g., x > 10 AND x < 5), and (5) nesting depth does not exceed your backend's limit. If validation fails, log the raw prompt output, the validation error, and the original user query for debugging. Do not silently fall back to an unfiltered search—instead, return a controlled error or strip the invalid clause and flag the response with reduced confidence.

For retry logic, implement a single retry with a reformulated prompt that includes the validation error message. The retry prompt should say: 'The previous filter output failed validation with error: [VALIDATION_ERROR]. Original query: [USER_QUERY]. Please correct the filter structure and return only valid JSON.' If the retry also fails, escalate to a human review queue or fall back to a safe default filter (e.g., last 30 days for temporal queries, or no filter with a warning). Log every retry attempt, the validation errors, and the final resolution path. For high-stakes enterprise search where missing documents has compliance implications, require human approval on any filter that modifies access scope or includes exclusion clauses before execution. Model choice matters here: use a model with strong JSON mode and instruction-following for the initial extraction, but consider a smaller, faster model for the retry if latency is critical. Never pass raw LLM output directly to your search backend without this harness layer.

IMPLEMENTATION TABLE

Expected Output Contract

Defines the structured JSON output for the combined AND/OR filter logic prompt. Use this contract to validate the model's response before passing it to your search backend.

Field or ElementType or FormatRequiredValidation Rule

filter

object

Top-level object must contain exactly one of 'must', 'should', 'must_not', or 'filter' keys to define the root boolean operator.

filter.must

array of filter objects

Each element must be a valid filter object. All conditions in this array must match (AND logic). Array must not be empty if present.

filter.should

array of filter objects

Each element must be a valid filter object. At least one condition must match (OR logic). Must include 'minimum_should_match' integer if strict OR semantics are not intended.

filter.must_not

array of filter objects

Each element must be a valid filter object. None of these conditions may match (NOT logic). Array must not be empty if present.

filter.filter

array of filter objects

Each element must be a valid filter object. All conditions must match but do not affect relevance scoring. Array must not be empty if present.

leaf_filter.field

string

Must be a non-empty string matching a known metadata field in the target schema. Reject unknown fields or flag for human review.

leaf_filter.op

string

Must be one of the allowed operators: 'eq', 'neq', 'gt', 'gte', 'lt', 'lte', 'in', 'nin', 'contains', 'starts_with', 'ends_with', 'exists', 'regex'.

leaf_filter.value

string | number | boolean | array | null

Type must be compatible with the specified operator. 'in' and 'nin' require a non-empty array. 'exists' requires a boolean. 'null' is only valid for 'eq' and 'neq'.

PRACTICAL GUARDRAILS

Common Failure Modes

Combined AND/OR filter logic is brittle because natural language is ambiguous about operator precedence, scope, and grouping. These are the most common production failures and how to prevent them.

01

Operator Precedence Ambiguity

What to watch: The model applies AND before OR (or vice versa) in a way that contradicts the user's intended grouping. A query like 'red or blue and large' can mean '(red or blue) and large' or 'red or (blue and large)'. Guardrail: Require the prompt to output explicit nested grouping structures with parentheses. Add a harness check that rejects flat filter lists when multiple operator types are present, forcing the model to commit to a precedence tree.

02

Scope Leakage Across Clauses

What to watch: A negation or facet constraint intended for one part of the query unintentionally applies to another. For example, 'documents from 2023 and not about finance' may incorrectly exclude finance documents from all years. Guardrail: Validate that each filter clause has an unambiguous target field and that NOT clauses are scoped to the correct logical group. Add a test case suite with deliberately ambiguous scope queries to catch regression.

03

Contradictory Filter Generation

What to watch: The model generates mutually exclusive constraints, such as status:active AND status:archived or a date range that contradicts a relative time expression. Guardrail: Implement a post-generation logical consistency check that looks for field-level contradictions (same field with disjoint values in an AND group) and empty range intersections. If a contradiction is detected, escalate to a repair prompt or return a clarification request to the user.

04

Implicit AND Assumption for Multi-Value Facets

What to watch: A user says 'show me documents from engineering and product teams' and the model generates department:engineering AND department:product, which returns zero results because a single document cannot have two departments. The user likely intended OR. Guardrail: For single-valued fields in the schema, enforce a rule that multiple values for the same field must use OR logic. For multi-valued fields, add a harness check that warns when AND is used, prompting a review of whether intersection semantics are truly intended.

05

Deeply Nested Unsupported Structures

What to watch: The model generates a logically correct but overly complex nested boolean structure that the target search backend (e.g., Pinecone, a simple REST API) cannot execute or that causes a timeout. Guardrail: Define a maximum nesting depth in the prompt's output schema (e.g., max 2 levels). Add a post-generation structural validator that flattens or rejects queries exceeding the depth limit before they reach the search API.

06

Empty or Null Clause Generation

What to watch: The model fails to extract a constraint for a mentioned facet and generates an empty AND clause or a null filter, which either crashes the search API or silently returns unfiltered results. Guardrail: Add a strict output schema check that rejects any filter object containing empty arrays, null values, or empty string clauses. The harness should treat a malformed filter as a generation failure and trigger a retry or a safe fallback to a broader query.

IMPLEMENTATION TABLE

Evaluation Rubric

Use these criteria to test the logical correctness and production readiness of generated AND/OR filter structures before integrating them into a search API.

CriterionPass StandardFailure SignalTest Method

Operator Precedence

AND groups are nested inside OR groups correctly per the input query's implied logic

A flat list of filters ignores explicit grouping keywords like 'either' or 'both'

Parse the output JSON; verify that AND conditions are objects inside an OR array when the input contains 'either A and B or C'

Logical Contradiction

No conflicting constraints exist for the same field (e.g., status:active AND status:inactive)

The output contains mutually exclusive values for a single field within the same AND group

Scan all filter objects within a single must/bool clause; flag any field with multiple values that cannot coexist

Field Schema Adherence

All generated filter field names match the provided [ALLOWED_FIELDS] list exactly

An extracted constraint uses a field name not present in the schema, or a typo variant

Diff the unique field names in the output against the allowed schema list; fail on any mismatch

Operator Validity

Operators used (eq, neq, gt, lt, gte, lte, in, nin) are valid for the field's data type

A range operator (gt) is applied to a string field, or an 'in' operator is used on a numeric field

Validate each filter clause against a provided field-type map; flag type-operator mismatches

Value Type Coercion

Filter values match the expected data type (string, integer, float, boolean, ISO date)

A date filter contains 'last week' instead of an ISO 8601 date string, or a numeric field receives a string

Assert JSON type of each filter's value field; check string fields for unresolved relative expressions

Nesting Depth Limit

Generated boolean logic does not exceed a depth of 3 nested levels

Output contains deeply nested, unreadable boolean structures that will degrade query engine performance

Recursively traverse the output JSON; count the maximum depth of nested must/should objects; fail if > 3

Empty Group Handling

Empty AND/OR groups are removed or never generated; all logical branches contain at least one constraint

The output includes an empty 'must' or 'should' array, which would match all documents

Assert that every boolean logic array in the output has a length greater than zero

Low-Confidence Exclusion

Constraints with a confidence score below the configured [CONFIDENCE_THRESHOLD] are excluded from the final output

A filter clause with a confidence score of 0.4 is included when the threshold is set to 0.7

Inject a test query with an ambiguous term; assert that the generated filter for that term is absent from the output

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Use the base prompt with a simple JSON schema constraint. Focus on getting the logical structure right before adding backend-specific syntax.

code
[CONSTRAINTS]: Return ONLY a valid JSON object with "operator" (AND/OR/NOT), "conditions" array, and optional "nested" filters. Do not include explanations.

Watch for

  • Operator precedence errors when queries mix AND and OR without explicit nesting
  • Missing NOT scope boundaries (what exactly is being excluded)
  • Overly flat filter structures that should be nested
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.