ElasticSearch的各种查询

ElasticSearch多种搜索方式
1、query string search
2、query DSL
3、queryfilter
4、full-tex search
5、phrase search
6、highlight search

1、query string search
搜索全部信息

格式:GET /index/type/_search

GET /employee/user/_search

took:耗时几毫秒 timed_out:是否超时,这里是没有
_shareds:数据拆分了5个分片,所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以) hits.total:查询结果的数量,2表示document
hits.max_score:score的含义,就是document对于一个search相关度的匹配分数,越相关,就越匹配,分数也高
hits.hits:包含了匹配搜索的document的详细数据

结果:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "employee",
        "_type": "user",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "lisi",
          "sez": "M",
          "china": 90,
          "addr": "beijing",
          "join": [
            "chengdu",
            "shenzhen",
            "wuhan"
          ]
        }
      },
      {
        "_index": "employee",
        "_type": "user",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "wangwu",
          "sez": "M",
          "china": 60,
          "addr": "xian",
          "join": [
            "xiamen",
            "nanning",
            "changsha"
          ]
        }
      }
    ]
  }
}

这个查询还可以带上一些参数,例如,搜索名字是"lisi"的人,而且按照语文成绩降序排序

GET /employee/user/_search?q=name:lisi&sort=china:desc

这种操作在生产使用比较少,因为复杂的查询很难去构建请求

2、query DSL
DSL:Domain Specified Language,特定领域的语言
http request body:请求体,可以用json的格式来构建查询语法,比较方便,可以构建各种复杂的语法

查询所有的同学

GET /employee/user/_search
{
  "query": {
    "match_all": {}
  }
}

结果

{
  "took": 91,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
   ........
        }
      }
    ]
  }
}

查询名字包含lisi的同学,同时按照语文成绩降序排序

GET /employee/user/_search
{
  "query": {
    "match": {
      "name": "lisi"
    }
  },
  "sort": [
    {
      "china": {
        "order": "desc"
      }
    }
  ]
}

分页查询同学,总共3个同学,假设每页就显示1条同学,现在显示第2页,所以就查出来第2个同学

GET /employee/user/_search
{
  "query": {
    "match_all": {}
  },
  "from": 1
  , "size": 1
}

指定要查询指定同学名称和成绩就可以

GET /employee/user/_search
{
  "query": {"match_all": {}},
  "_source": ["name","china"]
}

3、query filter
对数据进行过滤
查询所有学生中名字是lisi的,成绩大于60的记录

must是必须匹配的

GET /employee/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "lisi"
          }
        }
      ],
      "filter": {
        "range": {
          "china": {
            "gt": 60
          }
        }
      }
    }
  }
}

4、full-text search(全文检索)
先构建倒排索引,在用查询的值拆词以后去匹配,score表示相似度,值越大,相似度越高

GET /employee/user/_search
{
  "query": {
    "match": {
      "join": "shenzhen beijing"
    }
  }
}

结果

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "employee",
        "_type": "user",
        "_id": "5",
        "_score": 0.5753642,
        "_source": {
          "name": "lisi",
          "sez": "F",
          "china": 60,
          "addr": "beijing",
          "join": [
            "chengdu",
            "shenzhen",
            "beijing"
          ]
        }
      },
      {
        "_index": "employee",
        "_type": "user",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "name": "lisi",
          "sez": "F",
          "china": 70,
          "addr": "beijing",
          "join": [
            "chengdu",
            "shenzhen",
            "wuhan"
          ]
        }
      }
    ]
  }
}

5、phrase search(短语搜索)

跟全文检索相对应,相反,全文检索会将输入的搜索串拆解开来,去倒排索引里面去一一匹配,只要能匹配上任意一个拆解后的单词,就可以作为结果返回
phrase search,要求输入的搜索串,必须在指定的字段文本中,完全包含一模一样的,才可以算匹配,才能作为结果返回

GET /ecommerce/product/_search
{
    "query" : {
        "match_phrase" : {
            "producer" : "yagao producer"
        }
    }
}

6、highlight search(高亮搜索结果)

GET /employee/user/_search
{
  "query": {
    "match": {
      "name": "lisi"
    }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}

ElasticSearch的各种查询_第1张图片

你可能感兴趣的:(Elasticsearch)