3.3.1.布尔组合(bool)
bool把各种其他查询通过must(与)、must_not(非)、should(或)的方式进行组合
例如:
GET /jobinfo/_search
{
"_source": [
"jobAddr",
"jobName",
"companyAddr"
],
"query": {
"bool": {
"must": [
{
"match": {
"jobAddr": "北京"
}
}
],
"must_not": [
{
"match": {
"jobName": "工程师"
}
}
],
"should": [
{
"match": {
"companyAddr": "北京"
}
}
]
}
},
"size": 100
}
3.3.2.范围查询(range)
range查询找出那些落在指定区间内的数组或者时间
GET /jobinfo/_search
{
"_source": [
"jobAddr",
"jobName",
"companyAddr"
],
"query": {
"range": {
"pice": {
"gte": 10,//大于等于10
"lte": 20//小于等于20
}
}
},
"size": 100
}
3.3.3.模糊查询(funzzy)
fuzzy查询是term查询的模糊等价。它允许用户搜索词条与实际词条的拼写出现偏差,但是偏差的编辑距离不得超过2:
GET /jobinfo/_search
{
"_source": [
"jobAddr",
"jobName",
"companyAddr"
],
"query": {
"fuzzy": {
"jobName": {"value": "ava","fuzziness": 2}
}
},
"size": 100
}
结果也能查询出 "jobName": "Java中级开发工程师",我们可以通过fuzziness来指定允许的编辑距离,
值:0,1,2, 允许的最大edits
值:AUTO,依据term长度
0..2必须完全匹配
3..5最大允许1个
` >5最大允许2个
3.3.4.过滤查询(filter)
条件查询中进行过滤
所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么久不要把过滤条件作为查询条件来用。而是使用filter方式:
GET /jobinfo/_search
{
"_source": [
"jobAddr",
"jobName",
"companyAddr",
"salaryMin"
],
"query": {
"bool": {
"must": [
{
"match": {
"jobName": "Java"
}
}
],
"filter": {
"range": {
"salaryMin": {
"gte": 150000,
"lte": 200000
}
}
}
}
},
"size": 100
}
3.3.5排序
3.3.5.1.单字段排序
sort可以让我们按照不同的字段进行排序,并且通过order指定排序的方式
GET /jobinfo/_search
{
"_source": [
"jobAddr",
"jobName",
"companyAddr",
"salaryMin"
],
"query": {
"match_all": {}
},
"sort": [
{
"salaryMin": {
"order": "desc"
}
}
],
"size": 100
}
3.3.5.2多字段排序
假定我们想要结合使用salaryMin和salaryMax进行查询,并且匹配的结果首先按照salaryMin排序,然后按照相关性得分排序:
GET /jobinfo/_search
{
"_source": [
"jobAddr",
"jobName",
"companyAddr",
"salaryMin"
],
"query": {
"match": {
"jobName": "中级"
}
},
"sort": [
{
"salaryMin": {
"order": "desc"
}
},{
"_score":{
"order": "desc"
}
}
],
"size": 100
}
高亮显示
{
"_source": [
"jobAddr",
"jobName",
"companyAddr",
"salaryMin"
],
"query": {
"match": {
"jobName": "中级"
}
},
"sort": [
{
"salaryMin": {
"order": "desc"
}
},{
"_score":{
"order": "desc"
}
}
],
"highlight": {
"fields": {
"jobName": {}
}
},
"size": 100
}
输出:
"hits": [
{
"_index": "jobinfo",
"_type": "JobInfoField",
"_id": "75",
"_score": 3.0703192,
"_source": {
"jobName": "Java中级开发工程师",
"companyAddr": "深圳市展泰科技服务有限公司",
"jobAddr": "深圳-南山区 ",
"salaryMin": 180000
},
"highlight": {
"jobName": [
"Java中级开发工程师"
]
},
"sort": [
180000,
3.0703192
]
}
]
我们发现每条查询出来的额结果都有highlight属性。改属性中有需要高亮显示的字段,并且用标签展现到前端会切斜显示,可以适当加一下css样式是关键字更明显的显示。