Bagikan melalui


Contoh sintaks pencarian Lucene lengkap (kueri tingkat lanjut)

Saat membuat kueri untuk Azure AI Search, Anda dapat mengganti pengurai kueri sederhana default dengan pengurai kueri Lucene yang lebih kuat untuk merumuskan ekspresi kueri khusus dan tingkat lanjut.

Pengurai Lucene mendukung format kueri yang kompleks, seperti kueri tercakup bidang, pencarian fuzzy, pencarian kartubebas infiks dan akhiran, pencarian kedekatan, peningkatan istilah, dan pencarian ekspresi reguler. Kekuatan tambahan dilengkapi dengan lebih banyak persyaratan pemrosesan sehingga Anda harus mengharapkan waktu eksekusi yang sedikit lebih lama. Dalam artikel ini, Anda dapat menelusuri contoh yang menunjukkan operasi kueri berdasarkan sintaks penuh.

Catatan

Banyak konstruksi kueri khusus yang diaktifkan melalui sintaks kueri Lucene penuh tidak dianalisis teks, yang dapat mengejutkan jika Anda mengharapkan stemming atau lemmatisasi. Analisis leksikal hanya dilakukan pada istilah lengkap (kueri istilah atau kueri frasa). Jenis kueri dengan istilah yang tidak lengkap (kueri awalan, kueri kartubebas, kueri regex, kueri fuzzy) ditambahkan langsung ke pohon kueri, melewati tahap analisis. Satu-satunya transformasi yang dilakukan pada istilah kueri dari jenis tersebut adalah huruf kecil.

Indeks sampel hotel

Kueri berikut didasarkan pada indeks sampel hotel, yang dapat Anda buat dengan mengikuti instruksi dalam mulai cepat ini.

Contoh kueri diartikulasikan menggunakan permintaan REST API dan POST. Anda dapat menempelkan dan menjalankannya di klien REST. Atau, gunakan tampilan JSON Search Explorer di portal Azure. Dalam tampilan JSON, Anda bisa menempelkan contoh kueri yang diperlihatkan di sini dalam artikel ini.

Permintaan header harus memiliki nilai berikut:

Tombol Nilai
Content-Type application/json
api-key <your-search-service-api-key>, baik kueri atau kunci admin

Parameter URI harus menyertakan titik akhir layanan pencarian Anda dengan nama indeks, koleksi dokumen, perintah pencarian, dan versi API, mirip dengan contoh berikut:

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

Isi permintaan harus dibentuk sebagai JSON yang valid:

{
    "search": "*",
    "queryType": "full",
    "select": "HotelId, HotelName, Category, Tags, Description",
    "count": true
}
  • search diatur ke * adalah kueri yang tidak ditentukan, setara dengan pencarian null atau kosong. Ini tidak terlalu berguna, tetapi ini adalah pencarian paling sederhana yang dapat Anda lakukan, dan menunjukkan semua bidang yang dapat diambil dalam indeks, dengan semua nilai.

  • queryType atur ke penuh memanggil pengurai kueri Lucene lengkap dan diperlukan untuk sintaks ini.

  • select diatur ke daftar bidang yang dibatasi koma digunakan untuk komposisi hasil pencarian, termasuk hanya bidang yang berguna dalam konteks hasil pencarian.

  • count mengembalikan jumlah dokumen yang cocok dengan kriteria pencarian. Pada string pencarian kosong, jumlahnya adalah semua dokumen dalam indeks (50 di indeks sampel hotel).

Cakupan pencarian bidang masing-masing, ekspresi pencarian yang disematkan ke bidang tertentu. Contoh ini mencari nama hotel dengan istilah hotel di dalamnya, tetapi bukan motel. Anda dapat menentukan beberapa bidang menggunakan AND.

Saat Anda menggunakan sintaks kueri ini, Anda dapat menghilangkan searchFields parameter saat bidang yang ingin Anda kueri berada dalam ekspresi pencarian itu sendiri. Jika Anda menyertakan searchFields dengan pencarian bidang, fieldName:searchExpression selalu diutamakan daripada 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
}

Respons untuk kueri ini akan terlihat mirip dengan contoh berikut, difilter di Boutique, mengembalikan hotel yang menyertakan hotel dalam nama, sambil mengecualikan hasil yang menyertakan motel dalam nama.

{
  "@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"
    }
  ]
}

Ekspresi pencarian dapat berupa istilah tunggal atau frasa, atau ekspresi yang lebih kompleks dalam tanda kurung, secara opsional dengan operator Boolean. Beberapa contoh meliputi:

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

Pastikan untuk menempatkan frasa dalam tanda kutip jika Anda ingin kedua string dievaluasi sebagai entitas tunggal, seperti dalam hal ini mencari dua lokasi yang berbeda di Address/StateProvince bidang . Tergantung pada klien, Anda mungkin perlu meloloskan (\) tanda kutip.

Bidang yang ditentukan dalam fieldName:searchExpression harus merupakan bidang yang dapat dicari. Untuk mempelajari bagaimana definisi bidang diatribusikan, lihat Membuat Indeks (REST API).

Pencarian fuzzy mencocokkan dengan istilah yang serupa, termasuk kata-kata yang salah eja. Untuk melakukan pencarian fuzzy, tambahkan simbol tilde ~ di akhir satu kata dengan parameter opsional, nilai antara 0 dan 2, yang menentukan jarak edit. Misalnya, blue~ atau blue~1 akan mengembalikan warna biru, blues, dan lem.

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
}

Respons untuk kueri ini memutuskan untuk melakukan concierge dalam dokumen yang cocok, dipangkas untuk brevity:

{
  "@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"
      ]
    },
. . .
  ]
}

Frasa tidak didukung secara langsung tetapi Anda dapat menentukan kecocokan fuzzy pada setiap istilah frasa multi-bagian, seperti search=Tags:landy~ AND sevic~. Ekspresi kueri ini menemukan 15 kecocokan pada layanan binatu.

Catatan

Kueri fuzzy tidak dianalisis. Jenis kueri dengan istilah yang tidak lengkap (kueri awalan, kueri kartubebas, kueri regex, kueri fuzzy) ditambahkan langsung ke pohon kueri, melewati tahap analisis. Satu-satunya transformasi yang dilakukan pada istilah kueri dari jenis tersebut adalah huruf kecil.

Pencarian terdekat menemukan istilah yang saling berdekatan dalam sebuah dokumen. Sisipkan simbol tilde ~ di akhir frasa diikuti dengan jumlah kata yang membuat batas kedekatan.

Kueri ini mencari istilah hotel dan bandara dalam lima kata satu sama lain dalam dokumen. Tanda kutip lolos (\") untuk mempertahankan frasa:

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
}

Respons untuk kueri ini akan terlihat mirip dengan contoh berikut:

{
  "@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."
    }
  ]
}

Contoh 4: Peningkatan istilah

Peningkatan istilah mengacu pada peringkat dokumen yang lebih tinggi jika berisi istilah yang didorong, relatif terhadap dokumen yang tidak berisi istilah. Untuk meningkatkan istilah, gunakan tanda sisipan, ^simbol , dengan faktor peningkatan (angka) di akhir istilah yang Anda cari. Default faktor dorongan adalah 1, dan meskipun harus positif, itu bisa kurang dari 1 (misalnya, 0,2). Dokumen ini berbeda dari penilaian profil dalam profil penilaian yang meningkatkan bidang tertentu, bukan istilah tertentu.

Dalam kueri sebelum ini, cari akses pantai dan perhatikan bahwa ada enam dokumen yang cocok pada satu atau kedua istilah.

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
}

Bahkan, hanya dua dokumen yang cocok pada akses. Instans pertama berada di posisi kedua, meskipun dokumen tidak memiliki istilah pantai.

{
  "@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"
      ]
    }
  ]
}

Dalam kueri setelah, ulangi pencarian, kali ini meningkatkan hasil dengan istilah pantai selama istilah akses. Versi kueri yang dapat dibaca manusia adalah search=Description:beach^2 access. Tergantung pada klien Anda, Anda mungkin harus mengekspresikan ^2 sebagai %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
}

Setelah Anda meningkatkan istilah pantai, pertandingan di Campus Commander Hotel bergerak turun ke posisi kelima.

{
  "@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"
      ]
    }
  ]
}

Contoh 5: Regex

Pencarian ekspresi reguler menemukan kecocokan berdasarkan konten antara garis miring / ke depan dan string huruf kecil, seperti yang didokumenkan di kelas RegExp.

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

Respons untuk kueri ini akan terlihat mirip dengan contoh berikut (dipangkas untuk brevity):

{
  "@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"
    }
  ]
}

Catatan

Kueri regex tidak dianalisis. Satu-satunya transformasi yang dilakukan pada istilah kueri dari jenis tersebut adalah huruf kecil.

Anda dapat menggunakan sintaksis yang dikenali secara umum untuk beberapa (*) atau satu pencarian kartubebas karakter (?). Pengurai kueri Lucene mendukung penggunaan simbol-simbol ini dengan satu istilah, dan bukan frasa.

Dalam kueri ini, cari nama hotel yang berisi awalan sc. Anda tidak dapat menggunakan * simbol atau ? sebagai karakter pertama pencarian.

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

Respons untuk kueri ini akan terlihat mirip dengan contoh berikut:

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

Catatan

Kueri regex tidak dianalisis. Satu-satunya transformasi yang dilakukan pada istilah kueri dari jenis tersebut adalah huruf kecil.

Coba tentukan kueri dalam kode. Tautan berikut mencakup cara menyiapkan kueri pencarian menggunakan Azure SDK.

Referensi sintaks lainnya, arsitektur kueri, dan contoh dapat ditemukan di artikel berikut: