elasticsearch从入门到入门系列(二)---快速入门B

1.文档操作

  • 新增文档
请求(一):
    PUT localhost:9200/nba/_doc/1 (指定id)
    
    {
        "name": "哈登", 
        "team_name": "⽕火箭", 
        "position": "得分后卫", 
        "play_year": "10", 
        "jerse_no": "13"
    }
响应:
 	{
	    "_index": "nba", 
	    "_type": "_doc", 
	    "_id": "1", 
	    "_version": 1, 
	    "result": "created", 
	    "_shards": {
	        "total": 2, 
	        "successful": 1, 
	        "failed": 0
	    }, 
	    "_seq_no": 0, 
	    "_primary_term": 1
	}

请求(二):
    POST localhost:9200/nba/_doc (不不指定id)
    
    {
        "name": "库⾥里里", 
        "team_name": "勇⼠士", 
        "position": "组织后卫", 
        "play_year": "10", 
        "jerse_no": "30"
    }
响应:
	{
	    "_index": "nba", 
	    "_type": "_doc", 
	    "_id": "cVi582sB6wrnBnZnFqog", 
	    "_version": 1, 
	    "result": "created", 
	    "_shards": {
	        "total": 2, 
	        "successful": 1, 
	        "failed": 0
	    }, 
	    "_seq_no": 1, 
	    "_primary_term": 1
	}

说明:请使用postman进行请求   
  • 自动创建索引
    • 查看auto_create_index开关状态,请求http://localhost:9200/_cluster/settings

    • 当索引不不存在并且auto_create_index为true的时候,新增⽂文档时会⾃自动创建索引

    • 修改auto_create_index状态

      请求:
          PUT localhost:9200/_cluster/settings
      响应:
           
      {
          "persistent": {
              "action.auto_create_index": "false"
          }
      }
      
       
      {
          "acknowledged": true,
          "persistent": {
              "action": {
                  "auto_create_index": "false"
              } 
          },
          "transient": {}
      }

       

    • 当auto_create_index=false时,指定⼀个不不存在的索引,新增⽂文档

      请求:
          PUT localhost:9200/wnba/_doc/1
      
          {
              "name": "杨超越", 
              "team_name": "梦之队", 
              "position": "组织后卫", 
              "play_year": "0", 
              "jerse_no": "18"
          }
      响应:
      	{
      	    "error": {
      	        "root_cause": [
      	            {
      	                "type": "index_not_found_exception", 
      	                "reason": "no such index [wnba]", 
      	                "resource.type": "index_expression", 
      	                "resource.id": "wnba", 
      	                "index_uuid": "_na_", 
      	                "index": "wnba"
      	            }
      	        ], 
      	        "type": "index_not_found_exception", 
      	        "reason": "no such index [wnba]", 
      	        "resource.type": "index_expression", 
      	        "resource.id": "wnba", 
      	        "index_uuid": "_na_", 
      	        "index": "wnba"
      	    }, 
      	    "status": 404
      	}

       

    • 当auto_create_index=true时,指定⼀一个不不存在的索引,新增⽂文档

{
    "name": "杨超越", 
    "team_name": "梦之队", 
    "position": "组织后卫", 
    "play_year": "0", 
    "jerse_no": "18"
}

响应:

{
    "_index": "wnba", 
    "_type": "_doc", 
    "_id": "1", 
    "_version": 1, 
    "result": "created", 
    "_shards": {
        "total": 2, 
        "successful": 1, 
        "failed": 0
    }, 
    "_seq_no": 0, 
    "_primary_term": 1
}
  • 查看文档
请求:
    GET localhost:9200/nba/_doc/1
响应:
	{
	    "_index": "nba", 
	    "_type": "_doc", 
	    "_id": "1", 
	    "_version": 3, 
	    "_seq_no": 3, 
	    "_primary_term": 1, 
	    "found": true, 
	    "_source": {
	        "name": "哈登", 
	        "team_name": "⽕火箭", 
	        "position": "得分后卫", 
	        "play_year": "10", 
	        "jerse_no": "13"
	    }
	}    
  • 查看多个文档
请求:
    POST localhost:9200/_mget
响应:
    ??
  • 修改文档
1.根据提供的⽂文档⽚片段更更新数据
请求:
    POST localhost:9200/nba/_update/1

2.向_source字段,增加一个字段
请求:
    POST localhost:9200/nba/_update/1
    
    {
        "script": "ctx._source.age = 18"
    }

3.从_source字段,删除一个字段
请求:
    POST localhost:9200/nba/_update/1
    {
        "script": "ctx._source.remove(\"age\")"
    }

4.根据参数值,更新指定文档的字段
请求:
    POST localhost:9200/nba/_update/1
	{
	    "script": {
	        "source": "ctx._source.age += params.age", 
	        "params": {
	            "age": 4
	        }
	    }
	}
 说明:upsert 当指定的⽂文档不不存在时,upsert参数包含的内容将会被插⼊入到索引中,作为⼀一个 新⽂文档;如果指定的⽂文档存在,ElasticSearch引擎将会执⾏行行指定的更更新逻辑。
  • 删除文档
请求:
    DELETE localhost:9200/nba/_doc/1

2.搜索的简单使用

  • term(词条)查询和full text(全文)查询
    • 词条查询:词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时,才匹配搜索
    • 全文查询:ElasticSearch引擎会优先分析查询字符串,将其拆分成多个分词,只要已分析的字段中包含词条的任意一个,或者包含全部,就匹配查询条件,返回该文档;如果不包含任意一个分词,表示没有任何文档匹配查询条件
  • 单条term查询
请求:
    POST localhost:9200/nba/_search

	{
	    "query": {
	        "term": {
	            "jerse_no": "23"
	        }
	    }
	}    
  • 多条term查询
请求:
    POST localhost:9200/nba/_search

	{
	    "query": {
	        "terms": {
	            "jerse_no": [
	                "23", 
	                "13"
	            ]
	        }
	    }
	} 
响应:
    {
        "took": 21, 
        "timed_out": false, 
        "_shards": {
            "total": 1, 
            "successful": 1, 
            "skipped": 0, 
            "failed": 0
        }, 
        "hits": {
            "total": {
                "value": 2, 
                "relation": "eq"
            }, 
            "max_score": 1, 
            "hits": [
                {
                    "_index": "nba", 
                    "_type": "_doc", 
                    "_id": "1", 
                    "_score": 1, 
                    "_source": {
                        "name": "哈登", 
                        "team_name": "⽕火箭", 
                        "position": "得分后卫", 
                        "play_year": 10, 
                        "jerse_no": "13"
                    }
                }, 
                {
                    "_index": "nba", 
                    "_type": "_doc", 
                    "_id": "3", 
                    "_score": 1, 
                    "_source": {
                        "name": "詹姆斯", 
                        "team_name": "湖⼈人", 
                        "position": "⼩小前锋", 
                        "play_year": 15, 
                        "jerse_no": "23"
                    }
                }
            ]
        }
    }   
  • match_all
请求:
    POST localhost:9200/nba/_search
    
    {
        "query": {
            "match_all": {}
        },
        "from": 0,
        "size": 10 
    }
响应:
    {
        "took": 9, 
        "timed_out": false, 
        "_shards": {
            "total": 1, 
            "successful": 1, 
            "skipped": 0, 
            "failed": 0
        }, 
        "hits": {
            "total": {
                "value": 3, 
                "relation": "eq"
            }, 
            "max_score": 1, 
            "hits": [
                {
                    "_index": "nba", 
                    "_type": "_doc", 
                    "_id": "1", 
                    "_score": 1, 
                    "_source": {
                        "name": "哈登", 
                        "team_name": "⽕火箭", 
                        "position": "得分后卫", 
                        "play_year": 10, 
                        "jerse_no": "13"
                    }
                }, 
                {
                    "_index": "nba", 
                    "_type": "_doc", 
                    "_id": "2", 
                    "_score": 1, 
                    "_source": {
                        "name": "库⾥里里", 
                        "team_name": "勇⼠士", 
                        "position": "控球后卫", 
                        "play_year": 10, 
                        "jerse_no": "30"
                    }
                }, 
                {
                    "_index": "nba", 
                    "_type": "_doc", 
                    "_id": "3", 
                    "_score": 1, 
                    "_source": {
                        "name": "詹姆斯", 
                        "team_name": "湖⼈人", 
                        "position": "⼩小前锋", 
                        "play_year": 15, 
                        "jerse_no": "23"
                    }
                }
            ]
        }
    }
  • match
请求:
    POST localhost:9200/nba/_search

    {
        "query": {
            "match": {
                "position": "后卫"
            }
        }
    }
响应:
    {
        "took": 89, 
        "timed_out": false, 
        "_shards": {
            "total": 1, 
            "successful": 1, 
            "skipped": 0, 
            "failed": 0
        }, 
        "hits": {
            "total": {
                "value": 2, 
                "relation": "eq"
            }, 
            "max_score": 0.90630186, 
            "hits": [
                {
                    "_index": "nba", 
                    "_type": "_doc", 
                    "_id": "1", 
                    "_score": 0.90630186, 
                    "_source": {
                        "name": "哈登", 
                        "team_name": "⽕火箭", 
                        "position": "得分后卫", 
                        "play_year": 10, 
                        "jerse_no": "13"
                    }
                }, 
                {
                    "_index": "nba", 
                    "_type": "_doc", 
                    "_id": "2", 
                    "_score": 0.90630186, 
                    "_source": {
                        "name": "库⾥里里", 
                        "team_name": "勇⼠士", 
                        "position": "控球后卫", 
                        "play_year": 10, 
                        "jerse_no": "30"
                    }
                }
            ]
        }
    }    
  • multi_match
请求:
    POST localhost:9200/nba/_update/2
    
    {
        "query": {
            "multi_match": {
                "query": "shooter", 
                "fields": [
                    "title", 
                    "name"
                ]
            }
        }
    }
  • match_phrase
请求:
    post localhost:9200/nba/_search

    {
        "query": {
            "match_phrase": {
                "position": "得分后卫"
            }
        }
    }    
  • match_phrase_prefix
请求:
    POST localhost:9200/nba/_search

    {
        "query": {
            "match_phrase_prefix": {
                "title": "the best s"
            }
        }
    }    

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值