Basic query (Query Query Chinese)

 

check sentence: 

GET /lib4/user/_search
{
  "query": {
    "term": {"interests":"唱歌"}
  }
}

#terms:查询某个字段里含有多个关键词的文档
GET /lib4/user/_search 
{
  "query":{
    "terms":{
      "interests": ["喝酒","唱歌"]
    }
  }
}

 data preparation:

#2.7.1数据准备
PUT /lib4
{ 
  "settings":{
    "number_of_shards" : 3, 
    "number_of_replicas" :0
  },
  "mappings":{
    "user":{
      "properties":{
          "name": {"type":"text","analyzer":"ik_max_word"}, 
          "address": {"type":"text","analyzer": "ik_max_word"},
          "age": {"type" :"integer"},
          "interests": {"type":"text","analyzer": "ik_max_word"},
          "birthday": {"type":"date"}
        }
      }
    }
}
    
    

 

#ik芾有两个分词器
#ik_max_word :会将文本做最细粒度的拆分;尽可能多的拆分出词语
#ik_smart:会做最粗粒度的斥分;已被分出的词语将不会再次被其它词语占有
#2.7.2 term查询和terms查询
#term query会去倒排索引中寻找确切的term,它并不知道分词器的存在。
#这种查询适合keyword、numeric、date.
#term:查询某个字段里含有某个关键词的文档

Add five documents: 


PUT /lib4/user/1
{
  "name" : "赵六",
  "address" : "黑龙江省 铁岭",
  "age" : 50,
  "birthday" : "1970-12-12",
  "interests": "喜欢喝酒,锻炼,说相声"
}

PUT /lib4/user/2
{
  "name" :"赵明",
  "address" :"北京海淀区清河",
  "age" : 20,
  "birthday" : "1998-10-12" ,
  "interests": "喜欢喝酒,锻炼,唱歌"
}


PUT /lib4/user/3
{
  "name" : "lisi",
  "address" :"北京海淀区清河",
  "age" : 23,
  "birthday" : "1998-10-12",
  "interests": "喜欢喝酒,锻炼,唱歌"
}


PUT /lib4/user/4
{
  "name" :"王五",
  "address" : " 北京海淀区清河",
  "age" : 26,
  "birthday" : "1995-10-12",
  "interests": "喜欢编程,听音乐,旅游"
}

PUT /lib4/user/5
{
  "name" : "张三",
  "address" :"北京海淀区清河",
  "age" : 29,
  "birthday" : "1988-10-12",
  "interests": "喜欢摄影,听音乐,跳舞"
}

term and query terms 

term represents the exact match, that is, no word is analyzed, the document must contain the entire search terms

#term和terms查询
#查询字段中含有赵的
GET /lib4/user/_search
{
  "query": {
    "term": {"name":"赵"}
  }
}

#指定多个关键字,只要包含其中一个就会被查询出来
GET /lib4/user/_search
{
  "query":{
    "terms":{
      "interests": ["喝酒","唱歌"]
    }
  }
}

#控制返回的数据条数  取前2条
GET /lib4/user/_search
{
  "from":0,
  "size": 2, 
  "query":{
    "terms":{
      "interests": ["喝酒","唱歌"]
    }
  }
}

#版本号的返回
GET /lib4/user/_search
{
  "version":true,
  "query":{
    "terms":{
      "interests": [" 喝酒", "唱歌"]
    }
  }
}

match the query match is known to have said that before the existence of the word breaker

Zhao six will be divided into two words containing the word Zhao and six will be queried out

#match查询
GET /lib4/user/_search
{
  "query":{
    "match":{"name": "赵六"}
  }
}

Then the digital type will not be such a query word of this age, will not be divided into two and 020 are so query results to match the information from the document age 20

GET /lib4/user/_search
{
  "query":{
    "match":{"age": 20}
  }
}


GET /lib4/user/_search
{
  "query": {
    "match_all": {}
  }
}

 

#multi_match specify multiple fields keyword matching; match_phrase exact phrase matching; _source specified field information query results returned

#指定多个字段匹配
GET /lib4/user/_search
{
  "query":{
    "multi_match": {
      "query": "唱歌",
      "fields": ["interests", "name"]
    }
  }
}

#短语匹配
GET lib4/user/_search
{
  "query":{
    "match_phrase" :{"interests": "锻炼,说相声"}
  }
}

#返回指定的字段
GET /lib4/user/_search
{
  "_source": ["address" , "name"],
  "query": {
    "match": {"interests": "唱歌"}
  }
}

 

 

 

 

Published 298 original articles · won praise 107 · Views 140,000 +

Guess you like

Origin blog.csdn.net/ywl470812087/article/details/104932549