term
term是代表完全匹配,即不进行分词器分析,文档中必须包含整个搜索的词汇
{
"term" : {
"price" : 20
}
}
# SELECT document FROM products WHERE price = 20
通常当查找一个精确值的时候,我们不希望对查询进行评分计算。只希望对文档进行包括或排除的计算,所以我们会使用 constant_score 查询以非评分模式来执行 term 查询并以一作为统一评分。
curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query" : {
"constant_score" : { # 我们用 constant_score 将 term 查询转化成为过滤器
"filter" : {
"term" : {
"price" : 20
}
}
}
}
}
'
terms
curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query" : {
"constant_score" : {
"filter" : {
"terms" : {
"price" : [20, 30] # 这个 terms 查询被置于 constant_score 查询中
}
}
}
}
}
'
包含,而不是相等
一定要了解 term 和 terms 是 包含(contains) 操作,而非 等值(equals)。