es基本用法(五)——API

微信公众号:Feature社区

查询所有文档

GET luoyunlong/user/_search

结果

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "pjz",
          "address": "武汉"
        }
      },
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "lyl",
          "address": "武汉"
        }
      },
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "zg",
          "address": "河南"
        }
      }
    ]
  }
}

找到三个用户:pjz、lyl、zg

 

查询address为武汉的文档

GET luoyunlong/user/_search
{
  "query":{
    "match": {
      "address": "武汉"
    }
  }
}
{
  "took": 13,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "name": "pjz",
          "address": "武汉"
        }
      },
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "name": "lyl",
          "address": "武汉"
        }
      }
    ]
  }
}

查询name为lyl的文档

GET luoyunlong/user/_search
{
  "query":{
    "match": {
      "name": "lyl"
    }
  }
}
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "name": "lyl",
          "address": "武汉"
        }
      }
    ]
  }
}

 

复杂检索

查询address为武汉的

GET luoyunlong/user/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match": {
            "address": "武汉"
          }
        }
      ]
    }
  }
}
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "name": "pjz",
          "address": "武汉"
        }
      },
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "name": "lyl",
          "address": "武汉"
        }
      }
    ]
  }
}

查询address为武汉但是name不为pjz的文档

GET luoyunlong/user/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match": {
            "address": "武汉"
          }
        }
      ],
      "must_not": [
        {"match": {
          "name": "pjz"
        }}
      ]
    }
  }
}
{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "name": "lyl",
          "address": "武汉"
        }
      }
    ]
  }
}

es短语搜索

使用match_all来显示所有文档

GET luoyunlong/user/_search
{
  "query": {
   "match_all": {}
  }
}
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "pjz",
          "address": "武汉"
        }
      },
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "lyl",
          "address": "武汉"
        }
      },
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "zg",
          "address": "河南"
        }
      }
    ]
  }
}

模糊查询

GET luoyunlong/user/_search
{
  "query": {
   "match_phrase": {
     "address": "武"
   }
  }
}
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "name": "pjz",
          "address": "武汉"
        }
      },
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "name": "lyl",
          "address": "武汉"
        }
      }
    ]
  }
}

关键词

 

GET luoyunlong/user/_search
{
  "query": {
   "match_phrase": {
     "address": "武"
   }
  },"highlight": {
    "fields": {
      "address": {}
    }
  }
}
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "name": "pjz",
          "address": "武汉"
        },
        "highlight": {
          "address": [
            "<em>武</em>汉"
          ]
        }
      },
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "name": "lyl",
          "address": "武汉"
        },
        "highlight": {
          "address": [
            "<em>武</em>汉"
          ]
        }
      }
    ]
  }
}

把<em>换成其他标签

GET luoyunlong/user/_search
{
  "query": {
   "match_phrase": {
     "address": "武"
   }
  },"highlight": {
    "fields": {
      "address": {}
    },
    "pre_tags" : ["<color>"],
    "post_tags" : ["</color>"]
  }
}
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "name": "pjz",
          "address": "武汉"
        },
        "highlight": {
          "address": [
            "<color>武</color>汉"
          ]
        }
      },
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "name": "lyl",
          "address": "武汉"
        },
        "highlight": {
          "address": [
            "<color>武</color>汉"
          ]
        }
      }
    ]
  }
}

分析(固定写法)

GET luoyunlong/user/_search
{
  "aggs": {
    "all_interests": {
      "terms": {"field": "interests"}
    }
  }
}
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "pjz",
          "address": "武汉"
        }
      },
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "lyl",
          "address": "武汉"
        }
      },
      {
        "_index": "luoyunlong",
        "_type": "user",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "zg",
          "address": "河南"
        }
      }
    ]
  },
  "aggregations": {
    "all_interests": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": []
    }
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值