• 搜索的简单使用


      这这里主要是存在term与match的查询介绍。

    一:term

      词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时,才匹配搜索。

    1.准备工作

      新建索引,然后新建mappings

    PUT /nba/
    {
      "mappings": {
        "properties": {
          "name": {
    				"type": "text"
    			},
    			"team_name": {
    				"type": "text"
    			},
    			"position": {
    				"type": "text"
    			},
    			"play_year": {
    				"type": "long"
    			},
    			"jerse_no": {
    				"type": "keyword"
    			}
        }
      }
    }
    

      添加三条数据:

    PUT /nba/_doc/1
    {
      "name": "哈登",
    	"team_name": "⽕箭",
    	"position": "得分后卫",
    	"play_year": 10,
    	"jerse_no": "13"
    }
    
    PUT /nba/_doc/2
    {
    	"name": "库⾥",
    	"team_name": "勇⼠",
    	"position": "控球后卫",
    	"play_year": 10,
    	"jerse_no": "30"
    }
    
    PUT /nba/_doc/3
    {
    	"name": "詹姆斯",
    	"team_name": "湖⼈",
    	"position": "⼩前锋",
    	"play_year": 15,
    	"jerse_no": "23"
    }
    

      

    2.单条查询

    GET /nba/_search
    {
      "query": {
        "term":{
          "jerse_no": "13"
        }
      }
    }
    

      效果:

    {
      "took" : 961,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 0.9808292,
        "hits" : [
          {
            "_index" : "nba",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 0.9808292,
            "_source" : {
              "name" : "哈登",
              "team_name" : "⽕箭",
              "position" : "得分后卫",
              "play_year" : 10,
              "jerse_no" : "13"
            }
          }
        ]
      }
    }
    

      

    3.多条查询

    GET /nba/_search
    {
      "query": {
        "terms": {
          "jerse_no":[
            "23","13"  
          ]
        }
      }
    }
    

      效果:

    {
      "took" : 24,
      "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【会存在分词】

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

    1.match_al

      全部查询:

    GET /nba/_search
    {
      "query": {
        "match_all": {}
      },
      "from": 0,
      "size": 2
    }
    

      結果:

    {
      "took" : 0,
      "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"
            }
          }
        ]
      }
    }
    

      

    2.match

    GET /nba/_search
    {
      "query": {
        "match": {
          "position": "得分后卫"
        }
      }
    }
    

      效果:

    {
      "took" : 60,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "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"
            }
          },
          {
            "_index" : "nba",
            "_type" : "_doc",
            "_id" : "2",
            "_score" : 0.90630186,
            "_source" : {
              "name" : "库⾥",
              "team_name" : "勇⼠",
              "position" : "控球后卫",
              "play_year" : 10,
              "jerse_no" : "30"
            }
          }
        ]
      }
    }
    

      

    2.multi_match

      修改mapping,添加一个字段:

    POST /nba/_mapping
    {
          "properties" : {
            "jerse_no" : {
              "type" : "keyword"
            },
            "name" : {
              "type" : "text"
            },
            "play_year" : {
              "type" : "long"
            },
            "position" : {
              "type" : "text"
            },
            "team_name" : {
              "type" : "text"
            },
            "title":{
              "type":"text"
            }
          
      }
    }
    

      新增记录:

    PUT /nba/_doc/4
    {
    	"jerse_no": "13",
    	"title": "火箭"
    }
    

      查询:

    GET /nba/_search
    {
      "query": {
        "multi_match": {
          "query": "火箭",
          "fields": ["team_name","title"]
        }
      }
    }
    

      效果:

    {
      "took" : 257,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "relation" : "eq"
        },
        "max_score" : 0.9808292,
        "hits" : [
          {
            "_index" : "nba",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 0.9808292,
            "_source" : {
              "name" : "哈登",
              "team_name" : "⽕箭",
              "position" : "得分后卫",
              "play_year" : 10,
              "jerse_no" : "13"
            }
          },
          {
            "_index" : "nba",
            "_type" : "_doc",
            "_id" : "4",
            "_score" : 0.5753642,
            "_source" : {
              "jerse_no" : "13",
              "title" : "火箭"
            }
          }
        ]
      }
    }
    

      

    3.match_phase

      进一步说明:会对输入做分词,但是需要结果中也包含所有的分词,而且顺序要求一样。以"hello world"为例,要求结果中必须包含hello和world,而且还要求他们是连着的,顺序也是固定的,hello that word不满足,world hello也不满足条件。

      进行对比。

    GET /nba/_search
    {
      "query":{
        "match_phrase": {
          "position": "得"
        }
      }
    }
    
    
    GET /nba/_search
    {
      "query":{
        "match_phrase": {
          "position": "后卫"
        }
      }
    }
    

      效果:

    # GET /nba/_search
    {
      "took" : 0,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 0.94566005,
        "hits" : [
          {
            "_index" : "nba",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 0.94566005,
            "_source" : {
              "name" : "哈登",
              "team_name" : "⽕箭",
              "position" : "得分后卫",
              "play_year" : 10,
              "jerse_no" : "13"
            }
          }
        ]
      }
    }
    
    
    # GET /nba/_search
    {
      "took" : 0,
      "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"
            }
          }
        ]
      }
    }
    

      

    4.match_phase_prefix

      适合单词进行匹配.

      先修改文档:

    POST /nba/_update/4
    {
      "doc":{
        "jerse_no" : "13",
        "title" : "best the shooter"
      }
    }
    
    GET /nba/_doc/4
    

      查询:

    GET /nba/_search
    {
      "query": {
        "match_phrase_prefix": {
          "title": "th"
        }
      }
    }
    

      效果:

    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 0.2876821,
        "hits" : [
          {
            "_index" : "nba",
            "_type" : "_doc",
            "_id" : "4",
            "_score" : 0.2876821,
            "_source" : {
              "jerse_no" : "13",
              "title" : "best the shooter"
            }
          }
        ]
      }
    }
    

      

  • 相关阅读:
    npm包发布过程
    react树状组件
    js数据结构处理--------扁平化数组处理为树结构数据
    js数据结构处理--------树结构数据遍历
    JS fetch
    JS promise
    JS 闭包
    JS 异步回调
    三角形加正方形
    webAPI的分类
  • 原文地址:https://www.cnblogs.com/juncaoit/p/12650524.html
Copyright © 2020-2023  润新知