ES关于文档的基本操作

关于文档的基本操作

基本操作

添加数据

PUT /quanzhan/user/1
{
  "name": "xzM",
  "age": 18,
  "desc": "西西",
  "tags": ["技术宅", "温暖"]
}

获取数据

GET quanzhan/user/1

更新数据

PUT /quanzhan/user/1
{
 "name": "xzM",
 "age": 18,
 "desc": "东东",
 "tags": ["技术宅", "温暖"]
}

Post _update 推荐使用这种

POST quanzhan/user/1/_update
{
  "doc": {
    "name": "全栈自学社区"
  }
}

复杂操作

精确(匹配)查询

GET quanzhan/user/_search?q=name:全栈自学社区

反馈

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 19,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 6.033172,  /** 未来如果存在多条查询出来的结果 **/
    "hits" : [
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "1",
        "_score" : 6.033172,
        "_source" : {
          "name" : "全栈自学社区",
          "age" : 18,
          "desc" : "西西",
          "tags" : [
            "技术宅",
            "温暖"
          ]
        }
      }
    ]
  }
}

复杂搜索操作 SELECT(排序, 分页, 高亮,模糊查询,精准查询)

GET quanzhan/user/_search
{
  /** 查询的参数体 **/
  "query": {
    "match": {
       "name": "全"
    }
  }
}

反馈

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.7389809,
    "hits" : [
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.7389809,
        "_source" : {
          "name" : "全栈自学社区",
          "age" : 18,
          "desc" : "西西",
          "tags" : [
            "技术宅",
            "温暖"
          ]
        }
      }
    ]
  }
}

多条数据添加测试

GET quanzhan/user/_search
{
  "query": {
    "match": {
       "name": "王"
    }
  }
}

反馈

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.7721133,
    "hits" : [
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "5",
        "_score" : 0.7721133,
        "_source" : {
          "name" : "王六",
          "age" : 18,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "6",
        "_score" : 0.7721133,
        "_source" : {
          "name" : "王五",
          "age" : 18,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      }
    ]
  }
}

hit: 索引和文档信息, 查询结果总数, 然后就是查询出来的具体文档

_score 分数 我们可以通过来判断谁更加符合结果

指定查询结果

GET quanzhan/user/_search
{
  "query": {
    "match": {
       "name": "王"
    }
  },
  "_source": ["name", "desc"] /** 结果过滤 **/
}

反馈结果

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.7721133,
    "hits" : [
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "5",
        "_score" : 0.7721133,
        "_source" : {
          "name" : "王六",
          "desc" : "靓仔"
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "6",
        "_score" : 0.7721133,
        "_source" : {
          "name" : "王五",
          "desc" : "靓仔"
        }
      }
    ]
  }
}

排序

GET quanzhan/user/_search
{
  "query": {
    "match": {
       "name": "王"
    }
  },
  "_source": ["name", "desc"]
}

反馈结果

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "6",
        "_score" : null,    /** 由于排序查询, 所以 分值没了 **/
        "_source" : {
          "name" : "王五",
          "age" : 20,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        },
        "sort" : [
          20
        ]
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "name" : "王六",
          "age" : 18,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        },
        "sort" : [
          18
        ]
      }
    ]
  }
}

分页查询

分页参数 from:从第几条开始 size:返回多少条数据(单页面数据)

GET quanzhan/user/_search
{
  "query": {
    "match": {
       "name": "王"
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 1
}

反馈结果

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 7,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "11",
        "_score" : null,
        "_source" : {
          "name" : "王老吉呀",
          "age" : 30,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        },
        "sort" : [
          30
        ]
      }
    ]
  }
}

数据下标是在 0 开始的, 和所学的数据结构是一样的
/search/{current}/{pagesize}

布尔值查询

bool 多条件查询

must

must 命令(mysql 中的 and), 所有条件都要符合. 比如 where id = 1 and name = xxx

GET quanzhan/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "王"
          }
        },
        {
          "match": {
            "age": "12"
          }
        }
      ]
    }
  }
}

反馈结果

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.3930416,
    "hits" : [
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "9",
        "_score" : 1.3930416,
        "_source" : {
          "name" : "王老刘",
          "age" : 12,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      }
    ]
  }
}
should

should (or) :

GET quanzhan/user/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "王"
          }
        },
        {
          "match": {
            "age": "12"
          }
        }
      ]
    }
  }
}

反馈结果

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 7,
      "relation" : "eq"
    },
    "max_score" : 1.3930416,
    "hits" : [
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "9",
        "_score" : 1.3930416,
        "_source" : {
          "name" : "王老刘",
          "age" : 12,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "5",
        "_score" : 0.45239353,
        "_source" : {
          "name" : "王六",
          "age" : 18,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "6",
        "_score" : 0.45239353,
        "_source" : {
          "name" : "王五",
          "age" : 20,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "7",
        "_score" : 0.3930416,
        "_source" : {
          "name" : "王五aaaa",
          "age" : 20,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "10",
        "_score" : 0.3930416,
        "_source" : {
          "name" : "王老吉",
          "age" : 21,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "11",
        "_score" : 0.34745687,
        "_source" : {
          "name" : "王老吉呀",
          "age" : 30,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "8",
        "_score" : 0.311347,
        "_source" : {
          "name" : "王五哈哈哈",
          "age" : 20,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      }
    ]
  }
}
not
GET quanzhan/user/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "age": "12"
          }
        }
      ]
    }
  }
}

反馈结果

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 9,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "name" : "张三",
          "age" : 18,
          "desc" : "法外狂徒",
          "tags" : [
            "渣男"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "3",
        "_score" : 0.0,
        "_source" : {
          "name" : "李四",
          "age" : 18,
          "desc" : "靓女",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "name" : "全栈自学社区",
          "age" : 18,
          "desc" : "西西",
          "tags" : [
            "技术宅",
            "温暖"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "5",
        "_score" : 0.0,
        "_source" : {
          "name" : "王六",
          "age" : 18,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "6",
        "_score" : 0.0,
        "_source" : {
          "name" : "王五",
          "age" : 20,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "7",
        "_score" : 0.0,
        "_source" : {
          "name" : "王五aaaa",
          "age" : 20,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "8",
        "_score" : 0.0,
        "_source" : {
          "name" : "王五哈哈哈",
          "age" : 20,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "10",
        "_score" : 0.0,
        "_source" : {
          "name" : "王老吉",
          "age" : 21,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "11",
        "_score" : 0.0,
        "_source" : {
          "name" : "王老吉呀",
          "age" : 30,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      }
    ]
  }
}
过滤器

filter 可以使用它进行数据过滤

  • ge 大于
  • gte 大于等于
  • lt 小于
  • lte 小于等于
GET quanzhan/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "王"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
  }
}

反馈结果

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 63,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 0.45239353,
    "hits" : [
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "5",
        "_score" : 0.45239353,
        "_source" : {
          "name" : "王六",
          "age" : 18,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "6",
        "_score" : 0.45239353,
        "_source" : {
          "name" : "王五",
          "age" : 20,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "7",
        "_score" : 0.3930416,
        "_source" : {
          "name" : "王五aaaa",
          "age" : 20,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "9",
        "_score" : 0.3930416,
        "_source" : {
          "name" : "王老刘",
          "age" : 12,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "8",
        "_score" : 0.311347,
        "_source" : {
          "name" : "王五哈哈哈",
          "age" : 20,
          "desc" : "靓仔",
          "tags" : [
            "靓仔"
          ]
        }
      }
    ]
  }
}

匹配多个条件

GET quanzhan/user/_search
{
  "query": {
    "match": {
      "tags": "术 男" /** 多个条件使用空格隔开, 只要满足其中一个结果就可以被查出 **/
    }
  }
}

反馈结果

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 2.1047382,
    "hits" : [
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "2",
        "_score" : 2.1047382,
        "_source" : {
          "name" : "张三",
          "age" : 18,
          "desc" : "法外狂徒",
          "tags" : [
            "渣男"
          ]
        }
      },
      {
        "_index" : "quanzhan",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.3460207,
        "_source" : {
          "name" : "全栈自学社区",
          "age" : 18,
          "desc" : "西西",
          "tags" : [
            "技术宅",
            "温暖"
          ]
        }
      }
    ]
  }
}

精确查询

term 查询是直接通过倒排索引指定的词条进行精确查找

关于分词

  • term 直接查询精确的
  • match 会使用分词解析器(先分析文档, 然后通过分析文档进行查询)

两个类型 text keyword

创建索引

PUT testdb
{
  "mappings": {
    "properties": {
          "name":{
      "type":"text"
    },
    "desc": {
      "type": "keyword"
    }
    }
  }
}

反馈结果

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "testdb"
}

插入数据

PUT testdb/_doc/1
{
  "name": "全栈自学社区",
  "desc": "微信公众号"
}


PUT testdb/_doc/2
{
  "name": "全栈自学社区",
  "desc": "微信公众号2"
}

反馈结果

{
  "_index" : "testdb",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

查询

GET _analyze
{
  "analyzer": "keyword",
  "text": "全栈自学社区"
}

/** 可以看到被拆分 **/
GET _analyze
{
  "analyzer": "standard",
  "text": "全栈自学社区"
}

反馈结果

{
  "tokens" : [
    {
      "token" : "全栈自学社区",
      "start_offset" : 0,
      "end_offset" : 6,
      "type" : "word",
      "position" : 0
    }
  ]
}


{
  "tokens" : [
    {
      "token" : "全",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<IDEOGRAPHIC>",
      "position" : 0
    },
    {
      "token" : "栈",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "<IDEOGRAPHIC>",
      "position" : 1
    },
    {
      "token" : "自",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "<IDEOGRAPHIC>",
      "position" : 2
    },
    {
      "token" : "学",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "<IDEOGRAPHIC>",
      "position" : 3
    },
    {
      "token" : "社",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "<IDEOGRAPHIC>",
      "position" : 4
    },
    {
      "token" : "区",
      "start_offset" : 5,
      "end_offset" : 6,
      "type" : "<IDEOGRAPHIC>",
      "position" : 5
    }
  ]
}

查询

GET testdb/_search
{
  "query": {
    "term": {
      "name":"全"
    }
  }
}

反馈结果

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.18232156,
    "hits" : [
      {
        "_index" : "testdb",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.18232156,
        "_source" : {
          "name" : "全栈自学社区",
          "desc" : "微信公众号"
        }
      },
      {
        "_index" : "testdb",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.18232156,
        "_source" : {
          "name" : "全栈自学社区",
          "desc" : "微信公众号2"
        }
      }
    ]
  }
}
GET testdb/_search
{
  "query": {
    "term": {
      "desc": "微信公众号"
    }
  }
}

反馈结果

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "testdb",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.6931471,
        "_source" : {
          "name" : "全栈自学社区",
          "desc" : "微信公众号"
        }
      }
    ]
  }
}

keyword字段不会被分词器解析

多个值匹配的精确查询

GET testdb/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "t1": "22"
          }
        },
        {
          "term": {
            "t1": "23"
          }
        }
      ]
    }
  }
}

反馈结果

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "testdb",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.6931471,
        "_source" : {
          "t1" : "22",
          "t2" : "2021-1-19"
        }
      },
      {
        "_index" : "testdb",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.6931471,
        "_source" : {
          "t1" : "23",
          "t2" : "2021-1-3"
        }
      }
    ]
  }
}

高亮查询

GET testdb/_search
{
  "query": {
    "match": {
      "name": "全栈自学社区"
    }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}

反馈结果

{
  "took" : 199,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0939294,
    "hits" : [
      {
        "_index" : "testdb",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0939294,
        "_source" : {
          "name" : "全栈自学社区",
          "desc" : "微信公众号"
        },
        "highlight" : {
          "name" : [
            "<em>全</em><em>栈</em><em>自</em><em>学</em><em>社</em><em>区</em>"
          ]
        }
      },
      {
        "_index" : "testdb",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0939294,
        "_source" : {
          "name" : "全栈自学社区",
          "desc" : "微信公众号2"
        },
        "highlight" : {
          "name" : [
            "<em>全</em><em>栈</em><em>自</em><em>学</em><em>社</em><em>区</em>"
          ]
        }
      }
    ]
  }
}

定义标签

GET testdb/_search
{
  "query": {
    "match": {
      "name": "全栈自学社区"
    }
  },
  "highlight": {
    "pre_tags": "<p class='key' style='color:red>",
    "post_tags": "</p>", 
    "fields": {
      "name": {}
    }
  }
}

反馈结果

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0939294,
    "hits" : [
      {
        "_index" : "testdb",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0939294,
        "_source" : {
          "name" : "全栈自学社区",
          "desc" : "微信公众号"
        },
        "highlight" : {
          "name" : [
            "<p class='key' style='color:red>全</p><p class='key' style='color:red>栈</p><p class='key' style='color:red>自</p><p class='key' style='color:red>学</p><p class='key' style='color:red>社</p><p class='key' style='color:red>区</p>"
          ]
        }
      },
      {
        "_index" : "testdb",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0939294,
        "_source" : {
          "name" : "全栈自学社区",
          "desc" : "微信公众号2"
        },
        "highlight" : {
          "name" : [
            "<p class='key' style='color:red>全</p><p class='key' style='color:red>栈</p><p class='key' style='color:red>自</p><p class='key' style='color:red>学</p><p class='key' style='color:red>社</p><p class='key' style='color:red>区</p>"
          ]
        }
      }
    ]
  }
}

总结

这些其实 MySql也可以做, 效率低

  • 匹配
  • 按照条件匹配
  • 精确匹配
  • 区间范围匹配
  • 匹配字段过滤
  • 多条件查询
  • 高亮查询

文章已上传gitee https://gitee.com/codingce/hexo-blog
项目地址: https://github.com/xzMhehe/codingce-java

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Elasticsearch是一个开源的分布式搜索和分析引擎,它可以用于存储、搜索和分析大量的数据。以下是一些Elasticsearch基本操作: 1. 安装和启动Elasticsearch:首先,你需要从Elasticsearch官方网站下载和安装Elasticsearch。安装完成后,你可以使用命令行或者图形界面来启动Elasticsearch。 2. 创建索引:在Elasticsearch中,数据存储在索引中。你可以使用PUT请求来创建一个新的索引。例如,使用curl命令可以发送以下请求来创建一个名为"my_index"的索引: ``` curl -XPUT 'localhost:9200/my_index' ``` 3. 添加文档:一旦索引创建好了,你可以使用POST请求来向索引中添加文档文档是以JSON格式表示的数据。以下是向名为"my_index"的索引添加一个文档的示例请求: ``` curl -XPOST 'localhost:9200/my_index/_doc' -d ' { "title": "Elasticsearch Basics", "content": "This is a basic introduction to Elasticsearch" }' ``` 4. 搜索文档:你可以使用GET请求来搜索索引中的文档。以下是一个搜索名为"my_index"的索引中包含关键字"elasticsearch"的文档的示例请求: ``` curl -XGET 'localhost:9200/my_index/_search?q=elasticsearch' ``` 5. 更新文档:使用POST请求可以更新索引中的文档。以下是更新名为"my_index"的索引中ID为1的文档的示例请求: ``` curl -XPOST 'localhost:9200/my_index/_doc/1/_update' -d ' { "doc": { "content": "This is an updated content" } }' ``` 6. 删除文档:使用DELETE请求可以删除索引中的文档。以下是删除名为"my_index"的索引中ID为1的文档的示例请求: ``` curl -XDELETE 'localhost:9200/my_index/_doc/1' ``` 这些是Elasticsearch的一些基本操作。你可以根据需要进一步探索和学习更多高级功能和API。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后端码匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值