Elastic Search 搜索的简单使用

搜索的简单使用

准备工作

  • 删掉nba索引
    • DELETE localhost:9200/nba
  • 新建一个索引,并且指定mapping新增document
    • PUT localhost:9200/nba
    • {
          "mappings":{
              "properties":{
                  "name":{
                      "type":"text"
                  },
                  "team_name":{
                      "type":"text"
                  },
                  "position":{
                      "type":"text"
                  },
                  "play_year":{
                      "type":"long"
                  },
                  "jerse_no":{
                      "type":"keyword"
                  }
              }
          }
      }
    • 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": 18,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "name": "哈登",
                    "team_name": "火箭",
                    "position": "得分后卫",
                    "play_year": 10,
                    "jerse_no": "13"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_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": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "name": "哈登",
                    "team_name": "火箭",
                    "position": "得分后卫",
                    "play_year": 10,
                    "jerse_no": "13"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "name": "库里",
                    "team_name": "勇士",
                    "position": "控球后卫",
                    "play_year": 10,
                    "jerse_no": "30"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "name": "詹姆斯",
                    "team_name": "湖人",
                    "position": "小前锋",
                    "play_year": 15,
                    "jerse_no": "23"
                }
            }
        ]
    }
}

match

POST localhost:9200/nba/_search

参数:
{
    "query":{
        "match":{
            "position":"后卫"
        }
    }
}

返回值:
{
    "took": 55,
    "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": 12,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.797622,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "1",
                "_score": 2.797622,
                "_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"
        }
    }
}

返回值:
{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.8596163,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.8596163,
                "_source": {
                    "name": "库里",
                    "team_name": "勇士",
                    "position": "控球后卫",
                    "play_year": 10,
                    "jerse_no": "30",
                    "title": "the best shooter"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.6542599,
                "_source": {
                    "name": "詹姆斯",
                    "team_name": "湖人",
                    "position": "小前锋",
                    "play_year": 15,
                    "jerse_no": "23",
                    "title": "the best small forward"
                }
            }
        ]
    }
}

 

 

posted @ 2020-03-16 23:11  认真对待世界的小白  阅读(508)  评论(0编辑  收藏  举报