Azure AI 搜尋中的 OData 邏輯運算子 - andornot

Azure AI 搜尋中的 OData 篩選運算式 是評估為 truefalse 的布林運算式。 您可以撰寫一系列 較簡單的篩選,並使用布林代數 的邏輯運算子 來撰寫複雜的篩選 :

  • and:如果其左右子運算式評估為 true ,則為 的二進位運算子 true
  • or:如果其中一個左右子運算式評估為 ,則會評估 truetrue 的二進位運算子。
  • not:如果子運算式評估為 ,則評估 truefalse 的一元運算子,反之亦然。

這些集合運算子 anyall 可讓您建構可表達非常複雜搜尋準則的篩選。

語法

下列 EBNF ( Extended Backus-Naur Form ) 會定義使用邏輯運算子的 OData 運算式文法。

logical_expression ::=
    boolean_expression ('and' | 'or') boolean_expression
    | 'not' boolean_expression

您也可以使用互動式語法圖表:

注意

如需完整的 EBNF,請參閱 Azure AI 搜尋 的 OData 運算式語法參考。

邏輯運算式有兩種形式:binary ( and/or ),其中有兩個子運算式,而一元運算式 not 則只有一個。 子運算式可以是任何類型的布林運算式:

  • 類型的欄位或範圍變數 Edm.Boolean
  • 傳回 類型 Edm.Boolean 值的函式,例如 geo.intersectssearch.ismatch
  • 比較運算式 ,例如 rating gt 4
  • 集合運算式 ,例如 Rooms/any(room: room/Type eq 'Deluxe Room')
  • 布林常值 truefalse
  • 使用 andornot 建構的其他邏輯運算式。

重要

在某些情況下,並非所有子運算式都可以搭配 and/or 使用,特別是在 Lambda 運算式內。 如需詳細資訊,請參閱 Azure AI 搜尋 中的 OData 集合運算子。

邏輯運算子和 null

大部分的布林運算式,例如函式和比較都無法產生 null 值,而且邏輯運算子無法直接套用至 null 常值(例如 x and null 不允許)。 不過,布林值欄位可以是 null ,因此您必須知道 、 ornot 運算子在 Null 存在時的行為 and 。 下表摘要說明,其中 b 是 類型的 Edm.Boolean 欄位:

運算式 當 為 時 b 的結果 null
b false
not b true
b eq true false
b eq false false
b eq null true
b ne true true
b ne false true
b ne null false
b and true false
b and false false
b or true true
b or false false

當布林值欄位 b 本身出現在篩選運算式中時,其行為就如同已寫入 b eq true ,因此如果 bnull ,則運算式會評估為 false 。 同樣地, not b 其行為類似 not (b eq true) ,因此它會評估為 true 。 如此一來, null 欄位的行為會與 false 相同。 這與使用 和 or 與其他運算式 and 結合時的行為一致,如上表所示。 儘管如此,與 ( b eq false ) 的直接比較 false 仍會評估為 false 。 換句話說, null 不等於 false ,即使它在布林運算式中的行為就像它一樣。

範例

比對 rating 欄位介於 3 到 5 之間的檔,包括:

    rating ge 3 and rating le 5

比對欄位的所有元素 ratings 小於 3 或大於 5 的檔:

    ratings/all(r: r lt 3 or r gt 5)

比對欄位位於指定多邊形內的檔 location ,而且檔不包含「public」 一詞。

    geo.intersects(location, geography'POLYGON((-122.031577 47.578581, -122.031577 47.678581, -122.131577 47.678581, -122.031577 47.578581))') and not search.ismatch('public')

比對加拿大溫哥華酒店的檔,那裡有一個豪華房間,基本費率低於160:

    Address/City eq 'Vancouver' and Address/Country eq 'Canada' and Rooms/any(room: room/Type eq 'Deluxe Room' and room/BaseRate lt 160)

下一步