elasticsearch 权威指南搜索阅读笔记(四)

时间:2021-10-14 00:35:46

多索引多type搜索

elasticsearch 权威指南搜索阅读笔记(四)

分页搜索

每页5条 查询一到3页数据

第一页:http://127.0.0.1:9200/blogs2/product/_search?size=5&from=0

第二页:http://127.0.0.1:9200/blogs2/product/_search?size=5&from=5

第三页:http://127.0.0.1:9200/blogs2/product/_search?size=5&from=10

size参数为每页显示数量 from为跳过前面数量

搜索文档的一部分

elasticsearch 权威指南搜索阅读笔记(四)

或者通过指定排除属性和包含属性 支持匹配

http://localhost:9200/twitter/tweet/1?_source_include=*.id&_source_exclude=entities

搜索多个索引(type)文档

参数

{
"docs":[{
"_index":"blogs",
"_type":"product",
"_id":"1"
},{
"_index":"blogs2",
"_type":"product",
"_id":"2"
}]
}

结果

{
"docs": [
{
"_index": "blogs",
"_type": "product",
"_id": "1",
"_version": 13,
"_seq_no": 10,
"_primary_term": 1,
"found": true,
"_source": {
"productName": "测试修改",
"price": 11,
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花",
null
],
"videw": 1
}
},
{
"_index": "blogs2",
"_type": "product",
"_id": "2",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"productName": "法瑞思 天然椰棕床垫棕垫硬床垫定做 薄乳胶棕榈榻榻米床垫定制折叠1.2/1.5/1.8",
"price": 10,
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花"
]
}
}
]
}

如果index相同type不同

elasticsearch 权威指南搜索阅读笔记(四)

简易搜索

查询产品名字包含沙发同时品牌id为1的产品信息

or搜索?

文档是and搜索我测试时or搜索

http://127.0.0.1:9200/blogs2/product/_search?q=productName:大幅度 +price:10 如果一个字段多只查询 http://127.0.0.1:9200/blogs2/product/_search?q=productName:(大幅度 床垫) +price:10

elasticsearch 权威指南搜索阅读笔记(四)

所有字段中查询

查询所有字段里面包含床垫的文档

http://127.0.0.1:9200/blogs2/product/_search?q=测试

ES会将文档里面的所有值都拼接成一个串来查找

结构化查询DSL

term过滤

主要用于精确匹配 比如年龄  bool

{
"query":{
"term":{
"price":12
}
}
}

terms过滤

跟term相同 terms支持多值搜索  下面会搜索出单价为12和13的搜索出来

{
"query":{
"terms":{
"price":[12,13]
}
}
}

range过滤

{
"query": {
"range": {
"price": {
"gt": 10
}
}
}
}

将会查出价格大于10的  gt大于lt小于 gte大于等于 lte小于等于

大于等于小余等于

{
"query": {
"range": {
"price": {
"gt": 10,
"lt":13
}
}
}
}

exist

非空判断

{
"query": {
"missing": {
"field":"price"
}
}
}

字段不存在或为空判断

{
"query": {
"bool": {
"must_not": {
"exists": {
"field": "price"
}
}
}
}
}

bool过滤

合并多个查询条件的bool值

must:多个条件完全匹配相当于and

must_not 多个条件取想法 相当于<>

should: 至少有一个条件匹配相当于or

如下面例子

{
"query": {
"bool": {
"must":[
{"term":{"price":10}},
{"term":{"orgId":1}}
],
"numst_not":{
"term":{"id":1}
},
"should":[
{"term":{"id":10}},
{"term":{"id":20}}
]
}
}
}

(price=10 and orgId=1) and (id<>1) and (id=10 or id=20)

match_all

空查询

{
"query": {
"match_all":{}
}
}

match

在搜索确定值类型字段 将会按照完全匹配。如果是full text则是采用分词

{
"query": {
"match":{"price":10}
}
}

mult_match

与match相同 只是允许同时搜索多个字段

{
"query": {
"multi_match": {
"query": "不错的",
"fields": [
"productName","remark"
],
"operator": "AND"//OR AND
}
}
}

filter

filter将不进行_score打分 性能会高一点

{
"query": {
"bool": {
"must": {
"match": {
"productName": "床垫"
}
},
"filter": {
"match": {
"remark": "床垫"
} }
}
}
}

验证查询

验证一个查询是否有效

elasticsearch 权威指南搜索阅读笔记(四)

查询搜索分词索引情况

http://127.0.0.1:9200/blogs2/product/_validate/query?explain

elasticsearch 权威指南搜索阅读笔记(四)

查询权重分计算

get http://127.0.0.1:9200/index/type/id/_explain

body

{
"query":{
"match":{
"productName":"jw"
}
}
}

返回结果

{
"_index": "test",
"_type": "doc",
"_id": "560_406",
"matched": true,
"explanation": {
"value": 5.3691883,
"description": "sum of:",
"details": [
{
"value": 2.6373672,
"description": "weight(Synonym(productName:j productName:jw) in 0) [PerFieldSimilarity], result of:",
"details": [
{
"value": 2.6373672,
"description": "score(doc=0,freq=1.0 = termFreq=1.0\n), product of:",
"details": [
{
"value": 2.0502589,
"description": "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:",
"details": [
{
"value": 56,
"description": "docFreq",
"details": []
},
{
"value": 438,
"description": "docCount",
"details": []
}
]
},
{
"value": 1.2863581,
"description": "tfNorm, computed as (freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength)) from:",
"details": [
{
"value": 1,
"description": "termFreq=1.0",
"details": []
},
{
"value": 1.2,
"description": "parameter k1",
"details": []
},
{
"value": 0.75,
"description": "parameter b",
"details": []
},
{
"value": 32.90639,
"description": "avgFieldLength",
"details": []
},
{
"value": 15,
"description": "fieldLength",
"details": []
}
]
}
]
}
]
},
{
"value": 2.731821,
"description": "weight(productName:w in 0) [PerFieldSimilarity], result of:",
"details": [
{
"value": 2.731821,
"description": "score(doc=0,freq=1.0 = termFreq=1.0\n), product of:",
"details": [
{
"value": 2.1236863,
"description": "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:",
"details": [
{
"value": 52,
"description": "docFreq",
"details": []
},
{
"value": 438,
"description": "docCount",
"details": []
}
]
},
{
"value": 1.2863581,
"description": "tfNorm, computed as (freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength)) from:",
"details": [
{
"value": 1,
"description": "termFreq=1.0",
"details": []
},
{
"value": 1.2,
"description": "parameter k1",
"details": []
},
{
"value": 0.75,
"description": "parameter b",
"details": []
},
{
"value": 32.90639,
"description": "avgFieldLength",
"details": []
},
{
"value": 15,
"description": "fieldLength",
"details": []
}
]
}
]
}
]
}
]
}
}

查询explain分析器

http://127.0.0.1:9200/index/_search?_source

{
"profile":true,
"query":{
"match":{
"productName":"来啦"
}
}
}

count查询

通过url设置?_source=false 不返回_source 或者 get /index/type/_count