完整 Lucene 搜尋語法的範例 (進階查詢)
在建構 Azure AI 搜尋服務的查詢時,您可以將預設的簡單查詢剖析器取代為功能更強的 Lucene 查詢剖析器,以編寫特製化的進階查詢運算式。
Lucene 剖析器支援複雜的查詢建構,例如欄位範圍查詢、模糊搜尋、中置和尾碼萬用字元搜尋、鄰近搜尋、詞彙提升,以及規則運算式搜尋。 額外的處理需求需要更多能源,因此,您應該預期執行時間會略久。 在本文中,您可以逐步執行範例,以完整語法示範查詢作業。
注意
許多透過完整 Lucene 查詢語法來啟用的特製化查詢建構都不是文字分析,因此如果您預期的是詞幹分析或詞形歸併還原,可能會感到意外。 只能對完整詞彙執行語彙分析 (詞彙查詢或片語查詢)。 不完整詞彙的查詢類型 (前置詞查詢、萬用字元查詢、Regex 查詢、模糊查詢) 會直接新增至查詢樹狀結構,並略過分析階段。 只能對部分的查詢詞彙執行小寫轉換。
飯店範例索引
下列查詢是以 hotels-sample-index 為基礎,您可以遵循本快速入門中的指示來建立。
範例查詢會使用 REST API 和 POST 要求來表達。 您可以在 REST 用戶端貼上並加以執行。 在 Azure 入口網站中使用搜尋總管的 JSON 檢視。 在 JSON 檢視中,您可以貼上本文此處所示的查詢範例。
要求標頭必須具有下列值:
機碼 | 值 |
---|---|
內容-類型 | application/json |
api-key | <your-search-service-api-key> (查詢或系統管理金鑰) |
URI 參數必須包含具有索引名稱、文件集合、搜尋命令和 API 版本的搜尋服務端點,類似於下列範例:
https://{{service-name}}.search.windows.net/indexes/hotels-sample-index/docs/search?api-version=2024-07-01
要求本文應形成為有效的 JSON:
{
"search": "*",
"queryType": "full",
"select": "HotelId, HotelName, Category, Tags, Description",
"count": true
}
search
設定為 * 是未指定的查詢,相當於 Null 或空白搜尋。 這並不特別有用,但這是您可以執行的最簡單搜尋,而且其會顯示索引中所有可擷取的欄位,以及所有值。queryType
設定為 full 會叫用完整的 Lucene 查詢剖析器,而且此語法是必要的。select
設定為逗號分隔的欄位清單會用於搜尋結果組合,只包含那些在搜尋結果內容中很有用的欄位。count
會傳回符合搜尋準則的檔數目。 在空的搜尋字串上,此計數將會是索引中的所有文件 (在 hotels-sample-index 中,計數為 50)。
範例 1:欄位搜尋
欄位搜尋範圍是個別的內嵌搜尋表達式至特定欄位。 本範例會搜尋旅館名稱與其中一個字詞 旅館 ,但不會 搜尋旅館名稱。 您可以使用 指定多個字段 AND
。
使用此查詢語法時,若想要查詢的欄位位於搜尋運算式本身,您可以省略 searchFields
參數。 如果您包含具有欄位搜尋的 searchFields
,則 fieldName:searchExpression
一律優先於 searchFields
。
POST /indexes/hotel-samples-index/docs/search?api-version=2024-07-01
{
"search": "HotelName:(hotel NOT motel) AND Category:'Boutique'",
"queryType": "full",
"select": "HotelName, Category",
"count": true
}
此查詢的回應看起來應該類似下列範例,依 [精品] 篩選,傳回名稱中包含旅館的酒店,同時排除名稱中包含汽車旅館的結果。
{
"@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) 。
範例 2:模糊搜尋
模糊搜尋會比對類似字詞,包括拼錯的字組。 若要執行模糊搜尋,請在單一文字結尾附加波狀符號 ~
,加上選擇性參數 (介於 0 和 2 之間且會指定編輯距離的值)。 比方說,blue~
或 blue~1
會傳回 blue、blues 和 glue。
POST /indexes/hotel-samples-index/docs/search?api-version=2024-07-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個相符專案。
注意
模糊查詢不會進行分析。 不完整詞彙的查詢類型 (前置詞查詢、萬用字元查詢、Regex 查詢、模糊查詢) 會直接新增至查詢樹狀結構,並略過分析階段。 對部分查詢字詞執行的唯一轉換會轉換成小寫。
範例 3:鄰近搜尋
鄰近搜尋會尋找文件中彼此相近的字詞。 在片語的結尾插入波狀符號「~
」,後面加上建立鄰近界限的字數。
此查詢會在檔中的五個字內搜尋旅館和機場的字詞。 引號會逸出 (\"
) 以保留片語:
POST /indexes/hotel-samples-index/docs/search?api-version=2024-07-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)。 詞彙提升與評分設定檔的不同之處在於,評分設定檔會提升特定欄位,而不是特定字詞。
在查詢之前,搜尋海灘存取,並注意到有六份檔符合一或兩個字詞。
POST /indexes/hotel-samples-index/docs/search?api-version=2024-07-01
{
"search": "beach access",
"queryType": "full",
"select": "HotelName, Description, Tags",
"searchFields": "HotelName, Description, Tags",
"count": true
}
事實上,只有兩份檔符合存取權。 第一個實例處於第二個位置,即使文件遺漏了海灘一詞。
{
"@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"
]
}
]
}
在查詢之後,重複搜尋,這次會在字詞存取期間提升字詞海灘的結果。 查詢的人類可讀版本為 search=Description:beach^2 access
。 根據您的用戶端,您可能需要將 ^2
表示為 %5E2
。
POST /indexes/hotel-samples-index/docs/search?api-version=2024-07-01
{
"search": "Description:beach^2 access",
"queryType": "full",
"select": "HotelName, Description, Tags",
"searchFields": "HotelName, Description, Tags",
"count": true
}
當你提升海灘一詞后,校園指揮官酒店上的比賽將下降到第五位。
{
"@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=2024-07-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 查詢不會進行分析。 對部分查詢字詞執行的唯一轉換會轉換成小寫。
範例 6:萬用字元搜尋
您可以使用一般辨識語法來進行多字元 (*
) 或單一字元 (?
) 的萬用字元搜尋。 Lucene 查詢剖析器支援使用這些符號搭配單一字詞,而不是片語。
在此查詢中,搜尋包含前置 詞 sc 的旅館名稱。 您無法使用 *
或 ?
符號作為搜尋的第一個字元。
POST /indexes/hotel-samples-index/docs/search?api-version=2024-07-01
{
"search": "HotelName:sc*",
"queryType": "full",
"select": "HotelName",
"count": true
}
此查詢的回應看起來應該類似下列範例:
{
"@odata.count": 1,
"value": [
{
"@search.score": 1,
"HotelName": "Waterfront Scottish Inn"
}
]
}
注意
萬用字元查詢不會進行分析。 對部分查詢字詞執行的唯一轉換會轉換成小寫。
相關內容
嘗試在程式碼中指定查詢。 下列連結說明如何使用 Azure SDK 設定搜尋查詢。
您可以在下列文章中找到更多語法參考、查詢架構和範例: