栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Elasticsearch基础

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Elasticsearch基础

下载网址:https://www.elastic.co/cn/downloads/past-releases#elasticsearch

HTML1 :我爱我的祖国,我爱编程
HTML2 :我爱编程,我是个快乐的小码农

正向索引


分词器将内容进行分词处理,搜索时,es数据库将想要查询的字段对分词后的结果进行挨个匹配 效率低下

倒排索引

倒排索引是按照分词与文档进行映射

插入数据

POST请求 加 _update 为修改操作,只修改指定的的字段

查询 建立索引
PUT /test_keyword
{
   "mappings": {
			"properties": {
				"age": {
					"type": "long"
				},
				"birthday": {
					"type": "date"
				},
				"name": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"tags": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				}
			}
		}
}
普通查询 查询全部数据
GET /user/_search
{
  "query": {
    "match_all": {}
  }
}
查询name为小胖的
GET user/_doc/_search
{
  "query":{
     "match":{
       "name":"小胖"
     }
  }
}
结果
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0498221,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0498221,
        "_source" : {
          "name" : "小胖",
          "age" : 26,
          "birthday" : "1997-04-06",
          "tags" : [
            "呆萌",
            "认真",
            "散漫"
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.6931471,
        "_source" : {
          "name" : "大胖",
          "age" : 13,
          "birthday" : "1997-04-06",
          "tags" : [
            "稳重",
            "情商高",
            "慵懒"
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.35667494,
        "_source" : {
          "name" : "小宇",
          "age" : 13,
          "birthday" : "1997-04-06",
          "tags" : [
            "渣男",
            "心机boy",
            "高富帅"
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.35667494,
        "_source" : {
          "name" : "小轩",
          "age" : 13,
          "birthday" : "1997-04-06",
          "tags" : [
            "小胖子",
            "呆萌",
            "认真"
          ]
        }
      }
    ]
  }
}

为什么会出现这样的情况?
我们知道 字符串有两种类型 text 和 keywords ,text可用分词器进行分隔,keywords不可用分词器进行分隔

以小胖为例:

当 “小胖” 为 text 类型时  使用_analyze分词器对其进行分割
GET _analyze
{
  "analyzer": "standard",
  "text":"小胖"
}
结果如下
{
  "tokens" : [
    {
      "token" : "小",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "",
      "position" : 0
    },
    {
      "token" : "胖",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "",
      "position" : 1
    }
  ]
}

当 “小胖” 为 keyword 类型时  使用_analyze分词器对其进行分割
GET _analyze
{
  "analyzer": "keyword",
  "text":"小胖"
}
结果如下
{
  "tokens" : [
    {
      "token" : "小胖",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "word",
      "position" : 0
    }
  ]
}

由此可见当我们使用 match对 “小胖” 进行查找的时候 会查出满足以“小” 和“胖” 建立索引的数据 。

那我们用 term 进行查询的时候会查出来什么结果?

GET user/_doc/_search
{
  "query":{
     "term":{
       "name":"小胖"
     }
  }
}

结果显而易见 不会出来,为什么?( 这里先做一个补充,如果使用 match 进行匹配查询时,会先将查询字段‘小胖’进行分词 -‘小’和‘胖’,然后利用倒排索引进行查询,这也是为什么上面案例会查出来多条的原因;如果使用 term 进行查询是,不会进行分词,直接使用‘小胖’进行查询)

GET user/_doc/_search
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
查询指定列
GET user/_doc/_search
{
  "query":{
     "match":{
       "name":"小"
     }
  },
  "_source":["name","tags"]
}

结果:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.6931471,
        "_source" : {
          "name" : "大胖",
          "tags" : [
            "稳重",
            "情商高",
            "慵懒"
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.6931471,
        "_source" : {
          "name" : "小胖",
          "tags" : [
            "呆萌",
            "认真",
            "散漫"
          ]
        }
      }
    ]
  }
}
排序
GET user/_doc/_search
{
  "query":{
     "match":{
       "name":"小"
     }
  },
  "sort":[
     {
          "age":{
       "order":"desc"
     } 
    }
  ]
}

结果:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "小胖",
          "age" : 26,
          "birthday" : "1997-04-06",
          "tags" : [
            "呆萌",
            "认真",
            "散漫"
          ]
        },
        "sort" : [
          26
        ]
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "小宇",
          "age" : 13,
          "birthday" : "1997-04-06",
          "tags" : [
            "渣男",
            "心机boy",
            "高富帅"
          ]
        },
        "sort" : [
          13
        ]
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "小轩",
          "age" : 13,
          "birthday" : "1997-04-06",
          "tags" : [
            "小胖子",
            "呆萌",
            "认真"
          ]
        },
        "sort" : [
          13
        ]
      }
    ]
  }
}
分页
GET user/_doc/_search
{
  "query":{
     "match":{
       "name":"小"
     }
  },
  "sort":[
     {
          "age":{
       "order":"desc"
     } 
    }
  ],
  "from":0,   
  "size":1
}
结果:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "小胖",
          "age" : 26,
          "birthday" : "1997-04-06",
          "tags" : [
            "呆萌",
            "认真",
            "散漫"
          ]
        },
        "sort" : [
          26
        ]
      }
    ]
  }
}

from 从第几条数据开始;size 查询后边的多少条数据

多条件匹配 同时满足 (and)
查询既满足 名称为 小胖 又满足 年龄为 26 的  
GET user/_doc/_search
{
  "query":{
     "bool":{
       "must":[
         {
            "match":{
               "name": "小胖"
              } 
         },
          {
               "match":{
                 "age": 26
               }
          }
       ]
     }
  }
}
结果:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "小胖",
          "age" : 26,
          "birthday" : "1997-04-06",
          "tags" : [
            "呆萌",
            "认真",
            "散漫"
          ]
        },
        "sort" : [
          26
        ]
      }
    ]
  }
}
或者 (or)
GET user/_doc/_search
{
  "query":{
    "bool":{
      "should":[
         {
            "match":{
               "name": "小胖"
              } 
         },
          {
               "match":{
                 "age": 26
               }
          }
       ]
    }
  }
}
结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 2.049822,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 2.049822,
        "_source" : {
          "name" : "小胖",
          "age" : 26,
          "birthday" : "1997-04-06",
          "tags" : [
            "呆萌",
            "认真",
            "散漫"
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.6931471,
        "_source" : {
          "name" : "大胖",
          "age" : 13,
          "birthday" : "1997-04-06",
          "tags" : [
            "稳重",
            "情商高",
            "慵懒"
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.35667494,
        "_source" : {
          "name" : "小宇",
          "age" : 13,
          "birthday" : "1997-04-06",
          "tags" : [
            "渣男",
            "心机boy",
            "高富帅"
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.35667494,
        "_source" : {
          "name" : "小轩",
          "age" : 13,
          "birthday" : "1997-04-06",
          "tags" : [
            "小胖子",
            "呆萌",
            "认真"
          ]
        }
      }
    ]
  }
}

不满足
GET user/_doc/_search
{
  "query":{
    "bool":{
      "must_not":[
          {
               "match":{
                 "age": 26
               }
          }
       ]
    }
  }
}


{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "name" : "小宇",
          "age" : 13,
          "birthday" : "1997-04-06",
          "tags" : [
            "渣男",
            "心机boy",
            "高富帅"
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "name" : "小轩",
          "age" : 13,
          "birthday" : "1997-04-06",
          "tags" : [
            "小胖子",
            "呆萌",
            "认真"
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.0,
        "_source" : {
          "name" : "大胖",
          "age" : 13,
          "birthday" : "1997-04-06",
          "tags" : [
            "稳重",
            "情商高",
            "慵懒"
          ]
        }
      }
    ]
  }
}
过滤
GET user/_doc/_search
{
  "query":{
    "bool":{
      "must":[
          {
               "match":{
                 "name":"胖" 
               }
          }
       ],
       "filter":{
         "range":{
           "age":{
             "gt":20,
             "lt":26
           }
         }
       }
    }
  }
}

结果 :
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

lt 小于 ;lte 小于等于 ; gt 大于; gte大于等于

数组类型多条件匹配
GET user/_doc/_search
{
  "query":{
    "bool":{
      "must":[
          {
               "match":{
                 "tags":"呆萌 情商"   //空格分开 多条件匹配
               }
          }
       ]
    }
  }
}
结果:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 2.4079456,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 2.4079456,
        "_source" : {
          "name" : "大胖",
          "age" : 13,
          "birthday" : "1997-04-06",
          "tags" : [
            "稳重",
            "情商高",
            "慵懒"
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.4723402,
        "_source" : {
          "name" : "小胖",
          "age" : 26,
          "birthday" : "1997-04-06",
          "tags" : [
            "呆萌",
            "认真",
            "散漫"
          ]
        }
      },
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.3862942,
        "_source" : {
          "name" : "小轩",
          "age" : 13,
          "birthday" : "1997-04-06",
          "tags" : [
            "小胖子",
            "呆萌",
            "认真"
          ]
        }
      }
    ]
  }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/992116.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号