elastic笔记

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

POST /_analyze
{
  "text": ["我是中国人"],
  "analyzer": "ik_smart"
}

PUT my_index/_doc/1

  "region": "US",
  "age": 30,
  "name": { 
     "first": "John",
     "last":  "Smith"
  }
}

GET  my_index/_doc/1

PUT my_index/_doc/1
{
  "group":"fans",
  "user":[
    {
      "group":"fans",
      "user":[
          {
            "first":"John",
            "last":"Whrite"
          },
          {
            "first":"Alice",
            "last":"Editch"
          }
          
        ]
    }]
}

GET /my_index/_doc/1

GET /my_index/_search


GET /my_index/_search

PUT my_index
{
    "mappings": {
        "properties": { 
            "region": {"type": "keyword"},
            "age":  {"type": "integer" },
            "name": { 
                "properties": {
                    "first": { "type": "text" },
                    "last":  { "type": "text" }
                }
            }
        }
    }
}

GET my_index


GET /my_index/_search
{
  "query": {
    "match": {
      "name.first": "John"
    }
  }
}


DELETE /my_index


#Object数组的问题

PUT my_index/_doc/1
{
  "group":"fans",
  "user":[
      {
        "first":"John",
        "last":"Smith"
      },
    
      {
        "first":"Alice",
        "last":"White"
      }
    ]
}

GET my_index/_search

GET my_index/_search
{
  "query": {
    "bool": {
      "must": [
        
        {"match":{"user.first":"Alice"}},
        {"match":{"user.last":"Smith"}}
      ]
    }
  }
}


GET my_index/_search
{
  "query": {
    "bool":{
      "must": [
        {"match":{"user.first":"John"}},
        {"match":{"user.last":"White"}}
      ]
    }
  }
}

DELETE /my_index

PUT my_index
{
  "mappings": {
    "properties": {
      "user":{
        "type":"nested",
        "properties": {
          "first":{"type":"keyword"},
          "last":{"type":"keyword"}
        }
      }
    }
  }
}


put my_index/_doc/1
{
  "group":"fans",
  "user":[
    {
      "first":"Jone",
      "last":"Smith"
    },
    {
      "first":"Alice",
      "last":"White"
    }
    ]
}

GET my_index/_search


#声明是nested搜索
#指定对象数组的名称,这里是user
GET my_index/_search
{
  "query":{
    "nested": {
      "path": "user",
      "query": {
        "bool":{
          "must": [
            {"match":{"user.first": "Jone"}},
            {"match":{"user.last": "Smith"}}
            
          ]
        }
      }
    }
  }
}

PUT my_index/_doc/2
{
  "group":"fans",
  "user":[
    {
      "first":"John",
      "last":"white"
    },
    {
      "first":"Alice",
      "last":"Edith"
    }
    ]
}


GET my_index/_search
{
  "size": 0, 
  "aggs": {
    "name_agg": {
      "nested": {
        "path": "user"
      },
      "aggs": {
        "last_name": {
          "terms": {
            "field": "user.last",
            "size": 10
          },
          "aggs": {
            "first": {
              "terms": {
                "field": "user.first",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}


DELETE articles
#创建一个索引库,设置自动补全功能 type:"completion"
PUT articles
{
  "mappings": {
    "properties": {
      "suggestion":{
        "type": "completion"
      }
    }
  }
}

POST articles/_bulk
{ "index" : { } }
{ "suggestion": ["lucene", "is", "very", "cool"]}
{ "index" : { } }
{ "suggestion": ["Elasticsearch", "builds", "on", "lucene"]}
{ "index" : { } }
{ "suggestion": ["Elasticsearch", "rocks"]}
{ "index" : { } }
{ "suggestion": ["elastic", "is", "the", "company", "behind", "ELK"]}
{ "index" : { } }
{ "suggestion": ["Elk", "stack", "rocks"]}

GET articles/_search


POST articles/_search
{
  "suggest":{
    "article-suggester":{
      "prefix":"el",
      "completion":{
        "field":"suggestion",
        "size":10
      }
    }
  }
}

#templateid:定义一个模板,并指定模板的唯一id
#source:source中是模板的查询语句 ,本实例中用一个简单的
#match进行查询
#param_name:查询语句中预留的参数名称

POST _scripts/<templateid>
{
  "script":{
    "lang":"mustache",
    "source": {
      "query":{
        "match":{
          "title":"{{<param_name}}"
        }
      }
    }
  }
}

GET _scripts/<templateid>

DELETE _scripts/<templateid>

#先定义一个模板
#articles_search_template
#查询是一个match查询,然后带高亮的效果
#参数有两个
#search_value 搜索的内容
#hightlight_fiiled高亮的字段的名称
POST _scripts/articles_search_template
{
  "script":{
    "lang":"mustache",
    "source":{
      "query":{
        "match":{
          "suggestion":"{{search_value}}"
        }
      },
      "hightlight":{
        "fields":{
          "{{hightlight_field}}":{}
        }
      }
    }
  }
}


#查询的语法
GET _scripts/articles_search_template

#使用模板的语法
#index_name:指定索引库的名称,然后搜索方式是template
#templateid:搜索时要使用模板的id
#查询参数
#param_name:查询语句中预留的参数名称
#seach_words:搜索的语句
GET /<index_name>/_search/template  
{
    "id": "<templateid>", 
    "params": {
        "<param_name>": "<search_words>" 
    }
}

GET articles/_search_template


GET articles/_search_template
{
    "id": "articles_search_template", 
    "params": {
        "search_value": "elasticsearch",
        "highlight_field": "suggestion"
    }
}


#拼音搜索
#安装拼音插件
POST _analyze
{
  "text": ["张学友","刘德华"],
  "analyzer": "pinyin"
}


PUT /goods
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_pinyin": {
          "tokenizer": "ik_smart",
          "filter": [
            "py"
          ]
        }
      },
      "filter": {
        "py": {
          "type": "pinyin",
          "keep_full_pinyin": false,
          "keep_joined_full_pinyin": true,
          "keep_original": true,
          "limit_first_letter_length": 16,
          "remove_duplicated_term": true
        }
      }
    }
  },
  "mappings": {
    "_source": {
      "includes":["id", "title", "image", "prices"]
    }, 
    "properties": {
      "id": {
        "type": "keyword"
      },
      "name": {
        "type": "completion",
        "analyzer": "my_pinyin"
      },
      "title":{
        "type": "text",
        "analyzer": "my_pinyin",
        "search_analyzer": "ik_smart"
      },
      "price":{
        "type": "long"
      }
    }
  }
}


POST /goods/_analyze
{
  "text":"你好,华为",
  "analyzer": "my_pinyin"
}


PUT /goods/_bulk
{ "index" : {"_id":1}}
{"id":1,"name":"手机","title":"小米手机"}
{ "index":{"_id":2}}
{"id":2,"name":"空调","title":"小米空调"}
{"index":{"_id":3}}
{"id":3,"name":"sony","title":"sony播放器"}
{"index":{"_id":4}}
{"id":4,"name":"松下","title":"松下电视"}

POST /goods/_search
{
  "suggest": {
    "name_suggest": {
      "prefix":"s",
      "completion":{
        "field":"name"
      }
    }
  }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值