码迷,mamicode.com
首页 > 其他好文 > 详细

Elasticsearch 数据查询

时间:2019-02-03 23:46:06      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:mat   span   之间   fail   数据查询   部分   max   分词   rds   

一、基本查询

语法:

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 数据查询

标签:mat   span   之间   fail   数据查询   部分   max   分词   rds   

原文地址:https://www.cnblogs.com/heqiuyong/p/10351176.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!