elasticsearch笔记一

 

插入:

PUT test1/_doc/1
{
  "name": "张三",
  "age": 18
}

更新:

POST test1/_doc/1/_update
{
  "doc": {
    "name": "张三1"
  }
}

删除:

DELETE test1/_doc/1

匹配查询:

GET test1/_doc/_search
{
  "query":{
    "match":{
      "name":"赵"
    }
  }
}

查询部分内容:

GET test1/_doc/_search
{
  "query":{
    "match":{
      "name":"赵"
    }
  },
  "_source": ["name", "age"]
}

查询排序:

GET test1/_doc/_search
{
  "query":{
    "match":{
      "name":"赵"
    }
  },
  "sort": {
    "age": "desc"
  }
}

多字段排序:

GET test1/_doc/_search
{
  "query":{
    "match":{
      "name":"赵"
    }
  },
  "sort": {
    "age": "asc",
    "_id": "asc"
  }
}

分页查询:

GET test1/_doc/_search
{
  "query":{
    "match":{
      "name":"赵"
    }
  },
  "sort": {
    "age": "asc",
    "_id": "asc"
  },
  "from": 0,
  "size": 2
}

复合and查询:

GET test1/_doc/_search
{
  "query":{
    "bool":{
      "must":[
        {
          "match":{
            "name": "赵"
          }
        },
        {
          "match":{
            "age": 28
          }
        }
      ]
    }
  }
}

复合查询不等于:

GET test1/_doc/_search
{
  "query":{
    "bool":{
      "must_not":[
        {
          "match":{
            "name": "赵"
          }
        },
        {
          "match":{
            "age": 28
          }
        }
      ]
    }
  }
}

复合or查询:

GET test1/_doc/_search
{
  "query":{
    "bool":{
      "should":[
        {
          "match":{
            "name": "赵"
          }
        },
        {
          "match":{
            "age": 18
          }
        }
      ]
    }
  }
}

复合查询and和or嵌套:

GET test1/_doc/_search
{
  "query":{
    "bool":{
      "must":[
        {
          "match":{
            "name": "赵"
          }
        },
        {
          "bool":{
            "should":[
              {
                "match": {
                  "age": 28
                }
              },
              {
                "match":{
                  "desc": "ok"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

查询过滤:

GET test1/_doc/_search
{
  "query":{
    "bool":{
      "must":[
        {
          "match":{
            "name": "赵"
          }
        }
      ],
      "filter":{
        "range":{
          "age":{
            "gte": 20,
            "lte": 28
          }
        }
      }
    }
  }
}

空格匹配多值:

GET test1/_doc/_search
{
  "query":{
    "bool":{
      "must":[
        {
          "match":{
            "name": "赵 王"
          }
        }
      ]
    }
  }
}

精确查询:

GET test1/_doc/_search
{
  "query":{
    "term":{
      "name": "张三1"
    }
  }
}

复合精确查询:

GET test1/_doc/_search
{
  "query":{
    "bool":{
      "should":[
        {
          "term":{
            "name":"张三"
          }
        },
        {
          "term":{
            "name": "王"
          }
        }
      ]
    }
  }
}

注:term里面的词不会被拆分,而存储的因使用了倒排查询所以会拆开,所以可能查询不到,除非自己设置了分词

高亮显示:

GET test1/_doc/_search
{
  "query":{
    "bool":{
      "should":[
        {
          "term":{
            "name":"张三"
          }
        },
        {
          "term":{
            "name": "王"
          }
        }
      ]
    }
  },
  "highlight":{
    "fields":{
      "name":{
        
      }
    }
  }
}

自定义高亮:

GET test1/_doc/_search
{
  "query":{
    "bool":{
      "should":[
        {
          "term":{
            "name":"张三"
          }
        },
        {
          "term":{
            "name": "王"
          }
        }
      ]
    }
  },
  "highlight":{
    "pre_tags": "<p>",
    "post_tags": "</p>",
    "fields":{
      "name":{
        
      }
    }
  }
}
posted @ 2023-03-11 18:54  此时不卷何时卷  阅读(25)  评论(0)    收藏  举报