zoukankan      html  css  js  c++  java
  • 搜索的简单使用

      这这里主要是存在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"
            }
          }
        ]
      }
    }
    

      

  • 相关阅读:
    Jmeter 添加信息头
    Jmeter 添加 计数器
    Jmeter 用户定义的变量的使用
    Jmeter 获取系统时间
    Fiddler抓包工具使用技巧
    使用adb安装apk包到手机上的安装步骤:
    如何验证所使用的电脑Windows系统是正版还是盗版的方法
    VMware 15 虚拟机安装 win 7 操作系统步骤
    MySQL在windows上安装过程
    SecureCRT的下载、安装过程
  • 原文地址:https://www.cnblogs.com/juncaoit/p/12650524.html
Copyright © 2011-2022 走看看