基本操作
簡單的搜索
GET /test3/_doc/1
GET /test3/_doc/_search?q=name:小葉曲
GET /test3/_doc/_search?q=name:小葉曲11
簡答的條件查詢,可以根據默認的映射規則,產生基本的查詢!
複雜操作搜索
select ( 排序,分頁,高亮,模糊查詢,精準查詢!)
查詢匹配
GET /test3/_doc/_search
{
"query": {
"match":{
"name": "44"
}
}
}
輸出結果,不想要那麼多
GET /test3/_doc/_search
{
"query": {
"match":{
"name": "小葉曲11"
}
},
"_source": ["name","birthday"]
}
排序
desc: 倒序 asc:正序
GET /test3/_doc/_search
{
"query": {
"match":{
"name": "小葉曲"
}
},
"_source": ["name","age"] ,
"sort": [
{
"age":{
"order":"desc"
}
}
]
}
分頁查詢
GET /test3/_doc/_search
{
"query": {
"match":{
"name": "小葉曲"
}
},
"_source": ["name","birthday"] ,
"sort": [
{
"age":{
"order":"asc"
}
}
],
"from": 0,
"size": 2
}
數據下標還是從0開始的~
布爾值查詢
must (and),所有的條件都要符合 where age = 3 and name = 小葉曲
GET /test3/_doc/_search
{
"query": {
"bool":{
"must":[
{
"match":{
"name": "小葉曲"
}
},
{
"match":{
"age": "3"
}
}
]
}
}
}
should(or),所有的條件都要符合 where age = 3 or name = 小葉曲
GET /test3/_doc/_search
{
"query": {
"bool":{
"should":[
{
"match":{
"name": "11"
}
},
{
"match":{
"age": "3"
}
}
]
}
}
}
must_not (not)
GET /test3/_doc/_search
{
"query": {
"bool":{
"must_not":[
{
"match":{
"name": "11"
}
},
{
"match":{
"age": "3"
}
}
]
}
}
}
過濾器 filter
- gt 大於
- gte 大於等於
- lt 小於
- lte 小於等於!
GET /test3/_doc/_search
{
"query": {
"bool":{
"must":[
{
"match":{
"name": "小葉曲"
}
}
],
"filter": {
"range": {
"age": {
"gte": 1,
"lte": 3
}
}
}
}
}
}
匹配多個條件!
GET /test3/_doc/_search
{
"query": {
"match":{
"tags": "靚 技術"
}
}
}
精確查詢
term 查詢是直接通過倒排索引指定的詞條進程精確查找的!
關於分詞:
- term ,直接查詢精確的
- match,會使用分詞器解析!(先分析文檔,然後在通過分析的文檔進行查詢!)
兩個類型 text
keyword
GET _analyze
{
"analyzer": "keyword",
"text": "張三說我喜歡你"
}
GET _analyze
{
"analyzer": "standard",
"text": "張三說我喜歡你"
}
創建 testdb 並指定字段的類型:
PUT /testdb
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"desc": {
"type": "keyword"
}
}
}
}
GET /testdb/_doc/_search
{
"query": {
"term":{
"desc": "法外狂徒 desc"
}
}
}
多個值匹配精確查詢
GET /testdb/_doc/_search
{
"query": {
"bool":{
"should":[
{
"term":{
"t1": "44"
}
},
{
"term":{
"t1": "55"
}
}
]
}
}
}
高亮查詢
GET /test3/_doc/_search
{
"query": {
"match":{
"name": "小葉曲11"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
GET /test3/_doc/_search
{
"query": {
"match":{
"name": "小葉曲11"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"name": {}
}
}
}