ElasticSearch搜索语法学习(term,filter,bool,terms,range)
ES搜索语法学习
⽬录
0. 原始数据
1. term,filter使⽤
2. bool组合多个filter条件来搜索数据
3. terms搜索多个值以及多值搜索结果优化
4. 基于range filter来进⾏范围过滤
5. ⼿动控制全⽂检索结果的精准度
6. dis_max实现best fields策略进⾏多字段搜索
7.
1. term,filter使⽤
0. 原始数据(⽬录1~2使⽤)
POST /forum/article/_bulk
{"index":{"_id":1}}
{"articleID":"XHDK-A-1293-#fJ3","urID":1,"hidden":fal,"postDate":"2017-01-01"}
{"index":{"_id":2}}
{"articleID":"KDKE-B-9947-#kL5","urID":1,"hidden":fal,"postDate":"2017-01-02"}
{"index":{"_id":3}}dig
{"articleID":"JODL-X-1937-#pV7","urID":2,"hidden":fal,"postDate":"2017-01-01"}
{"index":{"_id":4}}
{"articleID":"QQPX-R-3956-#aD8","urID":2,"hidden":true,"postDate":"2017-01-02"}
1. 根据⽤户ID搜索帖⼦
{
"query":{
"constant_score":{
"filter":{
免费在线翻译"term":{
"urID":1
}
}
}
}
}
1. query:搜索
2. constant_score:默认
3. filter:过滤
4. term filter/query:对搜索⽂本不分词,直接拿去倒排索引中匹配,你输⼊的是什么,就去匹配什么。⽐如说,如果对搜索⽂本进⾏
分词的话,“helle world” --> “hello”和“world”,两个词分别去倒排索引中匹配term,“hello world” --> “hello
world”,直接去倒排索引中匹配“hello world”
5. 相当于SQL中的单个where条件
6. 搜索其他字段类似
2. bool组合多个filter条件来搜索数据
1. 搜索发帖⽇期为2017-01-01,或者帖⼦ID为XHDK-A-1293-#fJ3的帖⼦,同时要求帖⼦的发帖⽇期绝对不为2017-01-02 1. SQL实现
lect*
from forum.article
where(post_date='2017-01-01'or article_id='XHDK-A-1293-#fJ3')
and post_date!='2017-01-02'
GET /forum/article/_arch
{
"query": {
"constant_score": {
"filter": {
"bool": {
"should": [
{"term": { "postDate": "2017-01-01" }},
{"term": {"articleID": "XHDK-A-1293-#fJ3"}}
],
"must_not": {
"term": {
"postDate": "2017-01-02"
}
}
}
}
}
}
}
2. 搜索帖⼦ID为XHDK-A-1293-#fJ3,或者是帖⼦ID为JODL-X-1937-#pV7⽽且发帖⽇期为2017-01-01的帖⼦
lect*
from forum.article
where article_id='XHDK-A-1293-#fJ3'
or(article_id='JODL-X-1937-#pV7'and post_date='2017-01-01')
GET /forum/article/_arch
{
"query":{
"constant_score":{
"filter":{
"bool":{
"should":[
{
"term":{
"articleID":"XHDK-A-1293-#fJ3"
}
},
{
"bool":{
"must":[
{
"term":{
"articleID":"JODL-X-1937-#pV7"
}
},
{
"term":{
"postDate":"2017-01-01"
}
}
]
}
}
]
}
武汉室内设计
}
}
}
}
1. bool:组合多个过滤条件
1. must:必须匹配
2. must_not:必须不匹配
3. should:可以匹配其中任意⼀个即可
2. bool可以嵌套
3. 相当于SQL中的多个and条件
3. terms搜索多个值以及多值搜索结果优化0. 原始数据
POST /forum/article/_bulk
{"update":{"_id":"1"}}
{"doc":{"tag":["java","hadoop"]}}
{"update":{"_id":"2"}}
grenoble
{"doc":{"tag":["java"]}}
{"update":{"_id":"3"}}
{"doc":{"tag":["hadoop"]}}
{"update":{"_id":"4"}}
{"doc":{"tag":["java","elasticarch"]}}
1. term: {“field”: “value”}
2. terms: {“field”: [“value1”, “value2”]}
3. 相当于sql中的in
lect* from tbl where col in ("value1","value2")
1. 搜索articleID为KDKE-B-9947-#kL5或QQPX-R-3956-#aD8的帖⼦
GET /forum/article/_arch
{
"query":{
"constant_score":{
"filter":{
"terms":{
"articleID":[
"KDKE-B-9947-#kL5",
"QQPX-R-3956-#aD8"
]
}
}
}
}
}
2. 搜索tag中包含java的帖⼦
GET /forum/article/_arch
{
"query":{
"constant_score":{
"filter":{
"terms":{
"tag":["java"]
}
}
}
}
}
1. 此时会将tag中包含java字符串的结果返回
"took":2,
"timed_out":fal,
"_shards":{
"total":5,
"successful":5,
"failed":0
},
"hits":{
"total":3,
"max_score":1,
汉堡包的英文单词"hits":[
{
"_index":"forum",
"_type":"article",
"_id":"2",
"_score":1,
"_source":{
"articleID":"KDKE-B-9947-#kL5",padding
"urID":1,
"hidden":fal,
"postDate":"2017-01-02",
"tag":[
"java"
]
}
英语学习机},
{
"_index":"forum",
"_type":"article",
"_id":"4",
"_score":1,
"_source":{
"articleID":"QQPX-R-3956-#aD8",
"urID":2,
"hidden":true,
"postDate":"2017-01-02",
"tag":[
jaja"java",
"elasticarch"
不可预见费
]
}
},
{
"_index":"forum",
"_type":"article",
"_id":"1",
"_score":1,
"_source":{
"articleID":"XHDK-A-1293-#fJ3",
member是什么意思
"urID":1,
"hidden":fal,
"postDate":"2017-01-01",
"tag":[
"java",
"hadoop"
]
}
}
]
}
}
3. 优化搜索结果,仅仅搜索tag只包含java的帖⼦