def to_lucene_query(base_query: str,
                    adjustments: Optional[List[QueryAdjustment]] = None,
                    escape_query: bool = True) -> str:
  """Builds a valid lucene query from a base query, query adjustments, and
  vocabulary mappings.

  Args:
    base_query: str, The base query which is prepended to the new query.
    adjustments: List[QueryAdjustment], List of all the adjustmenst
      (=reformulations) applied to the base query. The terms in the adjustments
      can be templates or words directly.
    escape_query:  If True, escapes `base_query`.  Needed for Lucene, not needed
      for running against WebSearch.

  Returns:
    A valid and escaped lucene query.
  """
  if not adjustments:
    adjustments = []

  escape_fn = {
      True: utils.escape_for_lucene,
      False: lambda x: x,
  }[escape_query]

  lucene_query = escape_fn(base_query)

  for adjustment in adjustments:
    term = adjustment.term
    # `term` needs to be properly escaped if it contains any special characters.
    term = utils.escape_for_lucene(term)

    field = adjustment.field.value.strip('[]')
    if adjustment.operator == Operator.MINUS:
      lucene_query += f' -({field}:"{term}")'
    elif adjustment.operator == Operator.PLUS:
      lucene_query += f' +({field}:"{term}")'
    elif adjustment.operator == Operator.APPEND:
      if adjustment.field == Field.ALL:
        lucene_query += f' {term}'
      else:
        lucene_query += f' ({field}:"{term}")'

  return lucene_query
Builds a valid lucene query from a base query, query adjustments, and
  vocabulary mappings.

  Args:
    base_query: str, The base query which is prepended to the new query.
    adjustments: List[QueryAdjustment], List of all the adjustmenst
      (=reformulations) applied to the base query. The terms in the adjustments
      can be templates or words directly.
    escape_query:  If True, escapes `base_query`.  Needed for Lucene, not needed
      for running against WebSearch.

  Returns:
    A valid and escaped lucene query.
  
language/search_agents/muzero/state_tree.py