ES索引&文档查询常用操作

1.索引操作

(1)创建索引
//创建animals索引
curl --location --request PUT 'http://localhost:9200/animals' \
--header 'Content-Type: application/json' \
--data-raw '{
    "settings": {
        "number_of_shards": 2,
        "number_of_replicas": 0
    },
    "mappings": {
            "properties": {
                "kind": {
                    "type": "keyword"
                },
                "name": {
                    "type": "keyword"
                },
                "number": {
                    "type": "integer"
                }
            }
    }
}'
(2)查看索引
#查看索引相关信息
curl --location --request GET 'http://localhost:9200/animals'

#查看索引的文档总数
curl --location --request GET 'http://localhost:9200/animals/_count'

#查看以animals开头的索引
curl --location --request GET 'http://localhost:9200/_cat/indices/animals*?v&s=index'

#查看状态为绿的索引
curl --location --request GET 'http://localhost:9200/_cat/indices?v&health=green'

cat APIs
verbose :GET /_cat/XXX/?v ,详细输出
Sort: 排序,s=column1,根据column1升序排序

(3)删除index
curl --location --request DELETE 'http://localhost:9200/animals'

2.文档CRUD操作

Document APIs

(1)Index

如果id不存在创建,存在则删除,再创建,版本会增加

curl --location --request PUT 'http://localhost:9200/animals/_doc/1' \
--header 'Content-Type: application/json' \
--data-raw '{
    "kind":"金毛",
    "name":"Jim",
    "number":1
}'
(2)Create
  • 不指定id,自动生成id
curl --location --request POST 'http://localhost:9200/animals/_doc' \
--header 'Content-Type: application/json' \
--data-raw '{
    "kind":"柴犬",
    "name":"chai",
    "number":2
}'
  • 指定id,如果id已经存在,报错
curl --location --request PUT 'http://localhost:9200/animals/_doc/2?op_type=create' \
--header 'Content-Type: application/json' \
--data-raw '{
    "kind":"博美",
    "name":"mei",
    "number":2
}'
curl --location --request PUT 'http://localhost:9200/animals/_create/3' \
--header 'Content-Type: application/json' \
--data-raw '{
    "kind":"拉布拉多",
    "name":"duo",
    "number":3
}'
(3)Read

指定查询的索引

语法 范围
/_search 集群上所有索引
/index1/_search index1
/index1,index2 /_search index1,index2
/index*/_search 以index开头的索引
//查询指定id的文档
curl --location --request GET 'http://localhost:9200/animals/_doc/2'
(4)Update

文档必须已经存在,更新会对相应字段进行修改

curl --location --request POST 'http://localhost:9200/animals/_update/2' \
--header 'Content-Type: application/json' \
--data-raw '{
    "doc":{
        "number":2
    }
}'
(5)Delete
curl --location --request DELETE 'http://localhost:9200/animals/_doc/Jd7IuoIBSRe4rlchkqbA'

3.Search API

(1) URI Search(支持Get)

在url中使用查询参数

q:表示查询的字符串(语法:Query string query)
df:指定默认查询的字段。不指定会对全部字段进行查询
profile:查看查询是如何被执行的

//查询animals索引全部内容
curl --location --request POST 'http://localhost:9200/animals/_search' \
--header 'Content-Type: application/json' \
--data-raw '{}'

//查询animals索引中kind=金毛的文档
curl --location --request GET 'http://localhost:9200/animals/_search?q=kind:"金毛"'

//查询索引animals,movies中id=1的文档
curl --location --request GET 'http://localhost:9200/animals,movies/_search?q=_id:1'

curl --location --request GET 'http://localhost:9200/animals/_search?q=1&df=_id'
//泛查询
curl --location --request GET 'http://localhost:9200/animals/_search?q=1'

//term 查询:title包括Beautiful OR Mind
curl --location --request GET 'http://localhost:9200/movies/_search?q=title:(Beautiful Mind)'

//Phrase查询,title为Beautiful Mind
curl --location --request GET 'http://localhost:9200/movies/_search?q=title:"Beautiful Mind"'

//查询title含Beautiful,但Mind为泛查询(即全部字段都进行查询mind)
curl --location --request GET 'http://localhost:9200/movies/_search?q=title:Beautiful Mind'

//范围查询
curl --location --request GET 'http://localhost:9200/movies/_search?q=year:>2000'

curl --location -g --request GET 'http://localhost:9200/movies/_search?q=year:[2018 TO 2022]'
//布尔查询(-:must not present ;+:must present;without +/-: optional)
curl --location --request GET 'http://localhost:9200/movies/_search?q=title:(-The +Last)'

//查询term模糊匹配(~1:表示编辑距离允许为1,允许一个字符不匹配)
//beautifl~1:仍然允许查询beautiful
curl --location --request GET 'http://localhost:9200/movies/_search?q=title:(beautifl~1)'
//查询phrase近似度匹配
//可以查询出与"Lord Rings"最大编辑距离为2的内容,如Lord of the Rings
curl --location --request GET 'http://localhost:9200/movies/_search?q=title:"Lord Rings"~2'
(2)Request Body Search(支持Get、Post)

使用基于json格式的DSL(Query DSL )

DSL.png

  • Match query
    match query是执行全文搜索的标准查询
###.match query ###

//查询animals索引全部文档
curl --location --request GET 'http://localhost:9200/animals/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "profile": true,
    "query": {
        "match_all": {}
    }
}'
//分页查询
//ignore_unavailable=true,可以忽略尝试访问不存在的索引“404_idx”导致的报错
curl --location --request POST 'http://localhost:9200/movies,404_idx/_search?ignore_unavailable=true' \
--header 'Content-Type: application/json' \
--data-raw '{
  "profile": true,
  "from":10,
  "size":10,
  "query":{
    "match_all": {}
  }
}'
//term查询,title包括last OR christmas
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": {
    "match": {
      "title": "last christmas"
    }
  }
}
'
//term查询,title包括last AND christmas
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": {
    "match": {
      "title": {
         "query": "last christmas",
         "operator": "and"
       }
    }
  }
}
'
//phrase查询
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": {
    "match_phrase": {
      "title":{
        "query": "one love"

      }
    }
  }
}'
//phrase查询-近似度匹配
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": {
    "match_phrase": {
      "title":{
        "query": "one love",
        "slop": 1
      }
    }
  }
}'

//排序
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
  "sort":[{"year":"desc"}],
  "query":{
    "match_all": {}
  }
}
'
//过滤source
curl --location --request POST 'http://localhost:9200/animals/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
  "_source":["kind"],
  "query":{
    "match_all": {}
  }
}
'
  • Painless script query
curl --location --request POST 'http://localhost:9200/animals/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
  "script_fields": {
    "new_field": {
      "script": {
        "lang": "painless",
        "source": "'\''小 '\''+doc['\''kind'\''].value"
      }
    }
  },
  "query": {
    "match_all": {}
  }
}'
  • Query string query
### query string ###

//查询title包含last AND christmas
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "profile": true,
    "query":{
        "query_string":{
            "default_field": "title",
            "query": "last AND christmas"
        }
    }
}
'
//查询title包含last/christmas
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "profile": true,
    "query":{
        "query_string":{
            "default_field": "title",
            "query": "last christmas"
        }
    }
}
'
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "profile": true,
    "query":{
        "query_string":{
            "default_field": "title",
            "query": "last OR christmas"
        }
    }
}
'
  • Simple query string query
### simple  query string ###

//查询title包含last/AND/christmas
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "profile": true,
    "query":{
        "simple_query_string":{
            "fields": ["title"],
            "query": "last AND christmas"
        }
    }
}
'
//查询name包含last AND christmas
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "profile": true,
    "query":{
        "simple_query_string":{
            "fields": ["title"],
            "query": "last  christmas",
            "default_operator": "AND"
        }
    }
}
'
  • Term query

term query会查询一个准确匹配字段的分词,包括空格和区分大小写,查询文本避免使用term query,而应使用match query。

### term query ###
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "profile": true,
    "query":{
        "term": {
            "year": {
                "value": 2018
            }
        }
    }
}
'
  • Range query
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "query": {
        "range" : {
            "year" : {
                "gte" : 2018,
                "lte" : 2200,
                "boost" : 2.0
            }
        }
    }
}'
  • Boolean query
    boolean query是一个或多个查询子句的组合,每一个子句就是一个子查询

子查询组合方式:
must:返回的文档必须满足must子句的条件,并且参与计算分值
should:选择性匹配子查询,类似“或”。在一个bool查询中,如果没有must或者filter,有一个或者多个should子句,那么只要满足一个就可以返回。
must_not:必须不匹配,不参与算分,类似“非”
filter:必须匹配,不参与算分

//查询title必须包含Last,genre必须包含Crime,year为2018的文档
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": {
    "bool" : {
      "must" : [{
        "match" : { "title" : "Last"  } 
      },{
        "match" : { "genre" : "Crime"  }
      }],
      "should": {
        "term" : {"id":"5893"}
      },
      "filter": [ 
        { "range": { "year": { "gte": "2008" }}}
      ]
    }
  }
}'

你可能感兴趣的:(ES索引&文档查询常用操作)