Elasticsearch 数据查询

Posted heqiuyong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch 数据查询相关的知识,希望对你有一定的参考价值。

一、基本查询

语法:

GET /索引库名/_search
{
  "query": {
    "查询类型": {
      "查询条件": "查询条件值"
    }
  }
}

查询类型:match_all,match,term,range,fuzzy,bool 等等

查询条件:查询条件会根据类型的不同,写法也有差异

 

1.1 查询所有(match_all)

查询指令:

GET /demo/_search
{
  "query": {
    "match_all": {}
  }
}

 

查询结果:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 1,
    "hits": [
      {
        "_index": "demo",
        "_type": "goods",
        "_id": "LoHLs2gBBdkQnU_dAr4j",
        "_score": 1,
        "_source": {
          "title": "联想笔记本",
          "price": "5299.00"
        }
      },
      {
        "_index": "demo",
        "_type": "goods",
        "_id": "MIHNs2gBBdkQnU_d_r6o",
        "_score": 1,
        "_source": {
          "title": "华为手机",
          "price": "2199.00"
        }
      },
      {
        "_index": "demo",
        "_type": "goods",
        "_id": "L4HNs2gBBdkQnU_dXL4O",
        "_score": 1,
        "_source": {
          "title": "普联路由器",
          "price": "99.00"
        }
      },
      {
        "_index": "demo",
        "_type": "goods",
        "_id": "LYHJs2gBBdkQnU_dt75n",
        "_score": 1,
        "_source": {
          "title": "小米手机",
          "price": "1699.00"
        }
      }
    ]
  }
}

 

 

1.2 匹配查询(match)

or 关系:会把查询条件进行分词,然后进行查询,多个词条之间是or的关系

查询指令:

GET /demo/_search
{
  "query": {
    "match": {
      "title": {
        "query": "华为手机",
        "operator": "or"
      }
    }
  }
}

 

查询结果:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1.6051829,
    "hits": [
      {
        "_index": "demo",
        "_type": "goods",
        "_id": "MIHNs2gBBdkQnU_d_r6o",
        "_score": 1.6051829,
        "_source": {
          "title": "华为手机",
          "price": "2199.00"
        }
      },
      {
        "_index": "demo",
        "_type": "goods",
        "_id": "LYHJs2gBBdkQnU_dt75n",
        "_score": 0.2876821,
        "_source": {
          "title": "小米手机",
          "price": "1699.00"
        }
      }
    ]
  }
}

 

and关系:会把查询条件进行分词,然后进行查询,多个词条之间是and的关系

查询指令:

GET /demo/_search
{
  "query": {
    "match": {
      "title": {
        "query": "华为手机",
        "operator": "and"
      }
    }
  }
}

 

查询结果:

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.6051829,
    "hits": [
      {
        "_index": "demo",
        "_type": "goods",
        "_id": "MIHNs2gBBdkQnU_d_r6o",
        "_score": 1.6051829,
        "_source": {
          "title": "华为手机",
          "price": "2199.00"
        }
      }
    ]
  }
}

 

 

1.3 词条查询(term)

查询指令:

GET /demo/_search
{
  "query": {
    "term": {
      "price": {
        "value": 5299.00
      }
    }
  }
}

 

查询结果:

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "demo",
        "_type": "goods",
        "_id": "LoHLs2gBBdkQnU_dAr4j",
        "_score": 1,
        "_source": {
          "title": "联想笔记本",
          "price": "5299.00"
        }
      }
    ]
  }
}

 

二、结果过滤

 默认情况下,elasticsearch在搜索的结果中,会把文档中保存在 _source 的所有字段都返回。

如果我们只想获取其中的部分字段,我们可以添加 _source 字段进行过滤

查询指令:

GET /demo/_search
{
  "query": {
    "match_all": {}
  },
  "_source": {
    "includes": ["title"],
    "excludes": ["price"]
  }
}

 

查询结果:

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 1,
    "hits": [
      {
        "_index": "demo",
        "_type": "goods",
        "_id": "LoHLs2gBBdkQnU_dAr4j",
        "_score": 1,
        "_source": {
          "title": "联想笔记本"
        }
      },
      {
        "_index": "demo",
        "_type": "goods",
        "_id": "MIHNs2gBBdkQnU_d_r6o",
        "_score": 1,
        "_source": {
          "title": "华为手机"
        }
      },
      {
        "_index": "demo",
        "_type": "goods",
        "_id": "L4HNs2gBBdkQnU_dXL4O",
        "_score": 1,
        "_source": {
          "title": "普联路由器"
        }
      },
      {
        "_index": "demo",
        "_type": "goods",
        "_id": "LYHJs2gBBdkQnU_dt75n",
        "_score": 1,
        "_source": {
          "title": "小米手机"
        }
      }
    ]
  }
}

 

以上是关于Elasticsearch 数据查询的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch笔记九之优化

使用标准库Ruby将数据标记到Elasticsearch批量中

Elasticsearch--搜索

ElasticSearch--请求体查询

小烨收藏ElasticSearch权威指南-请求体查询

ElasticSearch 学习笔记一 简介