ElasticSearch语法练习

ElasticSearch语法练习,第1张

ElasticSearch语法练习
GET _search
{
  "query": {
    "match_all": {}
  }
}
POST _analyze
{
  "analyzer":"ik_max_word",
  "text":"歼10系列战斗机"
}
POST _analyze
{
  "analyzer":"pinyin",
  "text":"歼10系列战斗机"
}
PUT /myinde
{
  "settings": {
    "number_of_shards": 1
    , "number_of_replicas": 0
  }
}
HEAD /myinde
DELETe /myinde

PUT /myinde/_mapping
{
  "properties":{
    "name":{
      //字段设置
      "type":"text",
      //是否索引
      "index":true,
      //展示结果是否包含该内容,es中无效
      "store":true,
      //必须对应text
      "analyzer":"ik_max_word"
    },
    "age": {
      "type":"integer",
      "index":true,
      "store":true
    },
    "address":{
      "type":"text",
      "index":true,
      "store":true,
      "analyzer":"ik_max_word"
    },
    "birthday":{
      "type":"date",
      "index":true,
      "format":"yyyy-MM-dd HH:mm:ss"
    }
  }
}

#插入doc
POST /myinde/_doc
{
  "name":"性格温顺的赵六",
  "age":21,
  "address":"湖南省湘西洲凤凰县",
  "birthday":"1997-09-08 18:00:00"
}
POST /myinde/_doc
{
  "name":"脾气暴躁的王五",
  "age":19,
  "address":"山东省青岛市螺丝县",
  "birthday":"1999-09-08 18:00:00"
}

PUT /myinde/_doc/_bulk
  {"index":{"_id":1}}
  {"name":"暴躁且温顺的六耳","age":39,"address":"广州省深圳市宝安区","birthday":"1989-09-08 18:00:00"}
  {"index":{"_id":2}}
  {"name":"爱吃螺丝粉的王二麻子","age":9,"address":"北京市朝阳区中心街道","birthday":"2009-09-08 18:00:00"}

#查询文档
GET /myinde/_search

GET /myinde/_doc/1

#批量查询
GET /_mget
{
    "docs": [
        {
            "_index": "索引库名称1",
            "_type": "映射类型1",
            "_id":"查询文档id1"
        },
        {
       		"_index": "索引库名称2",
       		"_type": "映射类型2",
       		"_id":"查询文档id2"
    	}
    ]
}

#term查询
GET /myinde/_doc/_search
{
  "query":{
    "term":{
      "address":{
        "value":"青岛"
      }
    }
  }
}

#bool查询
GET /myinde/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "脾气"
          }
        }
      ],
      //可以包含也可以不包含的查询
      "should": [
        {"match": {
          "address": "北京"
        }},{
          "range": {
            "age": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ],
      //一定不能包含的查询
      "must_not": [
        {
          "range": {
            "age": {
              "gte": 0,
              "lte": 10
            }
          }
        }
      ],
      //"should": 必须匹配的最小值
      "minimum_should_match": 1
    }
  }
}

#boosting查询
GET /myinde/_search
{
  "query": {
    "boosting": {
      "positive": {
        "match": {
          "name": "的"
        }
      }, 
      //从positive的结果集再进行 *** 作
      //进行评分修改,结果为原评分*"negative_boost" 
      "negative": {
        "range": {
          "FIELD": {
            "gte": 10,
            "lte": 20
          }
        }
      },
      "negative_boost": 0.2
    }
  }
}

#分页
GET /myinde/_search
{
  
}

#聚合
GET /myinde/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0, 
  "aggs": {
    "NAME": {
      "avg": {
        "field": "age"
      }
    },
    "count":{
      "cardinality": {
        "field": "age"
      }
    },
    "satus":{
      "stats": {
        "field": "age"
      }
    }
  }
}

#范围分桶
GET /myinde/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    //桶
    "age_range": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 0,
            "to": 10
          },
          {
            "from": 10,
            "to": 50
          }
        ]
      },
      //指标
      "aggs": {
        "age_sum": {
          "sum": {
            "field": "age"
          }
        }
      }
    }
  }
}

#自定义文档相关性
GET /myinde/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "name": "暴躁且温顺"
        }
      },
      "functions": [
        {
          "filter": {
            "range": {
              "age": {
                "gte": 10,
                "lte": 20
              }
            }
          },
          "weight": 2
        }
      ]
    }
  }
}

#衰减函数
GET /myinde/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "name": "暴躁且温顺"
        }
      },
      "functions": [
        {
          "gauss": {
            "age": {
              "origin": "19",
              "scale": "10"
            }
          }
        }
      ],
      "boost_mode": "replace"
    }
  }
}

POST _analyze
{
  "analyzer":"ik_max_word",
  "text":"暴躁且温顺的六耳"
}

#嵌套类型
PUT /test_index

PUT /test_index/_mapping
{
  "properties": {
    "name":{
      "type":"keyword"
    },
    "age":{
      "type":"integer"
    },
    "classes":{
      //嵌套类型
      "type":"nested",
      "properties":{
        "className":{
          "type":"keyword"
        },
        "sum":{
          "type":"integer"
        }
      }
    }
  }
}

#插入方式
PUT

#查询方式
GET







欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5608162.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存