nowwowww

导航

 
PUT my_index/_doc/1
{
  "group":"fans",
  "user":[
    {
      "first":"张",
      "last":"学友"
    },
    {
      "first":"刘",
      "last":"德华"
    }]
}

GET my_index/_search

GET my_index/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "user.first": "张"
        }},
        {
          "match": {
            "user.last": "德华"
          }
        }
      ]
    }
  }
}

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":"张",
      "last":"学友"
    },
    {
      "first":"刘",
      "last":"德华"
    }]
}

GET my_index/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            {"match": {
              "user.first": "张"
            }},
            {
              "match": {
                "user.last": "学友"
              }}
          ]
        }
      }
    }
  }
}


PUT articles
{
  "mappings": {
    "properties": {
      "jj":{
        "type": "completion"
      }
    }
  }
}

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

GET articles/_search
{
  "suggest": {
    "YOUR_SUGGESTION": {
      "prefix": "el",
      "completion": {
        "field": "jj",
        "size":10
      }
    }
  }
}

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": {
    "properties": {
      "id":{
        "type": "keyword"
      },
      "name":{
         "type": "completion",
        "analyzer": "my_pinyin",
        "search_analyzer": "ik_smart"
      },
      "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":"松下电视"}

GET /goods/_search
{
  "suggest": {
    "YOUR_SUGGESTION": {
      "prefix": "sx",
      "completion": {
        "field": "name"
      }
    }
  }
}


DELETE /goods

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


#测试默认分词器
POST /_analyze
{
  "text": "黑马程序员java学科",
  "analyzer": "standard"
}


#测试ik分词器
POST /_analyze
{
  "text": "黑马程序员java学科",
  "analyzer": "ik_smart"
}
POST /_analyze
{
  "text": "黑马程序员java学科",
  "analyzer": "ik_max_word"
}
POST /_analyze
{
  "text": "黑马程序员java学科,奥利给,酷毙了",
  "analyzer": "ik_max_word"
}


#创建索引库
PUT /jiang
#查询索引库
GET jiang
#删除索引库
DELETE jiang

#创建索引库映射,存在索引库
PUT /jiang/_mapping
{
  "properties":{
    "title":{
      "type":"text",
      "analyzer":"ik_smart"
    },
    "image":{
      "type":"keyword",
      "index":"false"
    },
    "price":{
      "type":"float"
    }
  }
}
#查看映射关系
GET /jiang/_mapping


#创建索引库同时指定映射
PUT /jiang
{
  "mappings": {
    "properties": {
      "title":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "image":{
        "type": "keyword",
        "index": false
      },
      "price":{
        "type": "float"
      }
    }
  }
}


#新增文档,生成随机id
POST /jiang/_doc
{
  "title":"小米手机",
  "image":"https://pics6.baidu.com",
  "price":"65535"
}

#新增文档并指定id
POST /jiang/_doc/1
{
  "title":"huawei",
  "image":"123456789",
  "price":2499
}

#查看文档
GET /jiang/_doc/7evwvHUBk4pO-a2Q8Gjs
GET /jiang/_doc/3

#删除文档
DELETE /jiang/_doc/7evwvHUBk4pO-a2Q8Gjs

#修改文档
PUT /jiang/_doc/1
{
  "title":"华为",
  "image":"123456789",
  "price":2499
}

#id不存在就会创建文档
PUT /jiang/_doc/2
{
  "title":"小米",
  "image":"1233211234567",
  "price":2999
}

# 新增未映射字段(默认映射)
PUT /jiang/_doc/3
{
  "title":"华米",
  "image":"1233211234567",
  "price":1499,
  "description":"手表"
}

#定义动态模板
PUT /jiangheng
{
  "mappings": {
    "properties": {},
    "dynamic_templates":[
      {"aa":{
        "match_mapping_type":"long",
        "mapping":{
          "type":"integer"
        }
      }},
      {"bb":{
        "match_mapping_type":"string",
        "match":"t_*",
        "mapping":{
          "type":"text",
          "analyzer":"ik_smart"
        }
      }},
      {"cc":{
        "match_mapping_type":"string",
        "match":"k_*",
        "mapping":{
          "type":"keyword"
        }
      }}
      ]
  }
}
#测试/jiangheng
GET /jiangheng/_mapping
PUT /jiangheng/_doc/1
{
  "t_title":"超大米手机",
    "k_images":"http://image.leyou.com/12479122.jpg",
    "price":3299.00,
    "stock": 200,
    "saleable":true,
    "k_subTitle":"超级双摄,亿级像素"
}


#准备测试数据
PUT /jiang/_doc/4
{
    "title":"小米电视4A",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":3899.00
}
PUT /jiang/_doc/5
{
    "title":"乐视电视4X",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":2599.00,
    "brand":"乐视"
}
PUT /jiang/_doc/6
{
    "title":"乐视电视5X",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":2499.00,
    "brand":"乐视"
}
PUT /jiang/_doc/7
{
    "title":"华为手机",
    "images":"http://image.huawei.com/134569122.jpg",
    "price":3499.00,
    "brand":"huawei"
}


#查询所有
GET /jiang/_search
GET /jiang/_search
{
  "query": {
    "match_all": {}
  }
}

#分词查询
GET /jiang/_search
{
  "query": {
    "match": {
      "title": "小米电视"
    }
  }
}

#默认operator是'or'
GET /jiang/_search
{
  "query": {
    "match": {
      "title": {
        "query": "小米电视","operator": "and"
      }
    }
  }
}

#词条匹配term不会对用户输入内容分词,做为整体
# 如果查询的目标字段是keyword类型,就应该使用term查询
GET /jiang/_search
{
  "query": {
    "term": {
      "title": {
        "value": "小米"
      }
    }
  }
}


#模糊查询,会限制编辑距离
GET /jiang/_search
{
  "query": {
    "fuzzy": {
      "title": {
        "value": "华米","fuzziness": 1
      }
    }
  }
}

#范围查询
GET /jiang/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 2499,
        "lt": 3500
      }
    }
  }
}


#布尔查询,没有查询条件
#用与或非来连接其他条件
#-与(must)
#-或(should)
#-非(must_not)
GET /jiang/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "title": "小米"
        }},
        {"match": {
          "title": "电视"
        }}
      ]
    }
  }
}

GET /jiang/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "title": "小米"
        }}
      ],
      "must_not": [
        {"match": {
          "title": "电视"
        }}
      ]
    }
  }
}

#排序
GET /jiang/_search
{
  "query": {
    "match": {
      "title": "小米"
    }
  },
  "sort": [
    {
      "price": {
        "order": "asc"
      }
    }
  ]
}


#高亮查询
GET /jiang/_search
{
  "query": {
    "match": {
      "title": "小米"
    }
  },
  "highlight": {
    "pre_tags": "<em>",
    "post_tags": "</em>",
    "fields": {
      "title": {}
    }
  }
}


#分页
GET /jiang/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 6
}


#Filter过滤
GET /jiang/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "title": "小米"
        }}
      ],
      "filter": {
        "range": {
          "price": {
            "gte": 1599,
            "lte": 3699
          }
        }
      }
    }
  }
}


#source过滤
GET /jiang/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["title","price"]
}

GET /jiang/_search
{
  "query": {
    "match_all": {}
  },
  "_source": {
    "excludes": "brand"
  }
}

DELETE /goods

GET /goods/_mapping

GET /goods/_search
{
  "suggest": {
    "YOUR_SUGGESTION": {
      "prefix": "xm",
      "completion": {
        "field": "suggestion",
        "size":1
      }
    }
  }
}

#会有默认分页20
GET /goods/_search
{
  "query": {
    "match": {
      "title": "xm"
    }
  },"size": 49
}

POST /goods/_analyze
{
  "text": "小米全面屏电视E55S 55英寸 4K超高清HDR 蓝牙语音遥控 内置小爱 2GB+8GB A网络液晶平板电视",
  "analyzer": "ik_smart"
}

GET /goods/_search
{
  "query": {
    "match": {
      "title": {
        "query": "手机",
        "operator": "and"
      }
    }
  },
  "aggs":{
    "specsAgg":{
      "nested": {
        "path": "specs"
      },
      "aggs": {
        "nameAgg": {
          "terms": {
            "field": "specs.name",
            "size": 20
          },
          "aggs": {
            "valueAgg": {
              "terms": {
                "field": "specs.value",
                "size": 300
              }
            }
          }
        }
      }
    }
  },
  "size": 0
}

 

posted on 2020-11-17 15:09  nowwowww  阅读(48)  评论(0)    收藏  举报