elasticsearch——增删改查

-- 创建索引
PUT /icon_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "text_anlyzer": { 
          "tokenizer": "ik_max_word",
          "filter": "py"
        },
        "completion_analyzer": {
          "tokenizer": "keyword", 
          "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,
          "none_chinese_pinyin_tokenize": false
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "id":{
        "type": "keyword"
      },
      "username":{
        "type": "text",
        "analyzer": "text_anlyzer",
        "search_analyzer": "ik_smart"
      },
      "category":{
        "type": "keyword"
      },
      "icon_score":{
        "type": "integer"
      },
      "create_time": { 
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      }
    }
  }
}


--查询索引
GET /_cat/indices?v
get /icon_index/_search
get /icon_index/_mapping



--初始化数据
PUT /icon_index/_doc/1
{ 
    "username":"我是中国人,来自福建",
    "icon_score":9,
    "category":"仙侠",
    "create_time":"2019-04-26 01:59:54"
}

PUT /icon_index/_doc/2
{ 
    "username":"厦门水电费",
    "icon_score":7,
    "category":"回合",
    "create_time":"2019-06-10 01:59:54"
}

PUT /icon_index/_doc/3
{ 
    "username":"奥林匹克在中国举行",
    "icon_score":5,
    "category":"模拟",
    "create_time":"2019-05-18 01:59:54"
}

PUT /icon_index/_doc/4
{ 
    "username":"Java mysql 日语 应用",
    "icon_score":3,
    "category":"卡牌",
    "create_time":"2019-06-16 01:59:54"
}

PUT /icon_index/_doc/5
{ 
    "username":"币安欧意gate",
    "icon_score":1,
    "category":"仙侠",
    "create_time":"2019-07-15 01:59:54"
}

--组合查询
# username like '%名称%' and username like '%名称1%' 
# and score >= 10 and score <= 8 and category = '分类精确查询' 

# 匹配username包含中国或mysql,icon_score范围在6~10,category为仙侠
GET /icon_index/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "username": "中国 mysql"}},
        {
          "range": {
            "icon_score": { "gte": 6,"lte": 10 }
          }
        },
        { "term": { "category": "仙侠"}} 
      ]
    }
  }
}

 

posted on 2023-04-23 16:39  Ruthless  阅读(85)  评论(0编辑  收藏  举报