SetBERT: Enhancing Retrieval Performance for Boolean Logic and Set Operation Queries

Quan Mai, Susan Gauch, Douglas Adams · DOI

Abstract

Modern neural retrieval systems, while proficient at capturing semantic topics, often fail to respect basic Boolean logic. This paper introduces SetBERT, a model fine-tuned to bridge this "logic gap" using a novel inversed-contrastive loss and synthetic training data. SetBERT-base demonstrates significant performance gains, achieving up to a 63% improvement in recall over standard BERT models and allowing compact architectures to rival the performance of models three times their size.

The Challenge of Boolean Logic in Neural Retrieval

Despite the advancements in Large Language Models (LLMs), standard neural retrieval systems frequently treat functional words like "and," "or," and "not" as ignorable noise. This leads to significant retrieval errors when users provide specific logical constraints. For instance, a query for "Vietnam movies not about war" often returns combat films because the model prioritizes the topic ("war") over the exclusion operator ("not"). Similarly, "AND" queries often return documents satisfying only a single condition rather than the required intersection.

This failure stems from the way standard embeddings are trained; they prioritize topical similarity over logical structure. SetBERT addresses this by making neural embeddings "logic-aware," ensuring they respect complex set operations and user-defined constraints.

Methodology: Synthetic Data and Inversed-Contrastive Loss

To overcome the scarcity of high-quality Boolean query datasets, the researchers utilized GPT-3.5-turbo to generate a training set of 150,000 samples. These samples were balanced across AND, OR, and NOT operations. Each training instance includes a "gold" query, positive matches (paraphrases), and "hard negatives"—sentences that appear topically relevant but violate the logical constraint.

Figure 1: Examples of positive and negative matches for a Boolean query requiring both action and humor. The model must learn to reject 'hard negatives' that satisfy only one part of the logic.

The Inversed-Contrastive Loss

A critical innovation in SetBERT is the move away from standard triplet loss. In traditional contrastive learning, "A" and "NOT A" are pushed so far apart that the model loses their shared topical context (e.g., both being about "Trees"). SetBERT employs an inversed-contrastive loss that penalizes logical errors—specifically pushing away hard negatives—without destroying the underlying semantic relationship between related concepts. This balance allows the model to recognize that "Trees in the Amazon" and "Trees NOT in the Amazon" share a topic while remaining logically distinct.

Experimental Results

SetBERT was evaluated using the QUEST dataset, a benchmark for entity-seeking queries with implicit set operations derived from Wikipedia. The results demonstrate that logic-aware training significantly enhances retrieval effectiveness.

Figure 3: Average Recall@k and MRecall@k across different retrievers. SetBERT-Base significantly outperforms standard BERT-Base and approaches the performance of the much larger BERT-Large model.

Key Performance Metrics

  • Massive Recall Gains: SetBERT-base achieved an Average Recall@100 of 0.296, a 63% improvement over the standard BERT-base score of 0.182.
  • Efficiency and Scale: One of the most striking findings is that a logic-tuned BERT-base (110M parameters) can match or exceed the performance of a standard BERT-large (336M parameters). SetBERT-base reached a Recall@1000 of 0.612, nearly identical to BERT-large’s 0.627.
  • Template Sensitivity: The model performed exceptionally well on "OR" templates, reaching a Recall@1000 of 0.673, far surpassing the BM25 baseline of 0.535. Conversely, the researchers found that using standard triplet loss (SetBERT-L-TL) led to a marked decrease in performance across nearly all logical structures.

Discussion and Future Work

SetBERT proves that specialized training objectives can compensate for model size, offering a path toward more efficient and precise retrieval systems. However, several avenues for future research remain. This study focused on the initial retrieval phase; future work should investigate SetBERT’s role in multi-stage re-ranking pipelines. Additionally, while the model was tested on complex structures like "A AND B NOT C," the training data was limited to single operations. Expanding the training set to include nested logical operations and applying these techniques to multi-modal retrieval (e.g., image search with exclusions) could further enhance the utility of logic-aware embeddings.

Summary updated

Abstract

We introduce SetBERT, a fine-tuned BERT-based model designed to enhance query embeddings for set operations and Boolean logic queries, such as Intersection (AND), Difference (NOT), and Union (OR). SetBERT significantly improves retrieval performance for logic-structured queries, an area where both traditional and neural retrieval methods typically underperform. We propose an innovative use of inversed-contrastive loss, focusing on identifying the negative sentence, and fine-tuning BERT with a dataset generated via prompt GPT. Furthermore, we demonstrate that, unlike other BERT-based models, fine-tuning with triplet loss actually degrades performance for this specific task. Our experiments reveal that SetBERT-base not only significantly outperforms BERT-base (up to a 63% improvement in Recall) but also achieves performance comparable to the much larger BERT-large model, despite being only one-third the size.

Empirical ResultsBrowse all benchmarks →

BenchmarkMethodMetricScoreProtocol / Evidence
QUESTSetBERT-Base DEAve. Recall@1000.296+0.11 vs baselineImage Retrieval · k=100 · encoder=dual-encoder · backbone=BERT-Base
Source quote
L124: SetBERT-Base DE │ \textcolor{blue}{0.139} │ \textcolor{blue}{0.220} │ \textcolor{blue}{0.296} │ \textcolor{blue}{0.612} │ 0.016 │ 0.032 │ 0.050 │ 0.207
QUESTSetBERT-Base DEAve. Recall@10000.612Image Retrieval · k=1000 · encoder=dual-encoder · backbone=BERT-Base
Source quote
L124: SetBERT-Base DE │ \textcolor{blue}{0.139} │ \textcolor{blue}{0.220} │ \textcolor{blue}{0.296} │ \textcolor{blue}{0.612} │ 0.016 │ 0.032 │ 0.050 │ 0.207
QUESTSetBERT-L-TLRecall@10000.493Image Retrieval · k=1000 · loss=Triple Loss · backbone=BERT-Large · query-template=intersection (A and B)
Source quote
L162: SetBERT-L-TL generally underperforms compared to both BERT-Large and SetBERT-Large, especilly for the intersection templates. In the ``A and B'' template, SetBERT-L-TL records 0.493, trailing behind BERT-L's 0.613 and SetBERT-L's 0.636 by 19.6% and 22.5%, respectively. This trend continues across other templates, with SetBERT-L-TL showing notable performance drops, particularly in complex conjunctive queries like ``A and B and C,'' where it scores 0.547, compared to BERT-L's 0.640 and SetBERT-L's 0.667. Additionally, SetBERT-L-TL underperforms the SetBERT Base configuration, which has consistently higher scores, such as 0.639 in the ``A'' template and 0.673 in the ``A or B'' template. These results suggest that the triple loss training approach may hinder SetBERT-L-TL's effectiveness, particularly in handling complex query structures, making it less efficient than both its large and base counterparts.
QUESTSetBERT-LRecall@10000.636+0.01 vs baselineImage Retrieval · k=1000 · backbone=BERT-Large · query-template=intersection (A and B)
Source quote
L162: SetBERT-L-TL generally underperforms compared to both BERT-Large and SetBERT-Large, especilly for the intersection templates. In the ``A and B'' template, SetBERT-L-TL records 0.493, trailing behind BERT-L's 0.613 and SetBERT-L's 0.636 by 19.6% and 22.5%, respectively. This trend continues across other templates, with SetBERT-L-TL showing notable performance drops, particularly in complex conjunctive queries like ``A and B and C,'' where it scores 0.547, compared to BERT-L's 0.640 and SetBERT-L's 0.667. Additionally, SetBERT-L-TL underperforms the SetBERT Base configuration, which has consistently higher scores, such as 0.639 in the ``A'' template and 0.673 in the ``A or B'' template. These results suggest that the triple loss training approach may hinder SetBERT-L-TL's effectiveness, particularly in handling complex query structures, making it less efficient than both its large and base counterparts.
Reported baseline / comparator measurements (2)
BenchmarkMethodMetricScoreProtocol / Evidence
QUESTBERT-Base DE baselineAve. Recall@1000.182Image Retrieval · backbone=BERT-Base · k=100 · encoder=dual-encoder
Source quote
L123: BERT-Base DE │ 0.082 │ 0.134 │ 0.182 │ 0.453 │ 0.006 │ 0.011 │ 0.017 │ 0.086
QUESTBERT-Large DE baselineAve. Recall@10000.627Image Retrieval · backbone=BERT-Large · k=1000 · encoder=dual-encoder
Source quote
L125: BERT-Large DE │ 0.146 │ 0.227 │ 0.300 │ 0.627 │ 0.021 │ 0.041 │ 0.062 │ 0.235
BibTeX
@inproceedings{mai2024setbert,
  author = {Quan Mai and Susan Gauch and Douglas Adams},
  title = {{SetBERT: Enhancing Retrieval Performance for Boolean Logic and Set Operation Queries}},
  booktitle = {NLPIR},
  year = {2024},
  doi = {10.1145/3711542.3711548},
}
Figure