ElasticSearch入门3: 高级查询

 尊重原创:https://www.cnblogs.com/liuxiaoming123/p/8124969.html

单字段 模糊匹配查询与精准查询
复制代码
postman请求

POST     127.0.0.1:9200/book/_search

请求json:

{
   "query":{
           "match":{
               "name":"晓明9"
           }
   }
}

注:match 模糊查询的标识 :查询内容自动拆分成分词来查询
  若match 改为 match_phrase :精准查询 具体可以查看 http://www.cnblogs.com/liuxiaoming123/p/8119217.html
   响应结果: { "took": 51, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 0.5753642, "hits": [ { "_index": "book", "_type": "novel", "_id": "1", "_score": 0.5753642, "_source": { "name": "晓明1", "country": "china1", "age": 26, "date": "1992-08-08" } }, { "_index": "book", "_type": "novel", "_id": "5", "_score": 0.28363907, "_source": { "name": "晓明", "country": "china", "age": 26, "date": "1992-08-08" } }, { "_index": "book", "_type": "novel", "_id": "8", "_score": 0.28363907, "_source": { "name": "晓明", "country": "china", "age": 26, "date": "1992-08-08" } }, { "_index": "book", "_type": "novel", "_id": "9", "_score": 0.23911436, "_source": { "name": "晓明9", "country": "china9", "age": 26, "date": "1992-08-08" } } ] } }
复制代码

多字段 模糊匹配查询与精准查询
复制代码
postman请求URL:

POST  127.0.0.1:9200/book/_search

请求json字符串:

{
   "query":{
           "multi_match":{
               "query":"晓明china",
               "fields":["name","country"]
           }
   }
}

注:multi_match为指定多字段匹配

响应结果:

{
    "took": 42,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 0.5753642,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 0.5753642,
                "_source": {
                    "name": "晓明1",
                    "country": "china1",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "5",
                "_score": 0.47000363,
                "_source": {
                    "name": "晓明",
                    "country": "china",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "8",
                "_score": 0.47000363,
                "_source": {
                    "name": "晓明",
                    "country": "china",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 0.23911436,
                "_source": {
                    "name": "晓明9",
                    "country": "china9",
                    "age": 26,
                    "date": "1992-08-08"
                }
            }
        ]
    }
}
复制代码

语法查询 
未指定字段:
复制代码
postman请求:

POST 127.0.0.1:9200/book/_search

请求json字符串:

{
   "query":{
           "query_string":{
               "query":"(ElasticSearch AND 入门) OR SpringBoot"
           }
   }
}

返回结果:

{
    "took": 21,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 2.634553,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 2.634553,
                "_source": {
                    "name": "ElasticSearch 入门",
                    "country": "china9",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 0.2876821,
                "_source": {
                    "name": "SpringBoot",
                    "country": "china1",
                    "age": 26,
                    "date": "1992-08-08"
                }
            }
        ]
    }
}
复制代码

 语法查询 
 指定字段:

复制代码
postman请求:

POST 127.0.0.1:9200/book/_search

请求json字符串:

{
   "query":{
           "query_string":{
               "query":"SpringBoot OR 中国",
               "fields":["name","country"]
           }
   }
}

响应结果:

{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.8630463,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 0.8630463,
                "_source": {
                    "name": "SpringBoot",
                    "country": "中国",
                    "age": 26,
                    "date": "1992-08-08"
                }
            }
        ]
    }
}
复制代码

 (结构化数据的查询)

指定字段查询:(term)

复制代码
postman请求:

POST   127.0.0.1:9200/book/_search

请求json字符串:

{
    "query" : 
        {
            "term" : {"name" : "springboot"}
        }
}

响应结果:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 0.2876821,
                "_source": {
                    "name": "SpringBoot",
                    "country": "中国",
                    "age": 26,
                    "date": "1992-08-08"
                }
            }
        ]
    }
}
复制代码

注:若查询英文时 应全字母小写 精确查询

  若查询中文时 应按单个字来查询

范围查询:

注:json请求字符串中部分字段的含义

  range:范围关键字

  gte 大于等于

  lte  小于等于

  gt 大于

  lt 小于

  now 当前时间

复制代码
postman请求:

127.0.0.1:9200/book/_search

请求json字符串:

{
    "query" : 
        {
            "range" : {
                "date" : {
                        "gte":"2017-01-01",
                        "lte":"now"
                         }
                    }
        }
}

响应结果:

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "9",
                "_score": 1,
                "_source": {
                    "name": "ElasticSearch 入门",
                    "country": "china9",
                    "age": 28,
                    "date": "2017-08-08"
                }
            }
        ]
    }
}
复制代码

Filter Context(对数据进行过滤)

复制代码
postman请求:

POST  127.0.0.1:9200/book/_search

请求json字符串:

{
    "query" : {
            "bool" : {
                "filter" : {
                        "term":{
                        "age":20
                                }
                            }
                        }
              }
}

响应结果:

{
    "took": 24,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 0,
                "_source": {
                    "name": "SpringBoot",
                    "country": "中国",
                    "age": 20,
                    "date": "1992-08-08"
                }
            }
        ]
    }
}
复制代码

注: boost 固定响应结果分数的值

复制代码
postman请求:

POST 127.0.0.1:9200/_search

请求json字符串:

{
    "query" : {
            "constant_score" : {
                "filter" : {
                            "match":{
                                        "name":"晓明"
                                    }
                            },
                            "boost":2
                        }
              }
}

响应结果:

{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 8,
        "successful": 8,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 2,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "5",
                "_score": 2,
                "_source": {
                    "name": "晓明",
                    "country": "china",
                    "age": 26,
                    "date": "1992-08-08"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "8",
                "_score": 2,
                "_source": {
                    "name": "晓明8",
                    "country": "china",
                    "age": 26,
                    "date": "1992-08-08"
                }
            }
        ]
    }
}
复制代码

should关键词:或的关系  

若should改为must 关键词则表示 和的关系

复制代码
postman请求:

POST 127.0.0.1:9200/_search

请求json字符串:
    1.shuld:
{
    "query" : {
            "bool" : {
                "should" : [
                            {
                                "match":{"name":"springboot"}
                            },
                            {
                                "match":{"country":"中国"}
                            }
                            ]
                        }
              }
}

 2.must:
{
    "query" : {
            "bool" : {
                "must" : [
                            {
                                "match":{"name":"springboot"}
                            },
                            {
                                "match":{"country":"中国"}
                            }
                            ]
                        }
              }
}

3.must filter:
{
    "query" : {
            "bool" : {
                "must" : [
                            {
                                "match":{"name":"springboot"}
                            },
                            {
                                "match":{"country":"中国"}
                            }
                            ],
                   "filter":[
                            {
                                "term":{
                                    "age":20
                                }
                            }    
                            ]
                        }
              }
}

4.must_not:
{
    "query" : {
            "bool" : {
                "must_not" : {
                                "term":{"age":20}
                             }
                      }
               }
}

复制代码
 
分类: ElasticSearch
原文地址:https://www.cnblogs.com/xiaohouzai/p/9636439.html