一、query string search
    搜索全部商品:GET /ecommerce/produce/_search

    {
    “took”: 53, 耗费了几毫秒
    “timed_out”: false, 是否超时
    “_shards”: { 数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard上
    “total”: 5,
    “successful”: 5,
    “skipped”: 0,
    “failed”: 0
    },
    “hits”: { 查询结果的数量,3个document
    “total”: 3,
    “max_score”: 1, score的含义就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数就越高
    “hits”: [ 包含了匹配搜索的document的详细数据
    {
    “_index”: “ecommerce”,
    “_type”: “produce”,
    “_id”: “2”,
    “_score”: 1,
    “_source”: {
    “name”: “jiajieshi yagao”,
    “desc”: “youxiao fangzhu”,
    “price”: 25,
    “producer”: “jiajieshi producer”,
    “tags”: [
    “fangzhu”
    ]
    }
    },
    {
    “_index”: “ecommerce”,
    “_type”: “produce”,
    “_id”: “1”,
    “_score”: 1,
    “_source”: {
    “name”: “jiaqiangban gaolujie yagao”,
    “desc”: “gaoxiao meibei”,
    “price”: 30,
    “producer”: “gaolujie producer”,
    “tags”: [
    “meibai”,
    “fangzhu”
    ]
    }
    },
    {
    “_index”: “ecommerce”,
    “_type”: “produce”,
    “_id”: “3”,
    “_score”: 1,
    “_source”: {
    “name”: “zhonghua yagao”,
    “desc”: “caoben zhiwu”,
    “price”: 40,
    “producer”: “zhonghua producer”,
    “tags”: [
    “qingxin”
    ]
    }
    }
    ]
    }
    }

    搜索商品名称中包含yagao的商品,而且按照售价降序排序:
    GET /ecommerce/produce/_search?q=name:yagao&sort=price:desc
    适用于临时的再命令行中使用一些工具,比如curl,快速发出请求,来检索想要的信息,但是如果查询请求很复杂,是很难去构建在生产环境中,几乎很少使用query string search

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

    查询所有的商品:

    GET /ecommerce/produce/_search
    {
    “query”:{“match_all”:{}}
    }

    查询名称包含yagao的商品,同时按照价格降序排序

    GET /ecommerce/produce/_search
    {
    “query”:{“match”:
    {
    “name”:”yagao”
    }
    },
    “sort”: [
    {
    “price”: {
    “order”: “desc”
    }
    }
    ]
    }

    分页查询商品

    GET /ecommerce/produce/_search
    {
    “query”:{“match_all”: {}},
    “from”: 1,
    “size”: 2
    }

    指定要查询出的名称和价格就可以

    GET /ecommerce/produce/_search
    {
    “query”: {“match_all”: {}},
    “_source”: [“name”,”price”]
    }

    更加适合生产环境的使用,可以构建复杂的查询

    三、query filter,对数据进行过滤
    搜索商品名称包含yagao,而且售价大于25元的商品

    GET /ecommerce/produce/_search
    {
    “query”: {
    “bool”: {
    “must”: [
    {
    “match”: {
    “name”: “yagao”
    }
    }
    ],
    “filter”: {“range”: {
    “price”: {
    “gte”: 10,
    “lte”: 25
    }
    }}
    }
    }
    }

    四、full-text search,全文检索

    GET /ecommerce/produce/_search
    {
    “query”: {
    “match”: {
    “producer”: “special yagao”
    }
    }
    }

    按照匹配度分数排序的。

    五、phrash search,短语搜索
    与全文检索相对应,全文检索会将输入的搜索串拆解开来,去倒排索引里面去一一匹配,只要能匹配上任意一个拆解的单词,就可以作为结果返回,短语搜索要求输入的搜索串,必须包含一模一样的才可以算匹配,才能作为结果返回。

    GET /ecommerce/produce/_search
    {
    “query”: {
    “match_phrase”: {
    “producer”: “yagao producer”
    }
    }
    }

    六、highlight search,高亮

    GET /ecommerce/produce/_search
    {
    “query”: {
    “match”: {
    “producer”: “yagao producer”
    }
    },
    “highlight”: {
    “fields”: {“producer”: {}}
    }
    }