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

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"
    }
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值