elasticsearch

mapping的创建只能是在数据插入之前创建的,
这种创建方式可以是es自动创建的也可以是手动创建的
但是mapping一旦创建就不可以修改(如果错误执行这个操作就会报异常:”reason”: “Can’t process field [new_field], Analysis requests are only supported on tokenized fields”)
如果要添加新的field,那么可以为已有的mapping添加新的fiedl解析机制
————————————————
如果插入的是object(对象)类型的数据
那么底层的数据结构是这样的
普通类型是用“.”(一点)串起来的
如果是数组类型是把横向的数据类型改为纵向的数据类型
————————————————
elasticsearch中的查询语句:
默认是采用get http的方式进行查询的,比如: GET /test_index/test_type/_search
但是官方认为http请求体更符合查询的格式 ,但是带请求体就要使用POST的方式
但是官方又认为,get关键字才符合查询的习惯,post是用于做修改的,于是就把带请求体的方式用get的方式,但是有部分浏览器不支持get 带请求体,那么这时候可以用post的方式
————————————————
es中的查询语句有两种
query string,这种就是前面说的参数直接放置在url最后面
query dsl, 参数放置在请求体中
重点说明query dsl的基本语法:

{
    query
    sort
    from
    size
}
"must":{}
"must_not":{}
"should":{}
"filter":{}

query的几种类型
1,bool (组合条件查询,上面已经写了,bool下面可以放置的类型(must, must_not, should, filter))
2,match_all(匹配所有字段)
3,match(匹配一条字段)
4,multi_match(匹配多个字段)
5,range(使用范围匹配)
6,term(一个关键字)
7,terms(多个关键字)
8,exists( )

GET /website/article/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "elasticsearch"
          }
        }
      ],
      "should": [
        {
          "match": {
            "content": "elasticsearch"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "author_id": 111
          }
        }
      ]
    }
  }
}

GET /test_index/test_type/_search
{
  "query": {
    "match_all": {}
  }
}

GET /test_index/test_type/_search
{
  "query": {
    "match": {
      "name": "lisi"
    }
  }
}

GET /test_index/test_type/_search
{
  "query": {
    "multi_match": {
      "query": "test test2",
      "fields": ["test_context", "test_field"]
    }
  }
}

GET /test_index/test_type/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 20,
        "lte": 50
      }
    }
  }
}

GET /test_index/test_type/_search
{
  "query": {
    "term": {
      "test_field": {
        "value": "replaced"
      }
    }
  }
}

GET /test_index/test_type/_search
{
  "query": {
    "terms": {
      "test_context": [
        "test",
        "test2"
      ]
    }
  }
}

验证查询格式的合法性
GET /index/type/_validate/query?explain

GET /test_index/test_type/_validate/query?explain
{
  "query": {
    "match": {
      "age": 10
    }
  }
}

定制排序规则

GET /test_index/test_type/_validate/query?explain
GET /company/emplayee/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "age": {
            "gte": 30
          }
        }
      }
    } 
  },
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}
-----------------------
GET /company/emplayee/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 30
      }
    }
  },
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

对符串field进行排序

//创建索引
PUT /website
{
  "mappings": {
    "article": {
      "properties": {
        "title" : {
          "type": "text",
          "fields": {          //将field索引两次来进行字符串排序
            "raw" : {
              "type" : "string",
              "index" : "not_analyzed"
            }
          },
          "fielddata" : true     //创建正序排序
        },
        "context" :{
          "type": "text"
        },
        "post_data" : {
          "type": "date"
        },
        "author_id" : {
          "type": "long"
        }
      }
    }
  }
}

//插入数据,注意插入的数据类型一定要对应
PUT /website/article/3
{
  "title" : "third artitle",
  "author_id" : 3,
  "context" : "this is the third artitle",
  "post_data" : "2017-03-01"
}

//按照artitle.raw进行排序
GET /website/article/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "title.raw": {
        "order": "desc"
      }
    }
  ]
}

scoll 批量滚动搜索:目的是用来提供给后台处理的

GET /test_index/test_type/_search?scroll=1ms
{
  "query": {
    "match_all": {}
  },
  "sort": ["_doc"],     //这里要注意,给定的是["_doc"]参数
  "size": 2
}

//注意第二次以及后面的搜索,直接是:/_search/scoll   这里是不用带/index/type的
GET  /_search/scroll
{
  "scroll" : "1ms",
  "scroll_id" :     "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAACiMFjMzRFl5WTliUkMyR082ek55LVZFNWcAAAAAAAAojhYzM0RZeVk5YlJDMkdPNnpOeS1WRTVnAAAAAAAAKI0WMzNEWXlZOWJSQzJHTzZ6TnktVkU1ZwAAAAAAACiPFjMzRFl5WTliUkMyR082ek55LVZFNWcAAAAAAAAokBYzM0RZeVk5YlJDMkdPNnpOeS1WRTVn"      //这里要制定上次id的值
}

GET  /_search/scroll
{
  "scroll" : "1ms",
  "scroll_id" : "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAACiMFjMzRFl5WTliUkMyR082ek55LVZFNWcAAAAAAAAojhYzM0RZeVk5YlJDMkdPNnpOeS1WRTVnAAAAAAAAKI0WMzNEWXlZOWJSQzJHTzZ6TnktVkU1ZwAAAAAAACiPFjMzRFl5WTliUkMyR082ek55LVZFNWcAAAAAAAAokBYzM0RZeVk5YlJDMkdPNnpOeS1WRTVn"
}

索引的增,删,改

//创建一个索引
PUT /my_index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "my_type":{           //索引下面的type
      "properties": {     //该类索引的(properties)(也就是说对应的field)
        "name" : {
          "type" : "text"
        }
      }
    }
  }
}

//添加数据
PUT /my_index/my_type/2
{
  "name" : "world"
}

//查询数据
GET /my_index/my_type/_search

//修改索引(这里要注意语法:PUT /index/_settings)这个_settings是必须要有的
PUT /my_index/_settings
{
  "settings": {    //还有settings 中number_of_shards是不可以修改的,因为路由算法的原因
    "number_of_replicas": 1
  }
}

//删除索引,可以是单个,多个,正则匹配,以及全部
DELETE /my_index
DELETE /my_index,my_index_two
DELETE /my_index_*
DELETE /_all

//由于 DELETE /all操作的危险性,可以在es的配置文件中限制该操作
elasticsearch.yml
action.destructive_requires_name: true
//
DELETE /my_index

//
PUT /my_index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0,
    "analysis": {
      "analyzer": {
        "es_std" : {
          "type" : "standard",
          "stopwords" : "_english_"
        }
      }
    }
  }
}

//
GET /my_index/_analyze
{
  "text": "a dog is in the home",
  "analyzer": "standard"
}

GET /my_index/_analyze
{
  "text": "a dog is in the home",
  "analyzer": "es_std"
}

//
PUT /my_index
{
  "settings": {
    "analysis": {
      "char_filter": {
        "&_to_and": {
          "type": "mapping",
          "mappings": ["&=> and"]
        }
      },
      "filter": {
        "my_stopwords": {
          "type": "stop",
          "stopwords": ["the", "a", "dog"]
        }
      }, 
      "analyzer": {
        "my_analyzer" : {
          "type" : "custom",
          "char_filter" : ["html_strip", "&_to_and"],
          "tokenizer" : "standard",
          "filter" : ["lowercase", "my_stopwords"]
        }
      }
    }
  }
}

//
GET /my_index/_analyze
{
  "text" : "you & me are friend,and dog",
  "analyzer": "my_analyzer"
}

//该index下的某个type用上了我们自己定制化的mapping该如何做:答案如下
PUT /my_index/_mapping/my_type
{
  "properties": {
    "content" : {
      "type" : "text",
      "analyzer": "my_analyzer"
    }
  }
}
_mapping
_search
_analyze
_complain

既然,type是作为document的field存在的,这个field是_type
那么,就可以单独为某个type的某个field制定分词器
如下:

//该index下的某个type用上了我们自己定制化的mapping该如何做:答案如下
PUT /my_index/_mapping/my_type
{
  "properties": {
    "content" : {
      "type" : "text",
      "analyzer": "my_analyzer"
    }
  }
}

mapping root object 解析

"properties":{
    "the_field":{
        "type":"text",
        "index":"analyzed",
        "analyzer":"standard",
        "include_in_all":"false"
    }   
}
"_source":{
    "enabled":"false"
}
"_all":{
    "enabled":"false"
}
PUT /my_index
{
  "mappings": {
    "my_type":{
      "properties": {
        "content":{
          "type": "text",
          "index": true,
          "analyzer": "standard",
          "include_in_all": false
        }
      }
    }
  }
}

PUT /my_index/_mapping/my_type
{
  "properties": {
    "name" : {
      "type": "text",
      "index": false
    }
  }
}

PUT /my_index/_mapping/my_type
{
  "_source": {
    "enabled": true
  }
}

PUT /my_index/_mapping/my_type
{
  "_all": {
    "enabled": true
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值