数据准备

image
image

query string search

搜索全部商品

GET /company/emp/_search
序号 属性 描述
1 took 耗费了几毫秒
2 timed_out 是否超时,这里是没有
3 _shards 数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以)
4 hits.total 查询结果的数量,9个document
5 hits.max_score score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
6 hits.hits 包含了匹配搜索的document的详细数据
{
  "took": 41,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 9,
    "max_score": 1,
    "hits": [
      {
        "_index": "company",
        "_type": "emp",
        "_id": "5",
        "_score": 1,
        "_source": {
          "name": "xiaoming",
          "age": 25,
          "sex": "woman",
          "birthday": "1991-01-02",
          "hobby": "i like song"
        }
      },
      {
        "_index": "company",
        "_type": "emp",
        "_id": "8",
        "_score": 1,
        "_source": {
          "name": "zhaoliu",
          "age": 18,
          "sex": "woman",
          "birthday": "1998-01-02",
          "hobby": "i like compute game"
        }
        ......
      }
      }
    ]
  }
}

query string search

搜索company中包含trivel的人员,而且按照年龄降序排序

GET /company/_search?q=hobby:trivel&sort=age:desc

注意:适用于临时的在命令行使用一些工具,比如curl,快速的发出请求,来检索想要的信息;但是如果查询请求很复杂,是很难去构建的
在生产环境中,几乎很少使用query string search

query DSL

a.查询所有的company所有员工

GET /company/_search
{
  "query": { "match_all": {} }
}

b.查询hobby包含trivel的商品,同时按照age降序排序

GET /company/_search
{
    "query" : {
        "match" : {
            "hobby" : "trivel"
        }
    },
    "sort": [
        { "age": "desc" }
    ]
}

c.分页查询company,总共9条记录,假设每页就显示1条,现在显示第2页,所以就查出来第2个记录

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

d.指定要查询出来记录的name和hobby就可以

GET company/_search
{
  "query": { "match_all": {} },
  "_source": ["name", "hobby"]
}

query filter

搜索hobby包含trivel,而且age大于23元的记录

GET /company/_search
{
    "query" : {
        "bool" : {
            "must" : {
                "match" : {
                    "hobby" : "trivel" 
                }
            },
            "filter" : {
                "range" : {
                    "age" : { "gt" : 23 } 
                }
            }
        }
    }
}

full-text search(全文检索)

GET /company/_search
{
    "query" : {
        "match" : {
            "hobby" : "trivel"
        }
    }
}

phrase search

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

GET /company/_search
{
    "query" : {
        "match_phrase" : {
            "hobby" : "and music"
        }
    }
}

查询结果

{
  "took": 35,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.2320077,
    "hits": [
      {
        "_index": "company",
        "_type": "emp",
        "_id": "7",
        "_score": 1.2320077,
        "_source": {
          "name": "zhaowu",
          "age": 31,
          "sex": "man",
          "birthday": "1997-01-02",
          "hobby": "i like trivel and music"
        }
      }
    ]
  }
}

highlight search(高亮搜索结果)

GET /company/_search
{
    "query" : {
        "match" : {
            "hobby" : "trivel"
        }
    },
    "highlight": {
        "fields" : {
            "hobby" : {}
        }
    }
}

查询结果

{
  "took": 187,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.86312973,
    "hits": [
      {
        "_index": "company",
        "_type": "emp",
        "_id": "4",
        "_score": 0.86312973,
        "_source": {
          "name": "wangba",
          "age": 25,
          "sex": "woman",
          "birthday": "1996-01-02",
          "hobby": "i like trivel"
        },
        "highlight": {
          "hobby": [
            "i like <em>trivel</em>"
          ]
        }
      },
      {
        "_index": "company",
        "_type": "emp",
        "_id": "7",
        "_score": 0.6160039,
        "_source": {
          "name": "zhaowu",
          "age": 31,
          "sex": "man",
          "birthday": "1997-01-02",
          "hobby": "i like trivel and music"
        },
        "highlight": {
          "hobby": [
            "i like <em>trivel</em> and music"
          ]
        }
      },
      {
        "_index": "company",
        "_type": "emp",
        "_id": "3",
        "_score": 0.25316024,
        "_source": {
          "name": "wangwu",
          "age": 17,
          "sex": "man",
          "birthday": "1998-01-02",
          "hobby": "i like trivel"
        },
        "highlight": {
          "hobby": [
            "i like <em>trivel</em>"
          ]
        }
      }
    ]
  }
}