Elastsearch basic query (open)

Elastsearch basic query

Elasticsearch is a very powerful full-text search engine. The purpose of using elasticsearch is to quickly query the desired data.

Basic query: query with elastic search built-in query criteria

Composite query: a composite query that combines multiple basic queries

Filter: when querying, filter out the desired data through the filter condition without affecting the scoring

term, terms query
from,size
Return version number
match query
Ascending Descending
prefix match query
Range range query
wildcard query
Fuzzy fuzzy query
More? Like? This & more? Like? This? Field query

#View test data
GET /library/books/_mget
{
  "ids":["1","2","3","4","5","6","7","8"]
}

#Viewing the mapping information of the library
GET /library/_mapping

#Viewing the mapping information of Shakespear 
GET /shakespeare/_mapping

#---------------------------------
#Simple query

#Search specifying index name and type name de
GET /library/books/_search?q=title:elasticsearch

#Search without type name for specified index name   
GET /library/_search?q=title:mongodb

#Search without index or type name
GET /_search?q=title:elasticsearch

#---------------------------------------
#term query
GET /library/books/_search
{
  "query":{
    "term": {
      "preview": ["ealsticsearch","book"],
      "minmum_match": 1
    }
  }
}

#--------------------------------------------
#Control the number of queries returned 

#from and size
#Equivalent to limit in mysql
#From: from which result to return
#size: defines the maximum number of returned results 
GET /library/books/_search?q=title:elasticsearch

GET /library/books/_search
{
  "from":1,
  "size":2,
  "query":{
    "term": {
      "title":"elasticsearch"
    }
  }
}

#-------------------------------------------------
#

#Return version number

GET /library/books/_search
{
  "version":true
  "query": {
    "term": {
      "preview":"elasticsearch"
    }
  }
}

#----------------------------------------------------
#match query 
#match query can accept text, number, date and other data types 
#The difference between match and term is: when a match query is performed, elastic search will provide an appropriate analyzer according to the given fields, while term query will not have the process of analyzer analysis      
GET /library/books/_search
{
  "query":{
    "match": {
      "perview":"elasticsearch"
    }
  }
}


GET /library/books/_search
{
  "query":{
    "match":{
      "price":11
    }
  }
}

#Query by match all 
#chaxun queries all documents under the specified index 
GET /library/books/_search
{
  "query":{
    "match_all": {}
  }
}

#Query by match 
#Phrase query, slop defines how many unknown words are separated between keywords
GET /library/books/_search
{
  "query":{
    "match_phrase":{
      "preview":{
        "query":"Elasticsearch ,distributed",
        "slop":2
      }
    }
  }
}

#Multi match query 
#Multiple fields can be specified 
#For example, search for documents with Elasticsearch keywords in the title and preview fields 
GET /library/books/_search
{
  "query":{
    "multi_match":{
      "query":"Elasticsearch",
      "fields":["title","perview"]
    }
  }
}

#-------------------------------------------------
#

#Specify the fields to return 
#Note that only fields with store as yes can be returned 
GET /library/books/_search
{
  "fields":["preview"],
  "query":{
    "match":{
      "perview":"elasticsearch"
    }
  }
}


#Control the loaded fields through partial fields 
GET /library/books/_search
{
  "partial_fields": {
    "partial":{
      "include": ["preview"],
      "exclude":["title,price"]

    }

  },
    "query":{
      "match_all": {}
  }
}


#You can also add wildcards*
GET /library/books/_search
{
  "partial_fields": {
    "partial":{
      "include":["pr*"]
      "exclude":["tit*"]
    }
  },
  "query":{
    "match_all": {}
  }
}


#----------------------------------------------------
#paixu sorting 

#sort the results 
#desc descending 
#asc ascending
GET /library/books/_search
{
  "query":{
    "match_all": {}
  },
  "sort":[
    {
      "price":{
        "order":"desc"
      }
    }
  ]
}

GET /library/books/_search
{
  "query":{
    "match_all": {}
  },
  "sort":[
    {
      "price": {
        "order": "asc"
      }
    }
  ]
}


#------------------------------------------------
#Control range 
#Range queries: range queries 
#Yes from,to, include_lower,include_upper,boost Isoparametric #Include? Lower: whether to include the left boundary of the range. The default value is true
#Include? Upper: whether to include the right boundary of the range. The default value is true
GET /library/books/_search
{
  "query":{
    "range": {
      "publish_data": {
        "from": "2017-1-1",
        "to": "2017-12-29"
      }
    }
  }
}


GET /library/books/_search
{
  "query": {
    "range": {
      "price": {
        "from": 10,
        "to": 300,
        "include_lower":true,
        "include_upper":false
      }
    }
  }
}

#--------------------------------------------------
#
#wildcardchaxun query: yunxunishing allows you to use the tongpeifu wildcard * he and? To query 
# *ABCD on behalf of one or more characters 
# ? jindaibiaoyigezifu represents only one character 
#Note: this query affects performance 
GET /library/books/_search
{
  "query": {
    "wildcard": {
      "preview": "rab*"
    }
  }
}

GET /library/books/_search
{
  "query": {
    "wildcard": {
      "preview": "luc?ne"
    }
  }
}

#--------------------------------------------------
#Fuzzy query 
#value: keyword of query 
#boost: set the value of the query. The default value is 1.0

Keywords: ElasticSearch MongoDB MySQL

Added by natronp on Mon, 04 May 2020 06:30:21 +0300