Điểm tương tự chuỗi python

Mô-đun

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
5 cung cấp các hàm và toán tử để xác định độ giống nhau của văn bản chữ và số dựa trên đối sánh bát quái, cũng như các lớp toán tử chỉ mục hỗ trợ tìm kiếm nhanh các chuỗi tương tự

Mô-đun này được coi là "đáng tin cậy", nghĩa là nó có thể được cài đặt bởi những người không phải siêu người dùng có đặc quyền

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
6 trên cơ sở dữ liệu hiện tại

F. 35. 1. Trigram [hoặc Trigraph] Khái niệm

Bát quái là một nhóm gồm ba ký tự liên tiếp được lấy từ một chuỗi. Chúng ta có thể đo lường sự giống nhau của hai chuỗi bằng cách đếm số bát quái mà chúng chia sẻ. Ý tưởng đơn giản này hóa ra lại rất hiệu quả để đo lường sự giống nhau của các từ trong nhiều ngôn ngữ tự nhiên

Ghi chú

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
5 bỏ qua các ký tự không phải từ [không phải chữ và số] khi trích xuất bát quái từ một chuỗi. Mỗi từ được coi là có hai dấu cách ở đầu và một dấu cách ở hậu tố khi xác định bộ bát quái chứa trong chuỗi. Ví dụ: bộ bát quái trong chuỗi “_______08” là “_______09”, “_______50”, “_______08”, và “
CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];
2”. Bộ bát quái trong chuỗi “
CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];
3” là “_______54”, “_______55”, “
CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];
6”, “
CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];
7”, “_______58”, “_______59”, “
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
50”, và “
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
51”

F. 35. 2. Hàm và toán tử

Các chức năng được cung cấp bởi mô-đun

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
5 được hiển thị trong Bảng F. 24, các toán tử trong Bảng F. 25

Bảng F. 24.

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
5 Chức năng

Chức năng

Sự miêu tả

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
54 [
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55,
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 ] →
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
57

Trả về một số cho biết hai đối số giống nhau như thế nào. Phạm vi của kết quả là 0 [chỉ ra rằng hai chuỗi hoàn toàn khác nhau] đến một [chỉ ra rằng hai chuỗi giống hệt nhau]

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
58 [
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 ] →
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
50

Trả về một mảng gồm tất cả các bát quái trong chuỗi đã cho. [Trong thực tế, điều này hiếm khi hữu ích ngoại trừ gỡ lỗi. ]

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
51 [
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55,
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 ] →
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
57

Trả về một số cho biết mức độ tương đồng lớn nhất giữa tập hợp các bát quái trong chuỗi đầu tiên và bất kỳ mức độ liên tục nào của tập hợp các bát quái theo thứ tự trong chuỗi thứ hai. Chi tiết xem phần giải thích bên dưới

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 [
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55,
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 ] →
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
57

Tương tự như

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
51, nhưng buộc ranh giới phạm vi khớp với ranh giới từ. Vì chúng tôi không có bát quái ô chữ, nên hàm này thực sự trả về độ giống nhau lớn nhất giữa chuỗi đầu tiên và bất kỳ mức độ liên tục nào của các từ của chuỗi thứ hai

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
50[] →
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
57

Trả về ngưỡng tương tự hiện tại được sử dụng bởi toán tử

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
52. Điều này đặt mức độ giống nhau tối thiểu giữa hai từ để chúng được coi là giống nhau đủ để viết sai chính tả của nhau, ví dụ. [Không dùng nữa; thay vào đó hãy sử dụng
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
53
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
54. ]

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 [
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
57 ] →
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
57

Đặt ngưỡng tương tự hiện tại được sử dụng bởi toán tử

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
52. Ngưỡng phải nằm trong khoảng từ 0 đến 1 [mặc định là 0. 3]. Trả về cùng một giá trị được truyền vào. [Không dùng nữa; thay vào đó hãy sử dụng
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
59
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
54. ]

Xem xét ví dụ sau

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
6

Trong chuỗi đầu tiên, bộ bát quái là

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
71. Trong chuỗi thứ hai, bộ bát quái có thứ tự là
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
72. Mức độ giống nhau nhất của một bộ bát quái được sắp xếp theo thứ tự trong chuỗi thứ hai là
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
73 và độ tương tự là
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
74

Hàm này trả về một giá trị có thể được hiểu gần đúng là độ giống nhau lớn nhất giữa chuỗi đầu tiên và bất kỳ chuỗi con nào của chuỗi thứ hai. Tuy nhiên, chức năng này không thêm phần đệm vào ranh giới của phạm vi. Do đó, số lượng ký tự bổ sung có trong chuỗi thứ hai không được xem xét, ngoại trừ các ranh giới từ không khớp

Đồng thời,

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 chọn một phạm vi từ trong chuỗi thứ hai. Trong ví dụ trên,
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 sẽ chọn phạm vi của một từ duy nhất
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
77, có bộ bát quái là
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
78

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]

Do đó, hàm

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 hữu ích để tìm sự giống nhau của toàn bộ từ, trong khi hàm
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
51 phù hợp hơn để tìm sự giống nhau cho các phần của từ

Bảng F. 25.

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
5 Người vận hành

Nhà điều hành

Sự miêu tả

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
52
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 →
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
25

Trả về

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
26 nếu các đối số của nó có độ tương tự lớn hơn ngưỡng độ tương tự hiện tại được đặt bởi
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
54

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
29
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 →
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
25

Trả về

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
26 nếu độ tương tự giữa bộ bát quái trong đối số đầu tiên và phạm vi liên tục của bộ bát quái có thứ tự trong đối số thứ hai lớn hơn ngưỡng tương tự từ hiện tại được đặt bởi tham số
CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];
03

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55
CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];
05
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 →
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
25

Cổ góp của toán tử

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
29

________ 155 ________ 770 ________ 155 → ________ 625

Trả về

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
26 nếu đối số thứ hai của nó có phạm vi liên tục của bộ bát quái được sắp xếp phù hợp với ranh giới từ và độ tương tự của nó với bộ bát quái của đối số đầu tiên lớn hơn ngưỡng tương tự từ nghiêm ngặt hiện tại được đặt bởi tham số
CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];
74

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55
CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];
76
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 →
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
25

Cổ góp của toán tử

CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];
70

________ 155 ________ 001 ________ 155 → ________ 157

Trả về "khoảng cách" giữa các đối số, đó là một trừ đi giá trị

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
04

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
06
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 →
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
57

Trả về "khoảng cách" giữa các đối số, đó là một trừ đi giá trị

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
09

________ 155 ________ 011 ________ 155 → ________ 157

Cổ góp của toán tử

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
06

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
16
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 →
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
57

Trả về "khoảng cách" giữa các đối số, đó là một trừ đi giá trị

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
19

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
21
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
55 →
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
57

Cổ góp của toán tử

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
16


Đặt ngưỡng tương tự hiện tại được sử dụng bởi toán tử

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
52. Ngưỡng phải nằm trong khoảng từ 0 đến 1 [mặc định là 0. 3]

Đặt ngưỡng tương tự từ hiện tại được sử dụng bởi các toán tử

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
29 và
CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];
05. Ngưỡng phải nằm trong khoảng từ 0 đến 1 [mặc định là 0. 6]

Đặt ngưỡng tương tự từ nghiêm ngặt hiện tại được sử dụng bởi các toán tử

CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];
70 và
CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];
76. Ngưỡng phải nằm trong khoảng từ 0 đến 1 [mặc định là 0. 5]

Mô-đun

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
5 cung cấp các lớp toán tử chỉ mục GiST và GIN cho phép bạn tạo một chỉ mục trên một cột văn bản nhằm mục đích tìm kiếm sự tương đồng rất nhanh. Các loại chỉ mục này hỗ trợ các toán tử tương tự được mô tả ở trên và hỗ trợ thêm các tìm kiếm chỉ mục dựa trên trigram cho các truy vấn
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
31,
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
32,
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
33,
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
34 và
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
35. Toán tử bất đẳng thức không được hỗ trợ. Lưu ý rằng các chỉ mục đó có thể không hiệu quả như các chỉ mục cây B thông thường cho toán tử đẳng thức

Ví dụ

CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];

hoặc

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
5

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
36 GiST opclass xấp xỉ một bộ bát quái dưới dạng chữ ký bitmap. Tham số số nguyên tùy chọn của nó
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
37 xác định độ dài chữ ký theo byte. Độ dài mặc định là 12 byte. Các giá trị hợp lệ của độ dài chữ ký nằm trong khoảng từ 1 đến 2024 byte. Chữ ký dài hơn dẫn đến tìm kiếm chính xác hơn [quét một phần nhỏ hơn của chỉ mục và ít trang heap hơn], với chi phí của một chỉ mục lớn hơn

Ví dụ về việc tạo một chỉ mục như vậy với độ dài chữ ký là 32 byte

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
5

Tại thời điểm này, bạn sẽ có một chỉ mục trên cột

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
38 mà bạn có thể sử dụng để tìm kiếm sự tương đồng. Một truy vấn điển hình là

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
5

Điều này sẽ trả về tất cả các giá trị trong cột văn bản đủ giống với

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
39, được sắp xếp từ phù hợp nhất đến kém nhất. Chỉ mục sẽ được sử dụng để thực hiện thao tác này nhanh chóng ngay cả trên các tập dữ liệu rất lớn

Một biến thể của truy vấn trên là

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
7

Điều này có thể được thực hiện khá hiệu quả bởi các chỉ mục GiST, nhưng không phải bởi các chỉ mục GIN. Nó thường sẽ đánh bại công thức đầu tiên khi chỉ cần một số lượng nhỏ các trận đấu gần nhất

Ngoài ra, bạn có thể sử dụng một chỉ mục trên cột

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
38 cho từ tương tự hoặc từ tương tự nghiêm ngặt. Các truy vấn điển hình là

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
2

CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];
0

Điều này sẽ trả về tất cả các giá trị trong cột văn bản có phạm vi liên tục trong bộ bát quái được sắp xếp tương ứng đủ giống với bộ bát quái của

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
39, được sắp xếp từ phù hợp nhất đến kém nhất. Chỉ mục sẽ được sử dụng để thực hiện thao tác này nhanh chóng ngay cả trên các tập dữ liệu rất lớn

Các biến thể có thể có của các truy vấn trên là

CREATE TABLE test_trgm [t text];
CREATE INDEX trgm_idx ON test_trgm USING GIST [t gist_trgm_ops];
7

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
0

Điều này có thể được thực hiện khá hiệu quả bởi các chỉ mục GiST, nhưng không phải bởi các chỉ mục GIN

Bắt đầu từ PostgreSQL 9. 1, các loại chỉ mục này cũng hỗ trợ tìm kiếm chỉ mục cho

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
31 và
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
32, chẳng hạn

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
1

Tìm kiếm chỉ mục hoạt động bằng cách trích xuất các bát quái từ chuỗi tìm kiếm và sau đó tìm kiếm chúng trong chỉ mục. Càng nhiều bát quái trong chuỗi tìm kiếm, tìm kiếm chỉ mục càng hiệu quả. Không giống như các tìm kiếm dựa trên cây B, chuỗi tìm kiếm không cần phải neo trái

Bắt đầu từ PostgreSQL 9. 3, các loại chỉ mục này cũng hỗ trợ tìm kiếm chỉ mục cho các đối sánh biểu thức chính quy [các toán tử

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
33 và
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
34], chẳng hạn

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
2

Tìm kiếm chỉ mục hoạt động bằng cách trích xuất các bát quái từ biểu thức chính quy và sau đó tìm kiếm chúng trong chỉ mục. Càng nhiều bát quái có thể được trích xuất từ ​​​​biểu thức chính quy, tìm kiếm chỉ mục càng hiệu quả. Không giống như các tìm kiếm dựa trên cây B, chuỗi tìm kiếm không cần phải neo trái

Đối với cả tìm kiếm

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
31 và cụm từ thông dụng, hãy nhớ rằng một mẫu không có bát quái có thể trích xuất được sẽ chuyển thành quét toàn bộ chỉ mục

Lựa chọn giữa lập chỉ mục GiST và GIN phụ thuộc vào các đặc tính hiệu suất tương đối của GiST và GIN, được thảo luận ở nơi khác

F. 35. 5. Tích hợp tìm kiếm văn bản

Đối sánh bát quái là một công cụ rất hữu ích khi được sử dụng kết hợp với mục lục toàn văn. Đặc biệt, nó có thể giúp nhận ra các từ nhập sai chính tả sẽ không khớp trực tiếp với cơ chế tìm kiếm toàn văn

Bước đầu tiên là tạo một bảng phụ chứa tất cả các từ duy nhất trong tài liệu

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
3

trong đó

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
47 là một bảng có trường văn bản
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
48 mà chúng tôi muốn tìm kiếm. Lý do sử dụng cấu hình
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
49 với chức năng
# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
50, thay vì sử dụng cấu hình dành riêng cho ngôn ngữ, là vì chúng tôi muốn có một danh sách các từ gốc [không gốc]

Tiếp theo, tạo mục lục bát quái trên cột chữ

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
4

Giờ đây, truy vấn

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
51 tương tự như ví dụ trước có thể được sử dụng để đề xuất cách viết cho các từ sai chính tả trong cụm từ tìm kiếm của người dùng. Một bài kiểm tra bổ sung hữu ích là yêu cầu các từ được chọn cũng có độ dài tương tự với từ sai chính tả

Ghi chú

Vì bảng

# SELECT strict_word_similarity['word', 'two words'], similarity['word', 'words'];
 strict_word_similarity | similarity
------------------------+------------
               0.571429 |   0.571429
[1 row]
52 đã được tạo dưới dạng một bảng tĩnh, riêng biệt nên nó sẽ cần được tạo lại định kỳ để nó vẫn được cập nhật một cách hợp lý với bộ sưu tập tài liệu. Giữ nó chính xác hiện tại thường là không cần thiết

Chủ Đề