一.查询关键字

1.terms、term查询

term query会去倒排索引中寻找确切的term,它并不知道分词器的存在,这种查询适合keyword、numeric、date等明确值的

  1. GET test_index1/_search
  2. {
  3. "query": {
  4. "term": {
  5. "name": "zhangsan"
  6. }
  7. }
  8. }

2.match查询

match query 知道分词器的存在,会对field进行分词操作,然后再查询

  1. GET test_index1/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": "my ss"
  6. }
  7. }
  8. }

match和term区别可以理解为term是精确查询,这边match模糊查询;
1.match会对my ss分词为两个单词
2.term对认为这是一个单词

3.In匹配查询

  1. GET test_index1/_search
  2. {
  3. "query":{
  4. "terms":{
  5. "name":["张三","王五"]
  6. }
  7. }
  8. }

4.多条件查询语法

bool 过滤
bool 过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含一下操作符:

  • must 多个查询条件的完全匹配,相当于 and。
  • must_not 多个查询条件的相反匹配,相当于 not。
  • should 至少有一个查询条件匹配, 相当于 or。

    and查询 name=zhangsan,并且describe=xiangxixinxi

    1. GET test_index1/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "must": [
    6. {
    7. "term": {
    8. "name": "zhangsan"
    9. }
    10. },
    11. {
    12. "term": {
    13. "describe": "xiangxixinxi"
    14. }
    15. }
    16. ]
    17. }
    18. }
    19. }

    or查询 name=zhangsan,或describe=xiangxixinxi1

    1. GET test_index1/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "should": [
    6. {
    7. "term": {
    8. "name": "zhangsan"
    9. }
    10. },
    11. {
    12. "term": {
    13. "describe": "xiangxixinxi1"
    14. }
    15. }
    16. ]
    17. }
    18. }
    19. }

    5.boost权重查询

    es查询无指定排序情况下,分值越大越靠前
    1. GET test_index1/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "should": [
    6. {
    7. "term": {
    8. "name": {
    9. "term": "zhangsan",
    10. "boost": 1
    11. }
    12. }
    13. },
    14. {
    15. "term": {
    16. "name": {
    17. "term": "lisi",
    18. "boost": 2
    19. }
    20. }
    21. }
    22. ]
    23. }
    24. }
    25. }
    name=lisi的条件设置boost=2,权重是条件里最大的,匹配name=lisi的分数会乘以2,排在第一位