elasticsearch 第二章 elasticsearch的详细用法及参数

es的详细用法:

#查看s18下所有的文件
GET s18/doc/_search  

如果PUT s18/doc/1 添加内容后,再次PUT s18/doc/1,会将之前的所有东西覆盖掉.

查询文件中指定的字符串

方法1:
  #
查询年龄为19岁的数据   GET s18/doc/_search?q=age:19
方法2:
  #DSL
  GET s18/doc/_search
  {
    "query":{
      "match":{
        "age":19
        }
     }
  }

修改指定文件的指定字段

#将第一条数据的age变为100
POST s18/doc/1/_update { "doc":{ "age":100 } }

删除指定字段

POST s18/doc/_delete_by_query?q=age:30

排序(不是所有的字段都能排序)

GET s18/doc/_search
{
  "query":{
        "match_all":{}
    },
    "sort":[
        {
      "age":{
      #升序
      "order": "asc"
      #降序
      "order":"desc"
          }
       }
    ]  
}

分页

#从第一页开始,每页显示2条数据
GET s18/doc/_search
{
    "query":{
    "match_all":{}
  },
   "from":0,
   "size":2 
}

布尔查询(should(or:两者存在一个即可),must(and:两者必须都存在),must_not(not:不是这个的))

should

#查询出name为嘻哈天王age为19的数据
GET s18/doc/_search { "query":{ "bool":{ "should":[ { "match":{ "name":"嘻哈天王" } }, "match":{ "age":19 } ] } } }

 must

#查询出age为30并且name为guaishushu的数据
GET s18/doc/_search { "query": { "bool": { "must": [ { "match": { "age": "30" } }, { "match": { "name": "guaishushu" } } ] } } }

must_not

#查询出name不为tiantian的和age不为30的数据
GET s18/doc/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "age": "30"
          }
        },
        {
          "match": {
            "name": "tiantian"
          }
        }
      ]
    }
  }
}

filter查询(gte,lte,可以结合bool查询)

#查询年龄大于20岁的男的数据:
GET s18/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "sex": ""
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gt": 20
          }
        }
      }
    }
  }
}

# 查询年龄小于等于23的非男性

GET s18/doc/_search
{
"query":{
    "bool":{
      must_not:[
         {
          "match":{
            "sex": ""
          }
        }
      ],
     {
     match:{
       filter:{
        "range":{
           "age":{
              "lte":23
              }
            }
          }
        }
      }
    }
  }
}

高亮查询

#查询出name为tiantian的数据,给查出来的东西显示为下面的东西

GET s18/doc/_search { "query": { "match": { "name": "tiantian" } }, "highlight": { "pre_tags": "<b style='color:red;font-size:20px;' class='tian'>", "post_tags": "</b>", "fields": { "name":{} } } }

结果过滤

#查询出name为xiaokeai的数据,并过滤出name和age

GET s18/doc/_search
{
  "query": {
    "match": {
      "name": "xiaokeai"
    }
  },
  #如果只想显示出一个东西,那么只需要写一个字符串就行,列表只是在多个数据的时候才用
  "_source": ["name","age"]
}

聚合查询

# sum,查询所有男生的年龄总和
GET s18/doc/_search
{
  "query": {
    "match": {
      "sex": ""
    }
  },
  "aggs": {
    "age_sum": {
      "sum": {
        "field": "age"
      }
    }
  }
}

查询出男生的最大年龄

GET s18/doc/_search
{
  "query": {
    "match": {
      "sex": ""
    }
  },
  "aggs": {
    "max_age": {
      "max": {
        "field": "age"
      }
    }
  }
}

求所有人的平均值

GET s18/doc/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "my_avg": {
      "avg": {
        "field": "age"
      }
    }
  }
}

分组查询

# 分组,根据年龄,10-20,,20-30, 30-100
GET s18/doc/_search
{
  "aggs": {
    "my_age": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 10,
            "to": 20
          },
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 100
          }
        ]
      }
    }
  }
}

根据分组查每一组的详细信息

# 分组,根据年龄,10-20,,20-30, 30-100,并查出每一组的平均值
GET s18/doc/_search
{
  "aggs": {
    "my_agg": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 10,
            "to": 20
          },
          {
            "from": 20,
            "to":30
          },
          {
            "from": 30,
            "to": 100
          }
        ]
      },
      "aggs": {
        "my_avg": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}

映射(简而言之,映射就是之前都是es帮我自动创建表结构,映射就是我们自己创建表结构)

查看映射的数据结构

GET s6/_mapping
PUT s5
{
  "mappings": {
    "doc":{
      "properties":{
        "name":{
          "type":"text"
        },
        "age":{
          "type": "long"
        },
        "desc":{
          "type":"text"
        }
      }
    }
  }
}

这里我们要注意,如果我们给定的类型是文本,但是我们插入的时候是long类型,es会帮我们将long类型转化为文本类型

 稍微复杂点的映射

PUT s6
{
  "mappings": {
    "doc":{
      "properties":{
        "age":{
          "type":"long"
        },
        "b":{
          "type":"text",
          "fields":{
            "keyword":{
              "type":"keyword",
              "ignore_above":256
            }
          }
        },
        "desc":{
          "type":"text",
          "fields":{
            "keyword":{
              "type":"keyword",
              "ignore_above":256
            }
          }
        },
        "name":{
          "type":"text",
          "fields":{
            "keyword":{
              "type":"keyword",
              "ignore_above":256
            }
          }
        },
        "sex":{
          "type":"text",
          "fields":{
          "keyword":{
            "type":"keyword",
            "ignore_above":256
            }
          }
        }
      }
    }
  }
}

dynamic的三种状态

ture
false
strict

ture

PUT s7
{
  "mappings": {
    "doc":{
      "dynamic":true,
      "properies":{
        "name":{"type":"text"}
      }
    }
  }
}

false

PUT s7
{
  "mappings": {
    "doc":{
      "dynamic":false,
      "properies":{
        "name":{"type":"text"}
      }
    }
  }
}

 strict

PUT s8
{
  "mappings": {
    "doc":{
      "dynamic":"strict",
      "properies":{
        "name":{
          "type":"text"
        }
      }
    }
  }
}

ignore_above

PUT s9
{
  "mappings": {
    "doc":{
      "properies":{
        "title":{
          "type":"text",
          "ignore_above":256
        }
      }
    }
  }
}

mappings中的index参数

PUT s10
{
  "mappings": {
    "doc":{
      "properies":{
        "t1":{
        "name":"text",
        "index":true
      },
      "t2":{
        "name":"text",
        "index":false
        }
      }
    }
  }
}

copy_to

PUT s11
{
  "mappings": {
    "doc":{
      "properies":{
        "t1":{
        "name":"text",
        "copy_to":"full_name"
      },
      "t2":{
        "name":"text",
        "copy_to":"full_name"
        },
        "full_name":{
          "type":"text"
        }
      }
    }
  }
}
PUT s10
{
  "mappings": {
    "doc":{
      "properties":{
        "t1":{
          "type":"text",
          "copy_to":["f1", "f2"]
        },
        "t2":{
          "type":"text",
          "copy_to":["f1", "f2"]
        },
        "f1":{
          "type":"text"
        },
        "f2":{
          "type":"keyword"
        }
      }
    }
  }
}

嵌套类型

PUT q1
{
  "mappings": {
    "doc":{
      "properties":{
        "name":{
          "type":"text"
        },
        "age":{
          "type":"long"
        },
        "info":{
          "properties":{
            "addr":{
              "type":"text"
            },
            "tel":{
              "type":"long"
            }
          }
        }
      }
    }
  }
}

嵌套类型取值的时候

GET w1/doc/_search
{
  "query": {
    "match": {
      "info.tel": "10010"
    }
  }
}
原文地址:https://www.cnblogs.com/zty1304368100/p/10902448.html