以下是一个简单的Elasticsearch查询示例:

GET /my_index/_search
{
  "query": {
    "match": {
      "title": "Elasticsearch"
    }
  }
}

该查询将在名为my_index的索引中搜索所有具有title字段匹配Elasticsearch的文档。

以下是一个更复杂的Elasticsearch查询示例:

GET /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "Elasticsearch"
          }
        },
        {
          "range": {
            "date": {
              "gte": "2019-01-01",
              "lte": "2020-01-01"
            }
          }
        }
      ],
      "filter": {
        "term": {
          "category": "Technology"
        }
      },
      "should": [
        {
          "match": {
            "description": "search engine"
          }
        },
        {
          "match": {
            "description": "big data"
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "sort": [
    {
      "date": {
        "order": "desc"
      }
    }
  ]
}

此查询使用布尔查询来组合多个条件。使用“must”子句来指定必须匹配的条件,使用“filter”子句来指定必须过滤的条件,使用“should”子句来指定可选条件,使用“minimum_should_match”参数来指定必须满足的最小条件数。此外,该查询使用sort参数按date字段排序结果。

Elasticsearch查询代码

原文地址: https://www.cveoy.top/t/topic/bnuf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录