Elasticsearch 常用查询

Elasticsearch 常用查询,第1张

Elasticsearch 常用查询 Elasticsearch 常用查询

es入门使用
es索引使用
es分词使用

常用条件查询搜索
# 查看索引库
GET /elasticsearch_test/


# 查看所有
GET /elasticsearch_test/_search


# 全文搜索
# 全文搜索能够搜索已分析的文本字段,
# 使用索引期间应用于统一分析器处理查询字段
# 全文搜索分为匹配搜索和短语搜索、query_string、多字段匹配搜索

GET /elasticsearch_test/_search
{
  "query": {
    "match_all": {
    }
  }
}

# 匹配搜索
# 可以对一个字段进行匹配 模糊查询,接收一个文本对其进行分词分析
# 再组织成一个布尔查询,可以通过operator组成一个组合 *** 作,or and 默认是or


# or
# spring cloud实战  分词查询 spring 、 cloud 、 实战 这三个or的关系
POST /elasticsearch_test/_search 
{
  "query": {
    "match": {
      "description": "spring cloud实战"
    }
  }
}

# and
# spring cloud实战  分词查询 spring 、 cloud 、 实战 这三个and 的关系
POST /elasticsearch_test/_search 
{
  "query": {
    "match": {
      "description":{
      "query": "spring cloud实战",
      "operator": "and"
    }
  }
  }
}

# 短语搜索

# 用来只对一个字段进行短语查询 可以指定analyzer、 slop移动因子

POST /elasticsearch_test/_search
{
  "query": {
    "match_phrase": {
      "description": {
        "query": "cloud实战",
        "slop": 0
      }
    }
  }
}

## query_string 不需要指定字段即进行全文搜索查询,同时也可以指定在那些字段上查询

# 默认 

POST /elasticsearch_test/_search 
{
  
  "query": {
    "query_string": {
      "query": "实战"
    }
  }
  
}

# 指定字段
POST /elasticsearch_test/_search 
{
  
  "query": {
    "query_string": {
      "query": "实战",
      "default_field": 
        "description"
 
    }
  }
  
}

# 逻辑查询 
# AND OR必须大写 默认OR
POST /elasticsearch_test/_search 
{
  
  "query": {
    "query_string": {
      "query": "spring AND 实战",
      "default_field": 
        "description"
 
    }
  }
  
}


# 模糊查询

# ~2 表示默认 2 表示向后模糊几个分词
POST /elasticsearch_test/_search 
{
  
  "query": {
    "query_string": {
      "query": "golang~2",
      "default_field": 
        "name"
 
    }
  }
  
}


# 多字段支持
POST /elasticsearch_test/_search 
{
  
  "query": {
    "query_string": {
      "query": "golang~2",
        "fields": ["description","name"]
  }
  
}

}



# 使用*匹配多字段
POST /elasticsearch_test/_search 
{
  
  "query": {
    "query_string": {
      "query": "golang~2",
        "fields": ["description","n*"]
  }
  
}

}




# 词条级搜索 term查询
# 精确化查找,与全文相反,不分析搜索词,词条和存在字段中的术语内容完全匹配

POST /elasticsearch_test/_search
{
  "query": {
    "term": {
      "price":   "100"
      
    }
  }
}

POST /elasticsearch_test/_search
{
  "query": {
    "term": {
      "studymodel":   "201001"
      
    }
  }
}

# 不能完全匹配
POST /elasticsearch_test/_search
{
  "query": {
    "term": {
      "description":   "spring cloud实战"
      
    }
  }
}


# terms 词条集合搜索
POST /elasticsearch_test/_search
{
  "query": {
    "terms": {
      "description":  [ "spring","cloud","实战"]
      
    }
  }
}


# 范围查询
POST /elasticsearch_test/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 10,
        "lte": 200
      }
      
    }
  }
}

POST /elasticsearch_test/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 10,
        "lte": 200
      }
      
    }
  }
}


# 时间范围以及格式化
GET /elasticsearch_test/_search
{
  "query": {
    "range": {
      "timestamp": {
        "gte": "10/07/2020",
        "lte":"10/12/2022",
        "format": "dd/MM/yyyy"
      }
      
    }
  }
}


# 不为空查询
# 相当于sql中 is not null
GET /elasticsearch_test/_search
{
  "query": {
    "exists": {
      "field": "price"
    }
  }
}


# 词项前缀
# name字段 s开头的
GET /elasticsearch_test/_search
{
  "query": {
    "prefix": {
      "name": "s"
    }
  }
}


# 通配符

# spring s*r*
GET /elasticsearch_test/_search
{
  "query": {
    "wildcard": {
      "name": "s*r*"
    }
  }
}


# boost 通配符 后几个
GET /elasticsearch_test/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "s*",
        "boost": 8
      
    }
  }
}
}


# 正则搜索
GET /elasticsearch_test/_search
{
  "query": {
    "regexp": {
      "name":"s.*"
  }
}
}


# 模糊搜索

GET /elasticsearch_test/_search
{
  "query": {
    "fuzzy": {
      "name":"golang"
  }
}
}


GET /elasticsearch_test/_doc/2

# id集合搜索
GET /elasticsearch_test/_search
{
  "query": {
    "ids": {
      "values": ["1","2"]
    }
}
}

排序
# 排序

#  _score 排序
GET /elasticsearch_test/_search
{
"sort": [
  {"_score":"asc"},
  
  {
    "price": {
      "order": "desc"
    }
  }
]
}

#  字段排序
GET /elasticsearch_test/_search
{
  "query": {
  
    "query_string": {
      "default_field": "description",
      "query": "spring or go"
    }
   
},
"sort": [
  {
    "price": {
      "order": "desc"
    }
  }
]
}


#多字段排序

#  _score 排序
GET /elasticsearch_test/_search
{
  "query": {
  
    "query_string": {
      "default_field": "description",
      "query": "spring or go"
    }
   
},
"sort": [
  {"_score":"asc"},
  
  {
    "price": {
      "order": "desc"
    }
  },
  
    {
    "_id": {
      "order": "asc"
    }
  }
]
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5695986.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存