差別
match
全文搜尋, 通常用于對text類型字段的查詢,會對進行查詢的文本先進行分詞操作,如下圖
term
精确查詢,通常用于對keyword和有精确值的字段進行查詢,不會對進行查詢的文本進行分詞操作,精确比對,如下圖
執行個體說明
- 建立索引
- 添加資料
- 測試分詞效果
- 測試match查詢
- 測試term查詢
建立索引
添加一個text類型的字段,使用ik_max_word分析器
PUT /testquery
{
"mappings": {
"books":{
"properties": {
"bookName":{
"type":"text",
"analyzer": "ik_max_word"
}
}
}
}
}
添加資料
POST /testquery/books
{
"bookName":"Java程式設計思想"
}
測試分詞效果
GET /_analyze
{
"analyzer": "ik_max_word",
"text": ["Java程式設計思想"]
}
結果
{
"tokens": [
{
"token": "java",
"start_offset": 0,
"end_offset": 4,
"type": "ENGLISH",
"position": 0
},
{
"token": "程式設計",
"start_offset": 4,
"end_offset": 6,
"type": "CN_WORD",
"position": 1
},
{
"token": "思想",
"start_offset": 6,
"end_offset": 8,
"type": "CN_WORD",
"position": 2
}
]
}
是以,Java程式設計思想 實際分詞效果如下
測試match查詢,進行分詞操作
查詢 Java
先看看 Java 的分詞效果
GET /_analyze
{
"analyzer": "ik_max_word",
"text": ["Java"]
}
{
"tokens": [
{
"token": "java",
"start_offset": 0,
"end_offset": 4,
"type": "ENGLISH",
"position": 0
}
]
}
存在, 是以是可以查詢到的
POST /testquery/books/_search
{
"query": {
"match": {
"bookName": "Java"
}
}
}
{
"took": 60,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "testquery",
"_type": "books",
"_id": "vYOz-msB_TzP5bFY5JlL",
"_score": 0.2876821,
"_source": {
"bookName": "Java程式設計思想"
}
}
]
}
}
查詢 Java思想
檢視分詞效果
GET /_analyze
{
"analyzer": "ik_max_word",
"text": ["Java思想"]
}
{
"tokens": [
{
"token": "java",
"start_offset": 0,
"end_offset": 4,
"type": "ENGLISH",
"position": 0
},
{
"token": "思想",
"start_offset": 4,
"end_offset": 6,
"type": "CN_WORD",
"position": 1
}
]
}
存在, 是以也是可以查詢到的
POST /testquery/books/_search
{
"query": {
"match": {
"bookName": "Java思想"
}
}
}
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "testquery",
"_type": "books",
"_id": "vYOz-msB_TzP5bFY5JlL",
"_score": 0.5753642,
"_source": {
"bookName": "Java程式設計思想"
}
}
]
}
}
POST /testquery/books/_search
{
"query": {
"term": {
"bookName": "java"
}
}
}
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "testquery",
"_type": "books",
"_id": "vYOz-msB_TzP5bFY5JlL",
"_score": 0.2876821,
"_source": {
"bookName": "Java程式設計思想"
}
}
]
}
}
POST /testquery/books/_search
{
"query": {
"term": {
"bookName": "Java"
}
}
}
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}