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": []
    }
  }
}

 

发布了56 篇原创文章 · 获赞 34 · 访问量 3万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览