基本Rest命令说明:

1.索引的基本操作

1.1.创建索引

PUT  /索引名/~类型名~/文档id
{
    请求体
}
##添加或者覆盖更新(会覆盖未选中字段)
PUT mine/user/3
{
  "name":"wangwu3",
  "age":12
}
{
  "_index" : "mine",
  "_type" : "user",
  "_id" : "3",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

完成了自动增加索引

类型

  • 字符串
    text、keyword
  • 数值类型
    long、integer、short、byte、dubble、float、half float、scaled float
  • 日期类型
    date
  • te布尔值类型
    boolean
  • 二进制类型
    binary
  • ……

1.3.指定字段类型

##指定字段类型
PUT test_index
{
  "mappings": {
    "properties": {
      "name":{
        "type": "text"
      },
      "age":{
        "type": "long"
      },
      "birth":{
        "type": "date"
      },
      "desc":{
        "type": "keyword"
      }
    }
  }
}
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test_index"
}

获取信息 ,通过GET请求获取我们需要的信息

GET test_index
{
  "test_index" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "birth" : {
          "type" : "date"
        },
        "desc" : {
          "type" : "keyword"
        },
        "name" : {
          "type" : "text"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1622183843872",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "rqoMjj0OQTejZLPp5o6WCw",
        "version" : {
          "created" : "7060199"
        },
        "provided_name" : "test_index"
      }
    }
  }
}

1.4.查看默认信息

PUT test_01/_doc/1
{
  "name":"李四",
  "age":25,
  "birth":"2020-05-27",
  "desc":"654321"
}

GET test_01
{
  "test_01" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "birth" : {
          "type" : "date"
        },
        "desc" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1622184365053",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "6N1-OU2KQ8q6DL6w_tes0g",
        "version" : {
          "created" : "7060199"
        },
        "provided_name" : "test_01"
      }
    }
  }
}

如果不指定字段类型、那么es会默认指定数据类型

扩展:通过命令elasticSearch索引情况,通过get _cat/ 可以获得es的当前很多信息

##扩展:通过命令elasticSearch索引情况,通过get _cat/ 可以获得es的当前很多信息
GET _cat/indices?v

1.5.删除索引

##删除索引
DELETE test_index
{
  "acknowledged" : true
}

2.文档的基本操作

2.1.添加数据

PUT /everyingo/user/1
{
  "name":"张三",
  "age":10,
  "desc":"湖北人",
  "tags":["技术宅","暖男","细心"]
}
PUT /everyingo/user/2
{
  "name":"李四",
  "age":20,
  "desc":"湖南人",
  "tags":["死肥宅","冷男","粗心"]
}
PUT /everyingo/user/3
{
  "name":"王五",
  "age":30,
  "desc":"江西人",
  "tags":["就是宅","暖女","小心"]
}
{
  "_index" : "everyingo",
  "_type" : "user",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

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

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

2.2.获取数据GET

GET /everyingo/user/3
{
  "_index" : "everyingo",
  "_type" : "user",
  "_id" : "3",
  "_version" : 1,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "王五",
    "age" : 30,
    "desc" : "江西人",
    "tags" : [
      "就是宅",
      "暖女",
      "小心"
    ]
  }
}

2.3.更新数据PUT(put如果不传值就会被覆盖)

PUT /everyingo/user/3
{
  "name":"王五",
  "age":30,
  "desc":"江西人",
  "tags":["就是宅","暖女","小心心"]
}
{
  "_index" : "everyingo",
  "_type" : "user",
  "_id" : "3",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

2.4.POST更新,推荐使用这种方式(灵活性更好)

POST /everyingo/user/3/_update
{
  "doc":{
    "name":"王5"
  }
}
{
  "_index" : "everyingo",
  "_type" : "user",
  "_id" : "3",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}
  ![](https://img2020.cnblogs.com/blog/1223444/202105/1223444-20210528145500837-258990001.png)

3.简单的搜索

3.1获取数据GET

GET /everyingo/user/3
{
  "_index" : "everyingo",
  "_type" : "user",
  "_id" : "3",
  "_version" : 3,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "王5",
    "age" : 30,
    "desc" : "江西人",
    "tags" : [
      "就是宅",
      "暖女",
      "小心心"
    ]
  }
}

3.2.简单的条件搜索,可以根据默认的映射规则,产生基本的查询

GET /everyingo/user/_search?q=name:张
{
  "took" : 123,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.3862942,
    "hits" : [
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.3862942,
        "_source" : {
          "name" : "张三",
          "age" : 10,
          "desc" : "湖北人",
          "tags" : [
            "技术宅",
            "暖男",
            "细心"
          ]
        }
      }
    ]
  }
}

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

4.1.查询的参数使用Json构造

GET /everyingo/user/_search
{
  "query": {
    "match": {
      "desc": "湖"
    }
  }
}
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.87546873,
    "hits" : [
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.87546873,
        "_source" : {
          "name" : "张三",
          "age" : 10,
          "desc" : "湖北人",
          "tags" : [
            "技术宅",
            "暖男",
            "细心"
          ]
        }
      },
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "2",
        "_score" : 0.87546873,
        "_source" : {
          "name" : "李四",
          "age" : 20,
          "desc" : "湖南人",
          "tags" : [
            "死肥宅",
            "冷男",
            "粗心"
          ]
        }
      }
    ]
  }
}

4.2.输出结果不想要那么多,指定字段返回(类似select field1,field2 from table_name)

GET /everyingo/user/_search
{
  "query": {
    "match": {
      "desc": "湖"
    }
  }
  , "_source": ["name","desc"]
}
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.87546873,
    "hits" : [
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.87546873,
        "_source" : {
          "name" : "张三",
          "desc" : "湖北人"
        }
      },
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "2",
        "_score" : 0.87546873,
        "_source" : {
          "name" : "李四",
          "desc" : "湖南人"
        }
      }
    ]
  }
}

4.3.排序

GET /everyingo/user/_search
{
  "query": {
    "match": {
      "desc": "湖"
    }
  },
  "sort": [
    {
      "age": {
        "order": "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" : "everyingo",
        "_type" : "user",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "李四",
          "age" : 20,
          "desc" : "湖南人",
          "tags" : [
            "死肥宅",
            "冷男",
            "粗心"
          ]
        },
        "sort" : [
          20
        ]
      },
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "张三",
          "age" : 10,
          "desc" : "湖北人",
          "tags" : [
            "技术宅",
            "暖男",
            "细心"
          ]
        },
        "sort" : [
          10
        ]
      }
    ]
  }
}

4.4.分页查询(from:从第几个数据开始,size:返回多少条数据即pageSize)

GET /everyingo/user/_search
{
  "query": {
    "match": {
      "desc": "湖"
    }
  }, 
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 1
}
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "李四",
          "age" : 20,
          "desc" : "湖南人",
          "tags" : [
            "死肥宅",
            "冷男",
            "粗心"
          ]
        },
        "sort" : [
          20
        ]
      }
    ]
  }
}

4.5.布尔值查询

  • must(and),所有的条件都要符合 where id=1 and name=xxx
GET /everyingo/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "desc": "湖"
          }
        },
        {
          "match": {
            "age": "20"
          }
        }
      ]
    }
  }
}
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.4700036,
    "hits" : [
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.4700036,
        "_source" : {
          "name" : "李四",
          "age" : 20,
          "desc" : "湖南人",
          "tags" : [
            "死肥宅",
            "冷男",
            "粗心"
          ]
        }
      }
    ]
  }
}

  • should(or),符合其中一个条件 where id=1 or name=xxx
GET /everyingo/user/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "desc": "湖"
          }
        },
        {
          "match": {
            "age": 30
          }
        }
      ]
    }
  }
}
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "王5",
          "age" : 30,
          "desc" : "江西人",
          "tags" : [
            "就是宅",
            "暖女",
            "小心心"
          ]
        }
      },
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "张三",
          "age" : 10,
          "desc" : "湖北人",
          "tags" : [
            "技术宅",
            "暖男",
            "细心"
          ]
        }
      },
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "2",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "李四",
          "age" : 20,
          "desc" : "湖南人",
          "tags" : [
            "死肥宅",
            "冷男",
            "粗心"
          ]
        }
      }
    ]
  }
}

  • must_not(not) where id!=xxx
GET /everyingo/user/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "age": 20
          }
        }
      ]
    }
  }
}
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 137,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "name" : "张三",
          "age" : 10,
          "desc" : "湖北人",
          "tags" : [
            "技术宅",
            "暖男",
            "细心"
          ]
        }
      },
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "3",
        "_score" : 0.0,
        "_source" : {
          "name" : "王5",
          "age" : 30,
          "desc" : "江西人",
          "tags" : [
            "就是宅",
            "暖女",
            "小心心"
          ]
        }
      }
    ]
  }
}

4.6.过滤器filter

# gt  大于
# gte 大于等于
# lt  小于
# lte 小于等于

GET /everyingo/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "desc": "湖"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 10,
            "lte": 15
          }
        }
      }
    }
  }
}
#! 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.4700036,
    "hits" : [
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "张三",
          "age" : 10,
          "desc" : "湖北人",
          "tags" : [
            "技术宅",
            "暖男",
            "细心"
          ]
        }
      }
    ]
  }
}

4.7.匹配多个条件(多个条件使用空格隔开,只要满足其中一个结果就可以被查出,这个时候可以通过分值基本的判断)

GET /everyingo/user/_search
{
  "query": {
    "match": {
      "tags": "技术 女"
    }
  }
}
#! 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" : 1.9988267,
    "hits" : [
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.9988267,
        "_source" : {
          "name" : "张三",
          "age" : 10,
          "desc" : "湖北人",
          "tags" : [
            "技术宅",
            "暖男",
            "细心"
          ]
        }
      },
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "3",
        "_score" : 0.94566,
        "_source" : {
          "name" : "王5",
          "age" : 30,
          "desc" : "江西人",
          "tags" : [
            "就是宅",
            "暖女",
            "小心心"
          ]
        }
      }
    ]
  }
}

4.8.精确查询

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

关于分词:

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

两个类型

  • text 会被分词器解析
  • keyword 不会被分词器解析

没有被拆分:

#没有被拆分
GET _analyze
{
  "analyzer": "keyword",
  "text": "武汉说Java name"
}
{
  "tokens" : [
    {
      "token" : "武汉说Java name",
      "start_offset" : 0,
      "end_offset" : 12,
      "type" : "word",
      "position" : 0
    }
  ]
}

可以看到被拆分:

#可以看到被拆分
GET _analyze
{
  "analyzer": "standard",
  "text": "武汉说Java name"
}
{
  "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" : "java",
      "start_offset" : 3,
      "end_offset" : 7,
      "type" : "<ALPHANUM>",
      "position" : 3
    },
    {
      "token" : "name",
      "start_offset" : 8,
      "end_offset" : 12,
      "type" : "<ALPHANUM>",
      "position" : 4
    }
  ]
}

text 和 keyword 区别

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

#
PUT /testdb/_doc/1
{
  "name":"武汉说Java name",
  "desc":"武汉说Java desc"
}
#
PUT /testdb/_doc/2
{
  "name":"武汉说Java name2",
  "desc":"武汉说Java desc2"
}
GET /testdb
{
  "testdb" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "desc" : {
          "type" : "keyword"
        },
        "name" : {
          "type" : "text"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1622185788983",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "3xfyB2M6QYCuV0vIc6CtPA",
        "version" : {
          "created" : "7060199"
        },
        "provided_name" : "testdb"
      }
    }
  }
}
GET /testdb/_search
{
  "query": {
    "term": {
      "name":"java"
    }
  }
}

#result
{
  "took" : 3,
  "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" : "武汉说Java name",
          "desc" : "武汉说Java desc"
        }
      },
      {
        "_index" : "testdb",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.18232156,
        "_source" : {
          "name" : "武汉说Java name2",
          "desc" : "武汉说Java desc2"
        }
      }
    ]
  }
}
#keyword字段类型不会被分词器解析
GET /testdb/_search
{
  "query": {
    "term": {
      "desc":"武汉说Java desc"
    }
  }
}

#result
{
  "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" : "武汉说Java name",
          "desc" : "武汉说Java desc"
        }
      }
    ]
  }
}

4.9.多个值匹配精确查询

##多个值匹配精确查询
GET /everyingo/user/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "desc": "江"
          }
        },
        {
          "term": {
            "age": 20
          }
        }
      ]
    }
  }
}
#! 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" : 1.0,
    "hits" : [
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "李四",
          "age" : 20,
          "desc" : "湖南人",
          "tags" : [
            "死肥宅",
            "冷男",
            "粗心"
          ]
        }
      },
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "3",
        "_score" : 0.9808291,
        "_source" : {
          "name" : "王5",
          "age" : 30,
          "desc" : "江西人",
          "tags" : [
            "就是宅",
            "暖女",
            "小心心"
          ]
        }
      }
    ]
  }
}

4.10.高亮查询

##高亮查询
GET /everyingo/user/_search
{
  "query": {
    "match": {
      "desc": "湖"
    }
  },
  "highlight": {
    "fields": {
      "desc": {}
    }
  }
}
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 131,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.4700036,
    "hits" : [
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "张三",
          "age" : 10,
          "desc" : "湖北人",
          "tags" : [
            "技术宅",
            "暖男",
            "细心"
          ]
        },
        "highlight" : {
          "desc" : [
            "<em>湖</em>北人"
          ]
        }
      },
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "2",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "李四",
          "age" : 20,
          "desc" : "湖南人",
          "tags" : [
            "死肥宅",
            "冷男",
            "粗心"
          ]
        },
        "highlight" : {
          "desc" : [
            "<em>湖</em>南人"
          ]
        }
      }
    ]
  }
}

4.11.自定义搜索高亮条件

##自定义搜索高亮条件
GET /everyingo/user/_search
{
  "query": {
    "match": {
      "desc": "湖"
    }
  },
  "highlight": {
    "pre_tags": "<p class='key' style='color:red'>",
    "post_tags": "</p>", 
    "fields": {
      "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.4700036,
    "hits" : [
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "张三",
          "age" : 10,
          "desc" : "湖北人",
          "tags" : [
            "技术宅",
            "暖男",
            "细心"
          ]
        },
        "highlight" : {
          "desc" : [
            "<p class='key' style='color:red'>湖</p>北人"
          ]
        }
      },
      {
        "_index" : "everyingo",
        "_type" : "user",
        "_id" : "2",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "李四",
          "age" : 20,
          "desc" : "湖南人",
          "tags" : [
            "死肥宅",
            "冷男",
            "粗心"
          ]
        },
        "highlight" : {
          "desc" : [
            "<p class='key' style='color:red'>湖</p>南人"
          ]
        }
      }
    ]
  }
}

posted on 2021-05-28 15:14  whn051799  阅读(114)  评论(0编辑  收藏  举报