1.term
(1).query
将查询语句作为整体进行查询,不对查询语句做分词处理,如elasticsearch中存储了“How are you!”文档,且待查询语句为“How are you”,那么查询结果中不会匹配到“How old are you!”或者“How are you!”。
POST /people/_search
{
"query": {
"term": {
"description": "How are you!"
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
(2).原因分析
elasticsearch对“How are you!”进行分词时,拆成以下3个单词,没有将其作为一个整体存储。
GET /people/_analyze
{
"text": [ "How are you!"],
"field": "description"
}
{
"tokens" : [
{
"token" : "how",
"start_offset" : 0,
"end_offset" : 3,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "are",
"start_offset" : 4,
"end_offset" : 7,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "you",
"start_offset" : 8,
"end_offset" : 11,
"type" : "<ALPHANUM>",
"position" : 2
}
]
}
(3).解决办法
POST /people/_search
{
"query": {
"match": {
"description": "How are you!"
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.8630463,
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "zIlIoHsBEsHOdz1YB8pI",
"_score" : 0.8630463,
"_source" : {
"name" : "Deron Williams",
"country" : "Englend",
"age" : 32,
"date" : "1989-01-01",
"description" : "How are you!"
}
}
]
}
}
2.terms
通过terms参数控制查询的query_clause个数。
POST /people/_search
{
"query": {
"terms": {
"country": ["Englend","China"]
}
}
}
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "1111111111",
"_score" : 1.0,
"_source" : {
"_class" : "com.example.spring.elasticsearch.People",
"id" : "1111111111",
"date" : 599616000000,
"country" : "Englend",
"name" : "haha",
"description" : "How are you!",
"age" : 20
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "nIm-gXsBEsHOdz1Yrcq7",
"_score" : 1.0,
"_source" : {
"name" : "Deron Williams",
"country" : "Englend",
"age" : 32,
"date" : "1989-01-01",
"description" : "How are you!"
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "Mike Steven",
"country" : "China",
"age" : 26,
"date" : "1995-01-01",
"description" : "I am Mike Steven."
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "mom6gXsBEsHOdz1YRcov",
"_score" : 1.0,
"_source" : {
"name" : "Mike Sherry",
"country" : "China",
"age" : 23,
"date" : "1998-06-01",
"description" : "I am Mike Sherry."
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "2222222",
"_score" : 1.0,
"_source" : {
"_class" : "com.example.spring.elasticsearch.People",
"id" : "2222222",
"date" : "1989-01-01",
"country" : "Englend",
"name" : "haha",
"description" : "How are you!",
"age" : 20
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "nonCgXsBEsHOdz1YDcoP",
"_score" : 1.0,
"_source" : {
"_class" : "com.example.spring.elasticsearch.People",
"id" : "nonCgXsBEsHOdz1YDcoP",
"name" : "Mike Owen",
"age" : "34",
"date" : "1987-03-12",
"country" : "Englend",
"description" : "How are you!",
"class" : "class com.example.spring.elasticsearch.People"
}
}
]
}
}