ElasticSearch学习

文章40 |   阅读 24262 |   点赞0

来源:https://blog.csdn.net/ywl470812087/category_9621251.html

elasticsearch基本查询三(英文分词)match查询

x33g5p2x  于2021-12-19 转载在 其他  
字(4.6k)|赞(0)|评价(0)|浏览(741)
#match查询
#match query知道分词器的存在,会对filed进行分词操作, 然后再查询
GET /ib3/user/_search 
{ 
  "query":{ 
    "match":{ "name": "zhaoliu" }
  }
}
GET /lib3/user/_search 
{ 
  "query":{ 
    "match":{ "age": 20}
  }
}
#match_all:查询所有文档
GET /ib3/user/_search
{ 
  "query":{"match_all":{}}
}
#multi_match:可以指定多个字段
GET /ib3/user/_search 
{ 
  "query":{ 
    "multi_match": { 
      "query": "Ilyou", 
      "fields": ["interests" ,"name"]
    }
  }
}
#match_ phrase:短语匹配查询
#ElasticSearch引擎首先分析(analyze) 查询字符串,
#从分析后的文本中构建短语查询,
#这意味着必须匹配短语中的所有分词,
#并且保证各个分词的相对位置不变:
GET lib3/user/_search 
{ 
  "query":{
    "match_phrase":{"interests": "duanlian, shuoxiangsheng" }
  }
}

term查询试试,没有找到,因为term是不知道分词的,会对    "name": "zhaoliu zhaoming"  当成一个关键字,索引这个关键字在倒排索引是没有的,所以没有找到

terms查询同样不知道分词(在倒排索引中   "name": "zhaoliu zhaoming"   会被看成一个关键字,在倒排索引是不存在这个索引的)而且terms是数组查询,不支持单个字符串查询的

#match query知道分词器的存在,会对filed进行分词操作, 然后再查询    #match指定单个字段条件查询

GET /lib3/user/_search
{
  "query":{
    "match":{"name": "zhaoliu zhaoming"}
  }
}
{
  "took" : 38,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.6931472,
    "hits" : [
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "2",
        "_score" : 0.6931472,
        "_source" : {
          "name" : "zhaoming",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 20,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu, duanlian, changge"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "zhaoliu",
          "address" : "hei long jiang sheng tie ling shi",
          "age" : 50,
          "birthday" : "1970-12-12",
          "interests" : "xi buan hejiu, duanlian, lvyou"
        }
      }
    ]
  }
}

我们在看下匹配度的问题

GET /lib3/user/_search
{
  "query":{
    "match":{"interests": "duanlian changge"}
  }
}
{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.3862944,
    "hits" : [
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.3862944,
        "_source" : {
          "name" : "zhaoming",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 20,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu, duanlian, changge"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "3",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "lisi",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 23,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu,duanlian, changge"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "5",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "zhangsan",
          "address" : "bei jing chao yang qu",
          "age" : 29,
          "birthday" : "1988-10-12",
          "interests" : "xi huan tingyinyue , changge , tiaowu"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "zhaoliu",
          "address" : "hei long jiang sheng tie ling shi",
          "age" : 50,
          "birthday" : "1970-12-12",
          "interests" : "xi buan hejiu, duanlian, lvyou"
        }
      }
    ]
  }
}
#查询user索引下年龄20的文档
GET /lib3/user/_search
{
  "query":{
    "match":{"age": 20}
  }
}
#查询索引user下的所有文档
GET /lib3/user/_search
{
  "query":{
    "match_all":{}
  }
}

#multi_match:可以指定多个字段条件查询

#multi_match指定多个字段条件查询
#只要指定字段条件中含有changge的这个关键字都会被查询出来
#那么含有changge也会被检索到
GET /lib3/user/_search
{
  "query" :{
    "multi_match": {
      "query": "changge",
      "fields": ["interests" , "name"]
    }
  }
}

#短语匹配  match_phrase

#短语匹配,在interests这个字段中含有完全一样的短语就会被查询出来
GET lib3/user/_search
{
  "query":{
    "match_phrase":{"interests": "duanlian, changge"}
  }
}

相关文章