共用方式為


完整 Lucene 搜尋語法 (進階查詢) 的範例

在建構 Azure AI 搜尋服務的查詢時,您可以將預設的簡單查詢剖析器取代為功能更強的 Lucene 查詢剖析器,以編寫特製化的進階查詢運算式。

Lucene 剖析器支援複雜的查詢建構,例如欄位範圍查詢、模糊搜尋、中置和尾碼萬用字元搜尋、鄰近搜尋、詞彙提升,以及規則運算式搜尋。 額外的處理需求需要更多能源,因此,您應該預期執行時間會略久。 在本文中,您可以逐步執行範例,示範以完整語法為基礎的查詢作業。

附註

許多透過完整 Lucene 查詢語法來啟用的特製化查詢建構都不是文字分析,因此如果您預期的是詞幹分析或詞形歸併還原,可能會感到意外。 只能對完整詞彙執行語彙分析 (詞彙查詢或片語查詢)。 不完整詞彙的查詢類型 (前置詞查詢、萬用字元查詢、Regex 查詢、模糊查詢) 會直接新增至查詢樹狀結構,並略過分析階段。 只能對部分的查詢詞彙執行小寫轉換。

飯店範例索引

下列查詢是以 hotels-sample-index 為基礎,您可以遵循本快速入門中的指示來建立。

範例查詢會使用 REST API 和 POST 要求來表達。 您可以在 REST 用戶端貼上並加以執行。 在 Azure 入口網站中使用搜尋總管的 JSON 檢視。 在 JSON 檢視中,您可以貼上本文此處所示的查詢範例。

要求標頭必須具有下列值:

Key
Content-Type application/json
API金鑰 <your-search-service-api-key> (查詢或系統管理金鑰)

URI 參數必須包含具有索引名稱、文件集合、搜尋命令和 API 版本的搜尋服務端點,類似於下列範例:

https://{{service-name}}.search.windows.net/indexes/hotels-sample-index/docs/search?api-version=2025-09-01

要求本文應格式化為有效的 JSON:

{
    "search": "*",
    "queryType": "full",
    "select": "HotelId, HotelName, Category, Tags, Description",
    "count": true
}
  • 設定為 * 的 search 是未指定的查詢,相當於 Null 或空白搜尋。 這並不特別有用,但這是您可以執行的最簡單搜尋,而且其會顯示索引中所有可擷取的欄位,以及所有值。

  • 設定為 fullqueryType 會叫用完整的 Lucene 查詢剖析器,且其為此語法的必要項目。

  • 設定為以逗號分隔的欄位清單的 select 用於搜尋結果組合,只包含搜尋結果內容中有用的欄位。

  • count 會傳回符合搜尋準則的文件數目。 在空的搜尋字串上,此計數將會是索引中的所有文件 (在 hotels-sample-index 中,計數為 50)。

欄位搜尋涉及特定欄位的個別內嵌搜尋運算式。 本範例會搜尋具有 hotel 一詞的飯店名稱,但不會搜尋 motel。 您可以使用 AND 指定多個欄位。

使用此查詢語法時,若想要查詢的欄位位於搜尋運算式本身,您可以省略 searchFields 參數。 如果您包含具有欄位搜尋的 searchFields,則 fieldName:searchExpression 一律優先於 searchFields

POST /indexes/hotel-samples-index/docs/search?api-version=2025-09-01
{
    "search": "HotelName:(hotel NOT motel) AND Category:'Boutique'",
    "queryType": "full",
    "select": "HotelName, Category",
    "count": true
}

此查詢的回應看起來應該類似下列範例,根據 Boutique 篩選,傳回名稱中包含 hotel 的飯店,同時排除名稱中包含 motel 的結果。

{
  "@odata.count": 5,
  "value": [
    {
      "@search.score": 2.2289815,
      "HotelName": "Stay-Kay City Hotel",
      "Category": "Boutique"
    },
    {
      "@search.score": 1.3862944,
      "HotelName": "City Skyline Antiquity Hotel",
      "Category": "Boutique"
    },
    {
      "@search.score": 1.355046,
      "HotelName": "Old Century Hotel",
      "Category": "Boutique"
    },
    {
      "@search.score": 1.355046,
      "HotelName": "Sublime Palace Hotel",
      "Category": "Boutique"
    },
    {
      "@search.score": 1.355046,
      "HotelName": "Red Tide Hotel",
      "Category": "Boutique"
    }
  ]
}

搜尋運算式可以是單一字詞或片語,或是以括弧括住的更複雜運算式,選擇性地搭配布林運算子。 部分範例如下:

  • HotelName:(hotel NOT motel)
  • Address/StateProvince:("WA" OR "CA")
  • Tags:("free wifi" NOT "free parking") AND "coffee in lobby"

如果您想要將這兩個字串評估為單一實體評估,請務必將片語放在引號內,例如搜尋 Address/StateProvince 欄位中兩個不同位置的情況。 根據用戶端,您可能需要逸出 (\) 引號。

fieldName:searchExpression 中指定的欄位必須是可搜尋欄位。 若要了解如何將欄位定義屬性化,請參閱建立索引 (REST API)

模糊搜尋會比對類似字詞,包括拼錯的字組。 若要執行模糊搜尋,請在單一文字結尾附加波狀符號 ~,加上選擇性參數 (介於 0 和 2 之間且會指定編輯距離的值)。 比方說,blue~blue~1 會傳回 blue、blues 和 glue。

POST /indexes/hotel-samples-index/docs/search?api-version=2025-09-01
{
    "search": "Tags:conserge~",
    "queryType": "full",
    "select": "HotelName, Category, Tags",
    "searchFields": "HotelName, Category, Tags",
    "count": true
}

此查詢的回應會解析為比對文件中的指引,為了簡潔起見而修剪:

{
  "@odata.count": 9,
  "value": [
    {
      "@search.score": 1.4947624,
      "HotelName": "Twin Vortex Hotel",
      "Category": "Luxury",
      "Tags": [
        "bar",
        "restaurant",
        "concierge"
      ]
    },
    {
      "@search.score": 1.1685618,
      "HotelName": "Stay-Kay City Hotel",
      "Category": "Boutique",
      "Tags": [
        "view",
        "air conditioning",
        "concierge"
      ]
    },
    {
      "@search.score": 1.1465473,
      "HotelName": "Old Century Hotel",
      "Category": "Boutique",
      "Tags": [
        "pool",
        "free wifi",
        "concierge"
      ]
    },
. . .
  ]
}

不直接支援片語,但您可以在多部分片語的每個字詞上指定模糊比對,例如 search=Tags:landy~ AND sevic~。 此查詢運算式找到 15 個關於 laundry service 的相符項目。

附註

模糊查詢不會進行分析。 不完整詞彙的查詢類型 (前置詞查詢、萬用字元查詢、Regex 查詢、模糊查詢) 會直接新增至查詢樹狀結構,並略過分析階段。 對部分查詢字詞執行的唯一轉換會轉換成小寫。

鄰近搜尋會尋找文件中彼此相近的字詞。 在片語的結尾插入波狀符號「~」,後面加上建立鄰近界限的字數。

此查詢會在文件中搜尋彼此相隔 5 個字以內的 hotelairport 字詞。 引號會逸出 (\") 以保留片語:

POST /indexes/hotel-samples-index/docs/search?api-version=2025-09-01
{
    "search": "Description: \"hotel airport\"~5",
    "queryType": "full",
    "select": "HotelName, Description",
    "searchFields": "HotelName, Description",
    "count": true
}

此查詢的回應應該看起來類似下列範例:

{
  "@odata.count": 1,
  "value": [
    {
      "@search.score": 0.69167054,
      "HotelName": "Trails End Motel",
      "Description": "Only 8 miles from Downtown. On-site bar/restaurant, Free hot breakfast buffet, Free wireless internet, All non-smoking hotel. Only 15 miles from airport."
    }
  ]
}

範例 4:詞彙提升

字詞提升指的是如果某個文件包含提升的字詞,便會比未包含該字詞的文件獲得更高的順位。 若要提升字詞,請使用插入號 ^,並在搜尋字詞的結尾加上提升係數 (數字)。 提升因數預設值為 1,雖然其必須是正數,但可能小於 1 (例如,0.2)。 詞彙提升與評分設定檔的不同之處在於,評分設定檔會提升特定欄位,而不是特定字詞。

在這個 before 查詢中,搜尋 beach access,並注意有六份文件符合一或兩個字詞。

POST /indexes/hotel-samples-index/docs/search?api-version=2025-09-01
{
    "search": "beach access",
    "queryType": "full",
    "select": "HotelName, Description, Tags",
    "searchFields": "HotelName, Description, Tags",
    "count": true
}

事實上,只有兩份文件符合 access。 第一個實例位於第二個位置,即使文件遺漏了 beach 一詞。

{
  "@odata.count": 6,
  "value": [
    {
      "@search.score": 1.068669,
      "HotelName": "Johnson's Family Resort",
      "Description": "Family oriented resort located in the heart of the northland. Operated since 1962 by the Smith family, we have grown into one of the largest family resorts in the state. The home of excellent Smallmouth Bass fishing with 10 small cabins, we're a home not only to fishermen but their families as well. Rebuilt in the early 2000's, all of our cabins have all the comforts of home. Sporting a huge **beach** with multiple water toys for those sunny summer days and a Lodge full of games for when you just can't swim anymore, there's always something for the family to do. A full marina offers watercraft rentals, boat launch, powered dock slips, canoes (free to use), & fish cleaning facility. Rent pontoons, 14' fishing boats, 16' fishing rigs or jet ski's for a fun day or week on the water. Pets are accepted in the lakeside cottages.",
      "Tags": [
        "24-hour front desk service",
        "pool",
        "coffee in lobby"
      ]
    },
    {
      "@search.score": 1.0162708,
      "HotelName": "Campus Commander Hotel",
      "Description": "Easy **access** to campus and steps away from the best shopping corridor in the city. From meetings in town or gameday, enjoy our prime location between the union and proximity to the university stadium.",
      "Tags": [
        "free parking",
        "coffee in lobby",
        "24-hour front desk service"
      ]
    },
    {
      "@search.score": 0.9050383,
      "HotelName": "Lakeside B & B",
      "Description": "Nature is Home on the **beach**. Explore the shore by day, and then come home to our shared living space to relax around a stone fireplace, sip something warm, and explore the library by night. Save up to 30 percent. Valid Now through the end of the year. Restrictions and blackouts may apply.",
      "Tags": [
        "laundry service",
        "concierge",
        "free parking"
      ]
    },
    {
      "@search.score": 0.8955848,
      "HotelName": "Windy Ocean Motel",
      "Description": "Oceanfront hotel overlooking the **beach** features rooms with a private balcony and 2 indoor and outdoor pools. Inspired by the natural beauty of the island, each room includes an original painting of local scenes by the owner. Rooms include a mini fridge, Keurig coffee maker, and flatscreen TV. Various shops and art entertainment are on the boardwalk, just steps away.",
      "Tags": [
        "pool",
        "air conditioning",
        "bar"
      ]
    },
    {
      "@search.score": 0.83636594,
      "HotelName": "Happy Lake Resort & Restaurant",
      "Description": "The largest year-round resort in the area offering more of everything for your vacation – at the best value! What can you enjoy while at the resort, aside from the mile-long sandy **beaches** of the lake? Check out our activities sure to excite both young and young-at-heart guests. We have it all, including being named “Property of the Year” and a “Top Ten Resort” by top publications.",
      "Tags": [
        "pool",
        "bar",
        "restaurant"
      ]
    },
    {
      "@search.score": 0.7808502,
      "HotelName": "Swirling Currents Hotel",
      "Description": "Spacious rooms, glamorous suites and residences, rooftop pool, walking **access** to shopping, dining, entertainment and the city center. Each room comes equipped with a microwave, a coffee maker and a minifridge. In-room entertainment includes complimentary W-Fi and flat-screen TVs. ",
      "Tags": [
        "air conditioning",
        "laundry service",
        "24-hour front desk service"
      ]
    }
  ]
}

after 查詢中,重複搜尋,這次利用 beach 一詞蓋過 access 一詞來提升結果。 查詢的人類可讀版本為 search=Description:beach^2 access。 根據您的用戶端,您可能需要將 ^2 表示為 %5E2

POST /indexes/hotel-samples-index/docs/search?api-version=2025-09-01
{
    "search": "Description:beach^2 access",
    "queryType": "full",
    "select": "HotelName, Description, Tags",
    "searchFields": "HotelName, Description, Tags",
    "count": true
}

當你提升 beach 一詞後,關於 Campus Commander Hotel 的相符項目將下降到第五個位置。

{
  "@odata.count": 6,
  "value": [
    {
      "@search.score": 2.137338,
      "HotelName": "Johnson's Family Resort",
      "Description": "Family oriented resort located in the heart of the northland. Operated since 1962 by the Smith family, we have grown into one of the largest family resorts in the state. The home of excellent Smallmouth Bass fishing with 10 small cabins, we're a home not only to fishermen but their families as well. Rebuilt in the early 2000's, all of our cabins have all the comforts of home. Sporting a huge beach with multiple water toys for those sunny summer days and a Lodge full of games for when you just can't swim anymore, there's always something for the family to do. A full marina offers watercraft rentals, boat launch, powered dock slips, canoes (free to use), & fish cleaning facility. Rent pontoons, 14' fishing boats, 16' fishing rigs or jet ski's for a fun day or week on the water. Pets are accepted in the lakeside cottages.",
      "Tags": [
        "24-hour front desk service",
        "pool",
        "coffee in lobby"
      ]
    },
    {
      "@search.score": 1.8100766,
      "HotelName": "Lakeside B & B",
      "Description": "Nature is Home on the beach. Explore the shore by day, and then come home to our shared living space to relax around a stone fireplace, sip something warm, and explore the library by night. Save up to 30 percent. Valid Now through the end of the year. Restrictions and blackouts may apply.",
      "Tags": [
        "laundry service",
        "concierge",
        "free parking"
      ]
    },
    {
      "@search.score": 1.7911696,
      "HotelName": "Windy Ocean Motel",
      "Description": "Oceanfront hotel overlooking the beach features rooms with a private balcony and 2 indoor and outdoor pools. Inspired by the natural beauty of the island, each room includes an original painting of local scenes by the owner. Rooms include a mini fridge, Keurig coffee maker, and flatscreen TV. Various shops and art entertainment are on the boardwalk, just steps away.",
      "Tags": [
        "pool",
        "air conditioning",
        "bar"
      ]
    },
    {
      "@search.score": 1.6727319,
      "HotelName": "Happy Lake Resort & Restaurant",
      "Description": "The largest year-round resort in the area offering more of everything for your vacation – at the best value! What can you enjoy while at the resort, aside from the mile-long sandy beaches of the lake? Check out our activities sure to excite both young and young-at-heart guests. We have it all, including being named “Property of the Year” and a “Top Ten Resort” by top publications.",
      "Tags": [
        "pool",
        "bar",
        "restaurant"
      ]
    },
    {
      "@search.score": 1.0162708,
      "HotelName": "Campus Commander Hotel",
      "Description": "Easy access to campus and steps away from the best shopping corridor in the city. From meetings in town or gameday, enjoy our prime location between the union and proximity to the university stadium.",
      "Tags": [
        "free parking",
        "coffee in lobby",
        "24-hour front desk service"
      ]
    },
    {
      "@search.score": 0.7808502,
      "HotelName": "Swirling Currents Hotel",
      "Description": "Spacious rooms, glamorous suites and residences, rooftop pool, walking access to shopping, dining, entertainment and the city center. Each room comes equipped with a microwave, a coffee maker and a minifridge. In-room entertainment includes complimentary W-Fi and flat-screen TVs. ",
      "Tags": [
        "air conditioning",
        "laundry service",
        "24-hour front desk service"
      ]
    }
  ]
}

範例 5:Regex

規則運算式搜尋會根據正斜線 / 與小寫字串之間的內容尋找相符項目,如 RegExp 類別中所記錄。

POST /indexes/hotel-samples-index/docs/search?api-version=2025-09-01
{
    "search": "HotelName:/(Mo|Ho)tel/",
    "queryType": "full",
    "select": "HotelName",
    "count": true
}

查詢的回應看起來應該類似下列範例 (為了簡潔起見,我們進行刪減):

{
  "@odata.count": 25,
  "value": [
    {
      "@search.score": 1,
      "HotelName": "Country Residence Hotel"
    },
    {
      "@search.score": 1,
      "HotelName": "Downtown Mix Hotel"
    },
    {
      "@search.score": 1,
      "HotelName": "Gastronomic Landscape Hotel"
    },
    . . . 
    {
      "@search.score": 1,
      "HotelName": "Trails End Motel"
    },
    {
      "@search.score": 1,
      "HotelName": "Nordick's Valley Motel"
    },
    {
      "@search.score": 1,
      "HotelName": "King's Cellar Hotel"
    }
  ]
}

附註

RegEx 查詢不會進行分析。 對部分查詢字詞執行的唯一轉換會轉換成小寫。

您可以使用一般辨識語法來進行多字元 (*) 或單一字元 (?) 的萬用字元搜尋。 Lucene 查詢剖析器支援搭配使用這些符號與單一詞彙,而不是片語。

在此查詢中,搜尋包含前置詞 sc 的飯店名稱。 您無法使用 *? 符號作為搜尋的第一個字元。

POST /indexes/hotel-samples-index/docs/search?api-version=2025-09-01
{
    "search": "HotelName:sc*",
    "queryType": "full",
    "select": "HotelName",
    "count": true
}

此查詢的回應應該看起來類似下列範例:

{
  "@odata.count": 1,
  "value": [
    {
      "@search.score": 1,
      "HotelName": "Waterfront Scottish Inn"
    }
  ]
}

附註

萬用字元查詢不會進行分析。 對部分查詢字詞執行的唯一轉換會轉換成小寫。

嘗試在程式碼中指定查詢。 下列連結說明如何使用 Azure SDK 設定搜尋查詢。

您可以在下列文章中找到其他語法參考、查詢架構和範例: