如何更好的学习Elasticsearch DSL

2024-07-11 11:53:35 浏览数 (1)

一、结构化查询

结构化查询样例数据

代码语言:txt复制
DELETE users;
GET users/_mapping
PUT users/_bulk
{ "index": { "_id": "1" } }
{ "name": "Alice", "age": 25, "status": "active", "description": "Alice loves hiking and outdoor activities." }
{ "index": { "_id": "2" } }
{ "name": "Bob", "age": 30, "status": "inactive", "description": "Bob is a software engineer who enjoys coding." }
{ "index": { "_id": "3" } }
{ "name": "Charlie", "age": 35, "status": "pending", "description": "Charlie is a photographer and loves to travel." }
{ "index": { "_id": "4" } }
{ "name": "David", "age": 40, "status": "active", "description": "David is a data scientist with a passion for machine learning." }
{ "index": { "_id": "5" } }
{ "name": "Eve", "age": 45, "status": "inactive", "description": "Eve is a project manager who enjoys reading." }

1、term查询

代码语言:markdown复制
GET users/_search
{
  "query": {
    "term": {
      "description": {
        "value": "loves hiking and outdoor"
      }
    }
  }
}

2、terms查询

代码语言:txt复制
GET users/_search
{
  "query": {
    "terms":{
      "status":["active","inactive"]
    }
  }
}

3、range 查询

代码语言:txt复制
GET users/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 20,
        "lte": 250
      }
    }
  }
}

4、exist

代码语言:txt复制
GET users/_search
{
  "query": {
    "exists": {
      "field": "description1"
    }
  }
}

5、prefix

代码语言:txt复制
GET users/_search
{
  "query": {
    "prefix": {
      "status": {
        "value": "act"
      }
    }
  }
}

6、wildcard

代码语言:txt复制
GET users/_search
{
  "query": {
    "wildcard": {
      "status": {
        "value": "inact*"
      }
    }
  }
}

7、 regexp

代码语言:txt复制
GET users/_search
{
  "query": {
    "regexp": {
      "description": "**love.*"
    }
  }
}

8、fuzzy

代码语言:txt复制
GET users/_search
{
  "query": {
    "fuzzy": {
      "name": {
        "value": "davd"
      }
    }
  }
}

9、匹配特定id

代码语言:txt复制
GET users/_search
{
  "query": {
    "ids": {
      "values": ["1","4"]
    }
  }
}

10、用于filter查询

代码语言:txt复制
GET users/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "status": "active"
        }
      },
      "boost": 1.5
    }
  }
}
代码语言:txt复制
GET users/_search
{
  "query": {
    "term": {
      "status": "active"
    }
  }
}
代码语言:txt复制
GET users/_search
{
  "query": {
    "match": {
      "status": "active"
    }
  }
}

二、全文查询

代码语言:txt复制
POST /books/_bulk
{ "index": { "_index": "books", "_id": "1" } }
{ "title": "Elasticsearch Guide", "description": "A comprehensive guide to Elasticsearch, the powerful search engine." }
{ "index": { "_index": "books", "_id": "2" } }
{ "title": "Learning Elasticsearch", "description": "Learn how to use Elasticsearch for search and data analysis." }
{ "index": { "_index": "books", "_id": "3" } }
{ "title": "Elasticsearch in Action", "description": "A practical book on how to implement Elasticsearch in real-world scenarios." }
{ "index": { "_index": "books", "_id": "4" } }
{ "title": "Mastering Elasticsearch", "description": "Advanced techniques and best practices for Elasticsearch." }
{ "index": { "_index": "books", "_id": "5" } }
{ "title": "Elasticsearch Essentials", "description": "Essential knowledge for getting started with Elasticsearch." }

1、match query

代码语言:txt复制
GET /books/_search
{
  "query": {
    "match": {
      "title": "Learning Elasticsearch"
    }
  }
}
  • 因为match查询是分词后查询,默认是或的关系,所以会把learning或elasticsearch相关联的文档查出来。
  • 可以加上operator:"and",查询他们且的关系。
代码语言:txt复制
GET /books/_search
{
  "query": {
    "match": {
      "title": {
        "query": "Learning Elasticsearch",
        "operator": "and"
      }
    }
  }
}

2、match phrase

代码语言:txt复制
GET /books/_search
{
  "query": {
    "match_phrase": {
      "title": "Learning Elasticsearch"
    }
  }
}

3、multi match query

代码语言:txt复制
GET /books/_search
{
  "query": {
    "multi_match": {
      "query": "Elasticsearch",
      "fields": ["title","description"]
    }
  }
}

4、 query string query

代码语言:txt复制
GET /books/_search
{
  "query": {
    "query_string": {
      "query":  "guide OR learning" ,
      "fields": ["title", "description"]
    }
  }
}

5、simple query string query

代码语言:txt复制
GET /books/_search
{
  "query": {
    "simple_query_string": {
      "query": "guide  learning",
      "fields": ["title", "description"]
    }
  }
}

6、 common terms query

代码语言:txt复制
GET /books/_search
{
  "query": {
    "common": {
      "description": {
        "query":"Elasticsearch guide",
        "cutoff_frequency": 0.001
      }
    }
  }
}

7、 match_all

代码语言:txt复制
GET /books/_search
{
  "query": {
    "match_all": {}
  }
}

0 人点赞