ElasticSearch第六篇:高级查询

2019-10-22  本文已影响0人  小螺丝钉cici

子条件查询:特定字段查询所指特定值
复合条件查询以一定的逻辑组合子条件查询

子条件查询

Query context:

在查询过程中,除了判断文档是否满足查询条件外,ES还会计算一个_socre来标识匹配的程度,旨在判断文档和查询条件匹配的有多好。
(匹配度为多少)

常用查询:
1)全文本查询:针对文本类型数据(模糊匹配,习语匹配,多个字段的匹配查询)
2)字段级别查询:针对结构化数据,如数字,日期等

image.png
{
    "query":{
        "match":{
            "first_name":"小枫cici"
        }
    }
}
这种查询语法会匹配出4条数据;小枫cici /cici  / 小枫
它将小枫cici 拆开了小枫/cici两个词语。只要能匹配的都查询出来了
{
    "query":{
        "match_phrase":{
            "first_name":"小枫cici"
        }
    }
}
此时就只匹配了id=5的这一条数据
{
    "query":{
        "multi_match":{
            "query": "小枫cici",
            "fields": ["last_name" , "first_name"]
        }
    }
}
// 匹配出“小枫”字符串
{
    "query":{
        "query_string":{
            "query": "小枫"
    
        }
    }
}
// 匹配出“小枫” 或者“cici”字符串
{
    "query":{
        "query_string":{
            "query": "cici OR 小枫"
    
        }
    }
}

{
    "query":{
        "query_string":{
            "query": "cici OR 24",
            "fields":["first_name","age"]
        }
    }
}
查询年龄为100的数据
{
    "query":{
        "term":{
            "age": 100
        }
    }
}
查询年龄>=20,<100的数据
{
    "query":{
        "range":{
            "age": {
                "gte":20,
                "lt":100 // lt小于; lte小于等于 ;e=equals
            }
        }
    }
}
Filter context

filter 对数据进行过滤的。并且对数据进行缓存,比query速度快,性能高一些。
在查询过程中,只判断该文档是否满足条件,只有 Yes 或者 No。

// 只查询年龄=100的数据
{
    "query":{
        "bool":{
            "filter": {
                "term":{
                    "age":100
                }
            }
        }
    }
}

复合条件查询

{
    "query":{
        "match":{
            "first_name":"cici"
            }
        }
}

查询出来的数据分数不一样。 
_score": 0.18232156,"_score": 0.2876821,
{
    "query":{
        "constant_score":{
            "filter":{
                "match":{
                    "first_name":"cici"
                }
                }
        
            }
        }
}
跟上面返回的数据一致;但score都是1
{
"query":{
        "constant_score":{
            "filter":{
                "match":{
                    "first_name":"cici"
                    }
                },
                "boost":2
        
            }
        }
}
跟上面返回的数据一致;但score都是2
{
    "query":{
        "bool":{
            "should":[
                {
                "match":{
                    "first_name":"cici"
                }
                },
                {
                 "match":{
                    "age":"28"
                }
                }
                ]
        
            }
        }
}
{
    "query":{
        "bool":{
            "must/must_not":[ //此处两个语法都可用
                {
                "match":{
                    "first_name":"小枫"
                        }
                },
                {
                 "match":{
                    "age":"28"
                        }
                    }
                ],
                "filter":[
                {
                "term":{
                    "last_name":"cici"
                    }
                }
                ]
            }
        }
}
上一篇 下一篇

猜你喜欢

热点阅读