26/11/2018, 22:11

Full text queries và Term level queries trong ElasticSearch (P1)

Chúng ta đã biết đến elastic search mạnh mẽ, hỗ trợ nhiều phương thức query, tuy nhiên chúng ta sẽ chủ yếu sử dụng 2 phương thức với full text queries và Term level queries, vậy full text queries là gì và Term level queries là gì? khi nào thì chúng ta sử dụng full text queries, khi nào thì nên sử ...

Chúng ta đã biết đến elastic search mạnh mẽ, hỗ trợ nhiều phương thức query, tuy nhiên chúng ta sẽ chủ yếu sử dụng 2 phương thức với full text queries và Term level queries, vậy full text queries là gì và Term level queries là gì? khi nào thì chúng ta sử dụng full text queries, khi nào thì nên sử dụng Term level queries?

Full text queries:

The high-level full text queries are usually used for running full text queries on full text fields like the body of an email. They understand how the field being queried is analyzed and will apply each field’s analyzer (or search_analyzer) to the query string before executing.

Term level queries:

While the full text queries will analyze the query string before executing, the term-level queries operate on the exact terms that are stored in the inverted index, and will normalize terms before executing only for keyword fields with normalizer property.

These queries are usually used for structured data like numbers, dates, and enums, rather than full text fields. Alternatively, they allow you to craft low-level queries, foregoing the analysis process

Sự khác biệt:

Vậy Full text queries sẽ thực hiện query đối với từng field sau khi đã analyzed data, còn Term level queries không thực hiện analyze data như Full text queries mà sẽ thực hiện query theo cụm từ, và Term level queries chỉ thực hiện được trên các field có type là keyword. Lý do vì sao thì chúng ta cùng xem phần demo để hiểu rõ hơn.

Các bạn có thể sử dụng curl hoặc postman hoặc bất cứ tool gì để test cũng được, ở đây mình dùng postman.

Create index:

Trước tiên chúng ta cùng tạo 1 index nào :

curl PUT  localhost:9200/demoes1
{
        "aliases": {},
        "mappings": {
            "demo": {
                "properties": {
                    "content": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            }
        }
    }

Ok, như vậy ta đã có index demoes1 với trường content để test query.

Insert data:

Ta phải insert data để test

curl PUT localhost:9200/demoes1/demo/1
{
    "content" : "elasticsearch is awsome!"
}

Execute query:

Ta đã có data, giờ thì search thôi nào. Ở đây mình sẽ query theo kiểu LIKE FOO%.

Full text queries: Sử dụng Match Phrase Prefix Query(match_phrase_prefix)

curl POST localhost:9200/demoes1/demo/_search
{
	"query" : {
		"match_phrase_prefix":{
	    	"content" : "elasticsearch"
		}
	}
}

Result:

{
    "hits": {
        "hits": [
            {
                "_source": {
                    "content": "elasticsearch is awsome!"
                }
            }
        ]
    }
}

Tuyệt vời đúng ko nào. Giờ ta thử tiếp nếu ta input vào "is awsome" thì thế nào nhé

curl POST localhost:9200/demoes1/demo/_search
{
	"query" : {
		"match_phrase_prefix":{
	    	"content" : "is awsome"
		}
	}
}

Result:

{
    "hits": {
        "hits": [
            {
                "_source": {
                    "content": "elasticsearch is awsome!"
                }
            }
        ]
    }
}

Tiếp tục nào

curl POST localhost:9200/demoes1/demo/_search
{
	"query" : {
		"match_phrase_prefix":{
	    	"content" : "is"
		}
	}
}

Result:

{
    "hits": {
        "hits": [
            {
                "_source": {
                    "content": "elasticsearch is awsome!"
                }
            }
        ]
    }
}

Great! Vẫn search được, xem ra elasticsearch khá tuyệt vời. Nhưng mà khoan đã, đề bài của chúng ta là LIKE FOO% mà @@. Thế này thì sai đề rồi. OK, vậy ta phân tích lại Full text queries thế nào nhé. Như đã nói ở trên

They understand how the field being queried is analyzed and will apply each field’s analyzer

Như vậy, es khá thông minh khi tách data của chúng ta: "elasticsearch is awsome!" -> ["elasticsearch", "elasticsearch is", "elasticsearch is awsome!", "is", "is awsome!", "awsome!"] rồi đi thực hiện so sánh kiểu "LIKE FOO%" input truyền vào với từng phần tử trong mảng. Khá thông minh phải ko nào?

Nhưng bài toán của mình là search "LIKE FOO%" toàn bộ data mà? Mong muốn nhập "is" vào thì ko ra kết quả (grin). Bài toán đưa ra khá đơn giản đối với ngôn ngữ sql, vậy Elasticsearch có giải quyết được vấn đề này không? Cùng chờ đón phần 2 nhé!

0