zoukankan      html  css  js  c++  java
  • elasticsearch-搜索-基本搜索(四)

    多索引多type搜索

    分页搜索

    每页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为跳过前面数量

    搜索文档的一部分

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

    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不同

    简易搜索

    查询产品名字包含沙发同时品牌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

    所有字段中查询

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

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

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

    结构化查询DSL

    term(=)过滤

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

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

    terms(in)过滤

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

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

    range(between)过滤

    {
        "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"
                    }
                }
            }
        }
    }

    存在指定字段查询

    {
      "query": {
        "bool": {
          "filter": {
            "exists": {
              "field": "productSortItemIds"
            }
          }
        }
      }
    }

    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(like)

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

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

    mult_match( like and)

    与match相同 只是允许同时搜索多个字段  operator 是默认搜索字段会分词,只是区分分词匹配是and全部匹配或者or部分匹配

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

    filter

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

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

    is null查询

    需要在创建mapping的时候 设置字段的null_value 然后match搜索就搜索 设置的value就行了 

    验证查询

    验证一个查询是否有效

    查询搜索分词索引情况

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

    脚本字段

    可以通过脚本计算结果 但是不会返回其他字段 到时有需求可以研究一下

    #localhost:9200/test_3/doc/_search
    {
        "script_fields":{
            "new_filed":{
                "script":{
                    "lang":"painless",
                    "source":"doc['isActived'].value+1"
                }
            }
        }
    }

    响应

    {
        "took": 4,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 62342,
            "max_score": 1.0,
            "hits": [
                {
                    "_index": "test_3",
                    "_type": "doc",
                    "_id": "843_CB407",
                    "_score": 1.0,
                    "fields": {
                        "new_filed": [
                            2
                        ]
                    }
                },
                {
                    "_index": "test_3",
                    "_type": "doc",
                    "_id": "250_CB407",
                    "_score": 1.0,
                    "fields": {
                        "new_filed": [
                            2
                        ]
                    }
                },
                {
                    "_index": "test_3",
                    "_type": "doc",
                    "_id": "1040_CB407",
                    "_score": 1.0,
                    "fields": {
                        "new_filed": [
                            2
                        ]
                    }
                },
                {
                    "_index": "test_3",
                    "_type": "doc",
                    "_id": "984_CB407",
                    "_score": 1.0,
                    "fields": {
                        "new_filed": [
                            2
                        ]
                    }
                },
                {
                    "_index": "test_3",
                    "_type": "doc",
                    "_id": "284_CB407",
                    "_score": 1.0,
                    "fields": {
                        "new_filed": [
                            2
                        ]
                    }
                },
                {
                    "_index": "test_3",
                    "_type": "doc",
                    "_id": "486_CB407",
                    "_score": 1.0,
                    "fields": {
                        "new_filed": [
                            2
                        ]
                    }
                },
                {
                    "_index": "test_3",
                    "_type": "doc",
                    "_id": "414_CB407",
                    "_score": 1.0,
                    "fields": {
                        "new_filed": [
                            2
                        ]
                    }
                },
                {
                    "_index": "test_3",
                    "_type": "doc",
                    "_id": "1190_CB407",
                    "_score": 1.0,
                    "fields": {
                        "new_filed": [
                            2
                        ]
                    }
                },
                {
                    "_index": "test_3",
                    "_type": "doc",
                    "_id": "553_CB407",
                    "_score": 1.0,
                    "fields": {
                        "new_filed": [
                            2
                        ]
                    }
                },
                {
                    "_index": "test_3",
                    "_type": "doc",
                    "_id": "867_CB407",
                    "_score": 1.0,
                    "fields": {
                        "new_filed": [
                            2
                        ]
                    }
                }
            ]
        }
    }
    View Code

    查询权重分计算

    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
    ), 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
    ), 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": []
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }
    View Code

    查询explain分析器

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

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

    count查询

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

  • 相关阅读:
    zbb20190605 maven windows配置maven私服以及使用
    zbb20190528 城市经纬度json
    zbb20190528 adcode 城市编码
    zbb20190430 springboot 配置alimq
    zbb20190408 spring-boot-maven-plugin 插件的作用详解
    zbb20190131 Mybatis,mysql映射文件<!CDATA[[]]> 转义问题
    Spring Boot (29) 定时任务
    Spring Boot (28) actuator与spring-boot-admin
    Spring Boot (27) actuator服务监控与管理
    Java 8
  • 原文地址:https://www.cnblogs.com/LQBlog/p/10430784.html
Copyright © 2011-2022 走看看