美文网首页
检索,基本用法

检索,基本用法

作者: 潘大的笔记 | 来源:发表于2019-03-01 15:11 被阅读0次

检索

1、检索document

GET /index_name/type_name/id

2、轻量检索所有document

GET /index_name/type_name/_search

返回结构在hits数组中,其中_source是type_name对应的document字段属性

{
   "took":      6,
   "timed_out": false,
   "_shards": { ... },
   "hits": {
      "total":      3,
      "max_score":  1,
      "hits": [
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "3",
            "_score":         1,
            "_source": {
               "first_name":  "Douglas",
               "last_name":   "Fir",
               "age":         35,
               "about":       "I like to build cabinets",
               "interests": [ "forestry" ]
            }
         },
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "1",
            "_score":         1,
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            }
         },
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "2",
            "_score":         1,
            "_source": {
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests": [ "music" ]
            }
         }
      ]
   }
}

3、传递参数

GET /megacorp/employee/_search?q=last_name:Smith

仍然使用_search,将查询复制给参数q, 返回结果给出了所有的Smith

{
   ...
   "hits": {
      "total":      2,
      "max_score":  0.30685282,
      "hits": [
         {
            ...
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            }
         },
         {
            ...
            "_source": {
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests": [ "music" ]
            }
         }
      ]
   }
}

4、查询表达式,领域特定语言(DSL),返回的结果与之前相同

GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}

5、更复杂的搜索
使用过滤器filter,过滤年龄大于30的

GET /megacorp/employee/_search
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith" (1)
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 } (2)
                }
            }
        }
    }
}

(2)是一个range过滤器
相对于之前的查询结果,这次只返回一个employee 叫Jane Smith,32岁
6、全文搜索

GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}

返回以下结果

{
   ...
   "hits": {
      "total":      2,
      "max_score":  0.16273327,
      "hits": [
         {
            ...
            "_score":         0.16273327, (1)
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            }
         },
         {
            ...
            "_score":         0.016878016, (1)
            "_source": {
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests": [ "music" ]
            }
         }
      ]
   }
}

(1)相关性得分,默认按照相关性得分排序
7、短语搜索,精确匹配一系列单词或短语,使用match_phrase

GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}

8、高亮搜索 highlight参数

GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}

返回的结果与之前相同,但是还多一个highlight的部分,包含了about匹配的文本片段,并且以HTML标签<em><em>封装

{
   ...
   "hits": {
      "total":      1,
      "max_score":  0.23013961,
      "hits": [
         {
            ...
            "_score":         0.23013961,
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            },
            "highlight": {
               "about": [
                  "I love to go <em>rock</em> <em>climbing</em>" (1)
               ]
            }
         }
      ]
   }
}

分析(使用聚合功能)

例如:挖掘出雇员中最受欢迎的兴趣爱好

GET /megacorp/employee/_search
{
  "aggs": {
    "all_interests": {
      "terms": { "field": "interests" }
    }
  }
}

结果

{
   ...
   "hits": { ... },
   "aggregations": {
      "all_interests": {
         "buckets": [
            {
               "key":       "music",
               "doc_count": 2
            },
            {
               "key":       "forestry",
               "doc_count": 1
            },
            {
               "key":       "sports",
               "doc_count": 1
            }
         ]
      }
   }
}

组合查询。想知道叫Smith的雇员中最受欢迎的兴趣爱好

GET /megacorp/employee/_search
{
  "query": {
    "match": {
      "last_name": "smith"
    }
  },
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interests"
      }
    }
  }
}

结果只包含匹配查询的document

 ...
  "all_interests": {
     "buckets": [
        {
           "key": "music",
           "doc_count": 2
        },
        {
           "key": "sports",
           "doc_count": 1
        }
     ]
  }

聚合还支持分级汇总。例如,查询特定兴趣爱好员工的平均年龄

GET /megacorp/employee/_search
{
    "aggs" : {
        "all_interests" : {
            "terms" : { "field" : "interests" },
            "aggs" : {
                "avg_age" : {
                    "avg" : { "field" : "age" }
                }
            }
        }
    }
}

结果

 ...
  "all_interests": {
     "buckets": [
        {
           "key": "music",
           "doc_count": 2,
           "avg_age": {
              "value": 28.5
           }
        },
        {
           "key": "forestry",
           "doc_count": 1,
           "avg_age": {
              "value": 35
           }
        },
        {
           "key": "sports",
           "doc_count": 1,
           "avg_age": {
              "value": 25
           }
        }
     ]
  }

相关文章

  • 检索,基本用法

    检索 1、检索document 2、轻量检索所有document 返回结构在hits数组中,其中_source是t...

  • NSPredicate谓词

    NSPredicate可用于检索查询,是一个过滤容器,相当于数据库的where 基本用法 基本查询 注意: 如果谓...

  • time time_t tm用法

    最近搞视频检索,涉及到很多时间的计算。顺便记录下一些基本用法。 一、gmtime用法 运行结果: 作用打印出当前时...

  • Mysql入门(命令行)--过滤(2)

    高级条件检索语句andorinnot用法即表面意思所示例子:

  • 商品检索

    ElasticSearch实现商品检索 商品检索 检索入口 复杂条件检索 检索业务需要考虑的问题 商品基本的数据模...

  • 查找与替换

    查找与替换就是实现快速,高效检索信息的目的。充分利用查找和替换的十大技巧 ,快速整理报表资料。 一.基本用法 开始...

  • 4/21别说这些查找替换技巧我没告诉你

    查找与替换就是实现快速,高效检索信息的目的。充分利用查找和替换的十大技巧 ,快速整理报表资料。 一.基本用法 开始...

  • es基本检索

    More_like_this 文本相似检索 属性介绍

  • 数据库检索(Select)

    基本检索 检索单个列select column_name from table_name; 检索多个列select...

  • 定时器

    setTimeout和clearTimeout基本用法 setInterval和clearInterval基本用法...

网友评论

      本文标题:检索,基本用法

      本文链接:https://www.haomeiwen.com/subject/sxvduqtx.html