您的位置:首页 > 产品设计 > UI/UE

ElasticSearch38:初识搜索引擎_上机动手实战常用的各种query搜索语法

2018-01-04 11:21 691 查看
1.match all

GET /company/employee/_search

{

  "query": {

    "match_all": {}

  }

}

2.match

GET /company/employee/_search

{

  "query": {

    "match": {

      "name": "tom"

    }

  }

}

执行结果:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "company",
"_type": "employee",
"_id": "1",
"_score": 0.2876821,
"_source": {
"address": {
"province": "zhejiang",
"city": "wenzhou",
"country": "china"
},
"name": "tom",
"age": 31,
"join_date": "2015-01-02"
}
}
]
}
}


3.multi_match

例子:

表示title或者content中包含elasticsearch都行,都可以查询出来

GET /website/article/_search

{

  "query": {

    "multi_match": {

      "query": "elasticsearch",

      "fields": ["title","content"]

    }

  }

}

执行结果
{
"took": 255,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.25316024,
"hits": [
{
"_index": "website",
"_type": "article",
"_id": "1",
"_score": 0.25316024,
"_source": {
"title": "my elasticsearch article1",
"content": "elasticsearch is good",
"author_id": 10010
}
},
{
"_index": "website",
"_type": "article",
"_id": "3",
"_score": 0.25316024,
"_source": {
"title": "my elasticsearch article1",
"content": "elasticsearch is bad",
"author_id": 100
}
}
]
}
}


4.range query

可以放到query,也可以放到filter里面

例子:查询年龄大于等于30的员工

GET /company/employee/_search

{

  "query": {

    "range": {

      "age": {

        "gte": 30

      }

    }

  }

}

执行结果:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "company",
"_type": "employee",
"_id": "1",
"_score": 1,
"_source": {
"address": {
"province": "zhejiang",
"city": "wenzhou",
"country": "china"
},
"name": "tom",
"age": 31,
"join_date": "2015-01-02"
}
}
]
}
}


5.term query

将term中字段指定的值作为整个值,去倒排索引中查找

例子:因为倒排索引中没有elasticsearch is good这个词(倒排索引中有elasticsearch, is, good三个词),所以执行结果没有任何数据

GET /website/article/_search

{

  "query": {

    "term": {

      "content": "elasticsearch is good"

    }

  }

}

执行结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}


查看分词效果:

GET /_analyze

{

  "text": "elasticsearch is good",

  "analyzer": "standard"

}

分词结果:
{
"tokens": [
{
"token": "elasticsearch",
"start_offset": 0,
"end_offset": 13,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "is",
"start_offset": 14,
"end_offset": 16,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "good",
"start_offset": 17,
"end_offset": 21,
"type": "<ALPHANUM>",
"position": 2
}
]
}


match的执行:

GET /website/article/_search

{

  "query": {

    "match": {

      "content": "elasticsearch is good"

    }

  }

}

执行结果
{
"took": 27,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.7594807,
"hits": [
{
"_index": "website",
"_type": "article",
"_id": "1",
"_score": 0.7594807,
"_source": {
"title": "my elasticsearch article1",
"content": "elasticsearch is good",
"author_id": 10010
}
},
{
"_index": "website",
"_type": "article",
"_id": "2",
"_score": 0.5063205,
"_source": {
"title": "my hadoop article1",
"content": "hadoop is good",
"author_id": 10010
}
},
{
"_index": "website",
"_type": "article",
"_id": "3",
"_score": 0.5063205,
"_source": {
"title": "my elasticsearch article1",
"content": "elasticsearch is bad",
"author_id": 100
}
}
]
}
}


6.query exist(2.x中提供,后面的取消了)

GET /_search

{

  "query": {

    "exists":{

      "title":"hello"

    }

  }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  elasticsearch
相关文章推荐