ElasticSearch笔记

ElasticSearch学习笔记

分页查询

  • from: 开始位置

  • size: 查多少条

GET /credit_enterprise_info/_search
{
  "query": {
    "match": {
      "qymc": "大"
    }
  }
  , "from": 0
  , "size": 5
}

解决数据量很大时 总数只显示10000条

GET /credit_enterprise_info/_search
{
   "track_total_hits": true
}

只查询索引内文档数量

GET /credit_enterprise_info/_count

设置查询10000条以后的数据

PUT /credit_enterprise_info/_settings
{
   "index.max_result_window" : "1000000"
}

查询配置

GET /credit_enterprise_info/_settings
    
返回:
{
  "credit_enterprise_info" : {
    "settings" : {
      "index" : {
        "number_of_shards" : "5",
        "provided_name" : "credit_enterprise_info",
        "max_result_window" : "1000000000",
        "creation_date" : "1630920063923",
        "number_of_replicas" : "1",
        "uuid" : "CXgroui1SyqmWNDlg2-ifQ",
        "version" : {
          "created" : "7020099"
        }
      }
    }
  }
}

精确查询!

term查询是直接通过倒排索引指定的词条进行精确查找的!

  • 通过倒排索引

img

关于分词

  • trem,直接查询精确的(倒排索引直接查询)

  • match,会使用分词器解析!(先分析文档,然后在通过分析的文档进行查询!)

两个类型 text keyword

keyword字段类型不会被分词器解析

text类型可以被解析


PUT /testdb/_doc/1
{
  "name": "狂神说Java name",
  "desc": "狂神说Java desc"
}

PUT /testdb/_doc/2
{
  "name": "狂神说Java name",
  "desc": "狂神说Java desc2"
}

GET _analyze
{
  "analyzer": "keyword",
  "text": "狂神说Java name"
}

GET _analyze
{
  "analyzer": "standard",
  "text": "狂神说Java name"
}


GET /testdb/_search
{
  "query": {
    "term": {
      "name": "狂"
    }
  }
}

GET /testdb/_search
{
  "query": {
    "term": {
      "desc": "狂"
    }
  }
}

GET /testdb/_search
{
  "query": {
    "term": {
      "desc": "狂神说Java desc"
    }
  }
}

多个值匹配的精确查询

PUT /testdb/_doc/3
{
  "t1": "22",
  "t2": "2020-4-6"
}

PUT /testdb/_doc/4
{
  "t1": "33",
  "t2": "2020-4-7"
}

GET /testdb/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {
            "t1": "22"
        }},
        {"term": {
            "t1": "33"
        }}
      ]
    }
  }
}

must (and),所有条件都要符合 where id = 1 and name = XXX

must not(not)

should(or),所有条件都要符合 where id = 1 or name = XXX

filter

GET /testdb/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {
            "t1": "22"
        }},
        {"term": {
            "t1": "33"
        }}
      ]
      , "filter": {
        "range": {
          "t1": {
            "gte": 20,
            "lte": 30
          }
        }
      }
    }
  }
}
  • gt 大于
  • gte 大于等于
  • lt 小于
  • lte 小于等于!

匹配多个条件

直接分词 空格

GET /credit_enterprise_info/_search
{
  "query": {
    "match": {
      "qymc": "沈阳 齐齐哈尔"
    }
  }
}

高亮查询


GET /credit_enterprise_info/_search
{
  "query": {
    "match": {
      "qymc": "沈阳 齐齐哈尔"
    }
  }
  , "highlight": {
    "fields": {
      "qymc": {}
    }
  }
}

自定义高亮查询

GET /credit_enterprise_info/_search
{
  "query": {
    "match": {
      "qymc": "沈阳 齐齐哈尔"
    }
  }
  , "highlight": {
    "pre_tags": "<p class='key' style='color:red'>", 
    "post_tags": "</p>", 
    "fields": {
      "qymc": {}
    }
  }
}


GET /jd_goods/_search
{
	"from": 1,
	"size": 20,
	"timeout": "20s",
	"query": {
		"term": {
			"title": {
				"value": "java",
				"boost": 1.0
			}
		}
	},
	"highlight": {
		"pre_tags": ["<sapn style='color:red'>"],
		"post_tags": ["</span>"],
		"require_field_match": false,
		"fields": {
			"title": {}
		}
	}
}

模糊查询 wildcard

GET /credit_enterprise_info/_search
{
	"from": 0,
	"size": 20,
	"timeout": "20s",
	"query": {
		"bool": {
			"must": [{
				"wildcard": {
					"tyshxydm": "*925309*"
				}
			}],
			"adjust_pure_negative": true,
			"boost": 1.0
		}
	},
	"_source": {
		"includes": ["inserttime", "qymc", "tyshxydm"],
		"excludes": []
	},
	"sort": [{
		"_score": {
			"order": "desc"
		}
	}]
}

这些其实mysql也能做,知识mysql的效率比较低

  • 匹配
  • 按照条件匹配
  • 精确匹配
  • 区间范围匹配
  • 匹配字段过滤
  • 多条件查询
  • 高亮查询

索引相关

# 查看全部索引
GET _cat/indices

# 获取一个文档
GET /index/type/id

# 删除索引
DELETE /index


# 查看mapping
GET /index/_mapping

# 创建索引mapping
PUT /index
{
    "mappings": {
      "type": {
        "properties": {
          "id": {
            "type": "integer"
          },
          "industry": {
            "type": "text",
            "index": false
          },          
          "report_type": {
            "type": "text",
            "index": false
          },

          "title": {
            "type": "text",
            "index":true
          },

          "update_time": {
            "type": "date",
            "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
          },
          "url": {
            "type": "text",
            "index": false
          }
        }
      }
    }
}

说明
ignore_malformed:true 忽略格式错误的数值


# 部分更新
POST /index/type/id/_update
{
  "doc": {
    "update_time": "2019-11-13 12:12:03"
  }
}

# 查询,并过滤没有删除,分页,时间排序
get /index/_search
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must_not": {
            "term": {
              "is_del": 1
            }
          }
        }
      },
      "must": {
        "match_phrase": {
          "title": "国"
        }
      }
    }
  },
  "size": 10,
  "from": 0,
  "sort": [
    {"publish_date": {"order": "desc"}},
    {"_score": {"order": "desc"}}
    ]
  
}

# 新增字段
PUT <index>/_mapping/<type>
{
  "properties": {
    "<name>": { 
      "type":  "integer"
     }
   }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值