Cách tạo ComputedField AutoComplete trong Sitecore Solr

Cách tạo trong

Đề bài: Lấy Title Article để xây tính năng AutoComplete

Bước 1: Truy cập thư mục wwwroot/[thư mục sitcore] /App_Config/Sitecore/ContentSearch

Bước 2: Mở tệp Sitecore.ContentSearch.Solr.DefaultIndexConfiguration bằng Notepad++

Bước 3: Ctrl + F nhập raw:AddComputedIndexField và Enter để tìm kiếm

Thông báo Fields mới cho ComputedIndex

Figure 1. Thông báo Fields mới cho ComputedIndex

Bước 4: Xuống cuối fields tìm được thêm dòng sau:

Thêm Fields

Figure 2. Thêm Fields

Bước 5: Tạo thư mục và class AutoCompleteName

Thêm ComputedIndex vào Project

Figure 3. Thêm ComputedIndex vào Project

Bước 6: Dán đoạn mã này vào class AutoCompleteCame, sau đó Alt + Enter để thêm các thư viện yêu cầu.

Code để tạo fields search_autocomplete trong solr với value là title của Article

Figure 4. Code để tạo fields search_autocomplete trong solr với value là title của Article

Bước 7: Vào thư mục Models tạo class Article với code sau:

Models của Article để chứa Title, IndexFields là tênc của fields trong Solr. Truy cập quểy trong https://localhost:8983/solr để tìm hiểu thêm

Figure 5. Models của Article để chứa Title, IndexFields là tênc của fields trong Solr. Truy cập quểy trong https://localhost:8983/solr để tìm hiểu thêm

Bước 8: Vào Controller và thêm đoạn mã sau

Tìm trong sitecore/content/home với sitecore  web index, tìm với keyword được cấp sau đó chọn các fields với trường search_autocomplete được tạo

Figure 6. Tìm trong sitecore/content/home với sitecore  web index, tìm với keyword được cấp sau đó chọn các fields với trường search_autocomplete được tạo.

Bước 9: Đăng ký API cho Project, mở thư mục như hình và mở tệp config bên trong Feature

Đăng ký API cho Project

Bước 10: Thêm đoạn mã configurator như hình để đăng ký api cho project: namespace.class name, projectname

đăng ký api cho project: namespace.class name, projectname

Bước 11: Truy cập /sitecore, đăng nhập Admin -> Control Panel tìm mục Indexing và click vào Indexing manager

Indexing và click vào Indexing manager

Bước 12: Chọn tất cả các index rồi bấm Rebuild đến khi hoàn tất

Rebuild đến khi hoàn tất

Đến khi hoàn tất như hình dưới, sau khi build hoàn tất trường search_autocomplete sẽ được tạo ra, truy cập https://localhost:8983/solr/[your-web-index] /select?q=*%3A* ctrl + F gõ search_autocomplete để thấy kết quả dưới dạng hiển thị của Solr.

kết quả rebuilding index

Bước cuối: [tên domain sitecore của bạn]/api/sitecore/[Controller name] /SearchAutoComplete?term=a để thấy kết quả.

kết quả cuối cùng auto suggest complete

Từ đây sử dụng Js, Ajax, v.v. để đọc api, xử lý tiến trình AutoComplete khi người dùng nhập Input theo phương thức get hoặc post, mặc định get. Từ khoá nằm trong query: term=a

Xem thêm: Thư viện VoiceNET dễ dàng tạo chức năng Voice Command Control C#

Bình luận Facebook