二、Elasticsearch 六种搜索方式

查询结果解释

  • took - 耗费多少毫秒
  • time_out - 是否超时
  • _shards - 数据分片情况
  • hits.total - 查询结果的数量
  • hits.max_score - document 对于一个 search 的相关度匹配分数,越相关分数越高
  • hits.hits - 匹配 document 的详细数据

query string search

  • 语法
GET /index/type/_search

查询全部商品

GET /ecommerce/product/_search
{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "ecommerce",
                "_type": "product",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "name": "gaolujie yagao",
                    "desc": "gaoxiao meibai",
                    "price": 30,
                    "producer": "gaolujie producer",
                    "tags": [
                        "meibai",
                        "fangzhu"
                    ]
                }
            }
        ...
        ]
    }
}

条件查询排序

查询商品名称中包含 yagao 的商品,而且按照售价降序排序

GET /ecommerce/product/_search?q=name:yagao&sort=price:desc

query DSL 【常用】

Domain Specified Language:特定领域的语言

  • 语法
GET /index/type/_search
{
    "query": {
        "match_all": {}
    }
}

请求体放在 request body 里面 ,可以用 json 格式来构建各种复杂的语法,比 query string search 强大

查询所有商品

GET /ecommerce/product/_search
{
    "query": {
        "match_all": {}
    }
}

条件查询排序

查询商品名称中包含 yagao 的商品,而且按照售价降序排序

GET /ecommerce/product/_search
{
    "query": {
        "match": {
            "name": "yagao"
        }
    },
    "sort": [
        {
            "price": "desc"
        }
    ]
}

分页查询

当前总共3条商品数据,假设每页显示1条,现在显示第2页,所以只查出来第2个商品

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

查询指定属性

指定只查询商品的 name、price 属性

GET /ecommerce/product/_search
{
    "query": {
        "match_all": {}
    },
    "_source": [
        "name",
        "price"
    ]
}

query filter

范围查询

搜索商品名称包含 yaogao ,而且售价大于 25 元的商品

GET /ecommerce/product/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "name": "yagao"
                    }
                }
            ],
            "filter": {
                "range": {
                    "price": {
                        "gt": 25
                    }
                }
            }
        }
    }
}
  • gt : 大于
  • lt : 小于
  • eq : 等于
  • ne : 不等于
  • gte : 大于等于
  • lte : 小于等于

full-text search

全文检索 - 会将输入的关键字拆解,去倒排索引里面一一匹配,只要匹配上任意一个拆解后的单词,就可以作为结果返回

  • 先新增一条数据
PUT /ecommerce/product/4
{
    "name": "special yagao",
    "desc": "special meibai",
    "price": 50,
    "producer": "special yagao producer",
    "tag": [
        "meibai"
    ]
}
  • 查询 producer
GET /ecommerce/product/_search
{
  "query":{
    "match": {
      "producer":"yagao producer"
    }
  }
}
  • 查询结果展示
{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.2825179,
        "hits": [
            {
                "_index": "ecommerce",
                "_type": "product",
                "_id": "4",
                "_score": 1.2825179,
                "_source": {
                    "name": "special yagao",
                    "desc": "special meibai",
                    "price": 50,
                    "producer": "special yagao producer",
                    "tag": [
                        "meibai"
                    ]
                }
            },
            {
                "_index": "ecommerce",
                "_type": "product",
                "_id": "3",
                "_score": 0.09037233,
                "_source": {
                    "name": "zhonghua yagao",
                    "desc": "caoben zhiwu",
                    "price": 40,
                    "producer": "zhonghua producer",
                    "tags": [
                        "qingxin",
                        "fangzhu"
                    ]
                }
            },
            {
                "_index": "ecommerce",
                "_type": "product",
                "_id": "2",
                "_score": 0.09037233,
                "_source": {
                    "name": "jiajieshi yagao",
                    "desc": "youxiao fangzhu",
                    "price": 25,
                    "producer": "jiajieshi producer",
                    "tags": [
                        "fangzhu"
                    ]
                }
            },
            {
                "_index": "ecommerce",
                "_type": "product",
                "_id": "1",
                "_score": 0.09037233,
                "_source": {
                    "name": "gaolujie yagao",
                    "desc": "gaoxiao meibai",
                    "price": 30,
                    "producer": "gaolujie producer",
                    "tags": [
                        "name"
                    ]
                }
            }
        ]
    }
}

yagao producer 作为查询条件时会被拆分成 yagaoproducer 两个查询条件,但是查询结果的 _score 会体现跟原始查询条件的匹配度

phrase search

短语搜索 - 跟全文检索相反,搜索关键字必须在指定的字段文本中完全匹配

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

highlight search

高亮搜索 - 被匹配的关键字将会在查询结果中高亮标注(默认<em>

GET /ecommerce/product/_search
{
    "query": {
        "match": {
            "producer": "producer"
        }
    },
    "highlight": {
        "fields": {
            "producer": {}
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章