搜索的简单使⽤


准备⼯作

  • 删掉nba索引
  • DELETE localhost:9200/nba
  • 新建⼀个索引,并且指定mapping
  • PUT localhost:9200/nba

{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "text"
},
"play_year": {
"type": "long"
},
"jerse_no": {
"type": "keyword"
}
}
}
}
新增document
  • PUT localhost:9200/nba/_doc/1
{
"name": "哈登",
"team_name": "⽕箭",
"position": "得分后卫",
"play_year": 10,
"jerse_no": "13"
}
  • PUT localhost:9200/nba/_doc/2
{
"name": "库⾥",
"team_name": "勇⼠",
"position": "控球后卫",
"play_year": 10,
"jerse_no": "30"
}
  • PUT localhost:9200/nba/_doc/3
{
"name": "詹姆斯",
"team_name": "湖⼈",
"position": "⼩前锋",
"play_year": 15,
"jerse_no": "23"
}
term(词条)查询和full text(全⽂)查询

  • 词条查询​:词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时,才匹配搜索。
  • 全⽂查询​:ElasticSearch引擎会先分析查询字符串,将其拆分成多个分词,只要已分析的字段中包含词条的任意⼀个,或全部包含,就匹配查询条件,返回该⽂档;如果不包含任意⼀个分词,表示没有任何⽂档匹配查询条件

单条term查询
  • POST localhost:9200/nba/_search
{
"query": {
"term": {
"jerse_no": "23"
}
}
}
多条term查询
  • POST localhost:9200/nba/_search
{
"query": {
"terms": {
"jerse_no": [
"23",
"13"
]
}
}
}
{
"took": 21,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"name": "哈登",
"team_name": "⽕箭",
"position": "得分后卫",
"play_year": 10,
"jerse_no": "13"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "3",
"_score": 1,
"_source": {
"name": "詹姆斯",
"team_name": "湖⼈",
"position": "⼩前锋",
"play_year": 15,
"jerse_no": "23"
}
}
]
}
}
match_all
  • POST localhost:9200/nba/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 10
}
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1,
"hits": [{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"name": "哈登",
"team_name": "⽕箭",
"position": "得分后卫",
"play_year": 10,
"jerse_no": "13"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"_score": 1,
"_source": {
"name": "库⾥",
"team_name": "勇⼠",
"position": "控球后卫",
"play_year": 10,
"jerse_no": "30"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "3",
"_score": 1,
"_source": {
"name": "詹姆斯",
"team_name": "湖⼈",
"position": "⼩前锋",
"play_year": 15,
"jerse_no": "23"
}
}
]
}
}
match
  • POST localhost:9200/nba/_search
{
"query": {
"match": {
"position": "后卫"
}
}
}
{
"took": 89,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.90630186,
"hits": [{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_score": 0.90630186,
"_source": {
"name": "哈登",
"team_name": "⽕箭",
"position": "得分后卫",
"play_year": 10,
"jerse_no": "13"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"_score": 0.90630186,
"_source": {
"name": "库⾥",
"team_name": "勇⼠",
"position": "控球后卫",
"play_year": 10,
"jerse_no": "30"
}
}
]
}
}
multi_match
  • POST localhost:9200/nba/_update/2
{
"doc": {
"name": "库⾥",
"team_name": "勇⼠",
"position": "控球后卫",
"play_year": 10,
"jerse_no": "30",
"title": "the best shooter"
}
}
  • POST localhost:9200/nba/_search
{
"query": {
"multi_match": {
"query": "shooter",
"fields": ["title", "name"]
}
}
}
{
"query": {
"multi_match": {
"query": "shooter",
"fields": ["*title", "name"]
}
}
}
match_phrase
  • post localhost:9200/nba/_search
{
"query": {
"match_phrase": {
"position": "得分后卫"
}
}
}
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 3.0384295,
"hits": [{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_score": 3.0384295,
"_source": {
"name": "哈登",
"team_name": "⽕箭",
"position": "得分后卫",
"play_year": 10,
"jerse_no": "13"
}
}]
}
}
match_phrase_prefix
  • POST localhost:9200/nba/_update/3
{
"doc": {
"name": "詹姆斯",
"team_name": "湖⼈",
"position": "⼩前锋",
"play_year": 15,
"jerse_no": "23",
"title": "the best small forward"
}
}
  • POST localhost:9200/nba/_search
{
"query": {
"match_phrase_prefix": {
"title": "the best s"
}
}
}