term,match,match_phrase,query_string,simple_query_string区别
数据准备
设置mapping:
PUT /book
{
"mappings": {
"properties": {
"title": {
"type": "keyword"
},
"content": {
"type": "text"
}
}
}
}
插入两条数据:
POST /book/_doc/1
{
"title":"three kingdoms",
"content":"the three kingdoms period started with the end of the Han dynasty"
}
POST /book/_doc/2
{
"title":"journey to the west",
"content":"awakened to emptiness"
}
查看_mapping:
GET /book/_mapping
{
"book" : {
"mappings" : {
"properties" : {
"content" : {
"type" : "text"
},
"title" : {
"type" : "keyword"
}
}
}
}
}
查看两条document分词结果:
POST /book/_analyze
{
"field": "title",
"text": "three kingdoms"
}
_analyze结果: 可见 title 是没有被分词的
{
"tokens" : [
{
"token" : "three kingdoms",
"start_offset" : 0,
"end_offset" : 14,
"type" : "word",
"position" : 0
}
]
}
POST /book/_analyze
{
"field": "content",
"text": "awakened to emptiness"
}
_analyze结果: text会被分词
{
"tokens" : [
{
"token" : "awakened",
"start_offset" : 0,
"end_offset" : 8,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "to",
"start_offset" : 9,
"end_offset" : 11,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "emptiness",
"start_offset" : 12,
"end_offset" : 21,
"type" : "<ALPHANUM>",
"position" : 2
}
]
}
term
term查询keyword
term是将传入的文本原封不动地(不分词)拿去查询,keyword字段不分词,需要完全匹配才可查处结果
查询一:
GET /book/_search
{
"query": {
"term": {
"title": "kingdoms"
}
}
}
结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
查询二:
GET /book/_search
{
"query": {
"term": {
"title": "three kingdoms"
}
}
}
结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.6931471,
"hits" : [
{
"_index" : "book",
"_type" : "_doc",
"_id"