记录学习ES的DSL的一些比较重要的概念和方法。
term VS match
首先要说的是, 一个doc进入ES被索引,哪些字段(fields)被索引是可以自定义的。 默认,ES会对所有的fields进行索引。
PUT /my_index
{
"mappings": {
"my_type": {
"properties": {
"status_code": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
这是个简单的mapping, 有个status_code字段,类型是string, index的方式是“not_analyzed”。 ES中某个字段的index方式有三种值: no/not_analyzed/analyzed, 对应的意思是:
no = > 该字段不进入index, 即不可对该字段进行query。
not_analyzed => 该字段作为一个term进入index,不进行分词等analyse操作。对除string类型之外的fields, 这个是默认的。对string类型的字段,默认要进行分词的,除非指定不进行分词(即not_analyzed) 。
analyzed =》 只对string类型的field有效, 且是默认的。 通过analyzer, 该field会被切分成若干的分词(terms),
再来说,term和match
term是用来进行分词精确匹配的(The term query finds documents that contain the exact term specified in the inverted index.)
match是用来进行全文检索的(full-text search)。
看下面的一个例子, 就能够很清楚的知道二者的区别了。例子来自https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
myindex下的一个类型是my_type的mapping:
PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"full_text": {
"type": "string"
},
"exact_value": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
PUT my_index/my_type/1
{
"full_text": "Quick Foxes!",
"exact_value": "Quick Foxes!"
}
| The full_text field is analyzed by default. |
| The exact_value field is set to be not_analyzed. |
| The full_text inverted index will contain the terms: [quick, foxes]. |
| The exact_value inverted index will contain the exact term: [Quick Foxes!]. |
Now, compare the results for the term query and the match query:
(1)GET my_index/my_type/_search
{
"query": {
"term": {
"exact_value": "Quick Foxes!"
}
}
}
(2)GET my_index/my_type/_search
{
"query": {
"term": {
"full_text": "Quick Foxes!"
}
}
}
(3)GET my_index/my_type/_search
{
"query": {
"term": {
"full_text": "foxes"
}
}
}
(4)GET my_index/my_type/_search
{
"query": {
"match": {
"full_text": "Quick Foxes!"
}
}
}
This query matches because the | |
This query does not match, because the | |
A | |
This |