term查询

# term查询

# term查询:查询某字段里有某个关键词的文档
GET /library/books/_search
{
  "query": {
    "term": {
        "preview": "elasticsearch"
    }
  }
}

{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.15372275,
    "hits": [
      {
        "_index": "library",
        "_type": "books",
        "_id": "2",
        "_score": 0.15372275,
        "_source": {
          "title": "The Elasticsearch cookbook",
          "price": 15,
          "preview": "One of the main requirements of today's applications is search capability. In the market, we can find a lot of solutions that answer this need, both in commercial as well as the open source world. One of the most used libraries for searching is Apache Lucene. This library is the base of a large number of search solutions such as Apache Solr, Indextank, and ElasticSearch.",
          "publish_date": "2015-05-12"
        }
      },
      {
        "_index": "library",
        "_type": "books",
        "_id": "1",
        "_score": 0.125,
        "_source": {
          "title": "Elasticsearch: The Definitive Guide",
          "price": 5,
          "preview": "Elasticsearch is a distributed, scalable, real-time search and analytics engine. It ena‐bles you to search, analyze, and explore your data, often in ways that you did not anticipate at the start of a project. It exists because raw data sitting on a hard drive is just not useful.",
          "publish_date": "2015-02-08"
        }
      },
      {
        "_index": "library",
        "_type": "books",
        "_id": "3",
        "_score": 0.033562027,
        "_source": {
          "title": "Elasticsearch Blueprints",
          "price": 9,
          "preview": "This book starts with the creation of a Google-like web search service, enabling you to generate your own search results. You will then learn how an e-commerce website can be built using Elasticsearch. We will discuss various approaches in getting relevant content up the results, such as relevancy based on how well a query matched the text, time-based recent documents, geographically nearer items, and other frequently used approaches.",
          "publish_date": "2015-06-01"
        }
      }
    ]
  }
}

[elk@node01 api]$ cat a11.pl 
##发送消息
use  LWP::UserAgent; 
use LWP;
use Encode;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Headers;
use HTTP::Response;
use Encode;
use URI::Escape;
use URI::URL;
use JSON;
use Data::Dumper;
  my $ua = LWP::UserAgent->new;
     $ua->agent("Mozilla/5.0 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/30.0");
  my $cookie_jar = HTTP::Cookies->new(
     file=>'lwp_cookies.txt',
     autosave=>1,
     ignore_discard=>1);
     $ua->cookie_jar($cookie_jar);  
     my $login_url ="http://192.168.137.2:9200/library/books/_search";  
     my $post ={
     "query" => {
        "term" => {
        "preview" => "elasticsearch"
    }
  }
};
    
    use JSON qw(encode_json);  
    $json_string = encode_json($post);  
  
    my $req = HTTP::Request->new(  
        'GET' => $login_url
    );  
    $req->content_type('application/json; charset=UTF-8')  
      ;    #post请求,如果有发送参数,必须要有这句  
    $req->content("$json_string");    #发送post的参数  
    my $res = $ua->request($req);  
    print $res->content()."
";            #获取的是响应正文  
[elk@node01 api]$ perl a11.pl 
{"took":1,"timed_out":false,
"_shards":{"total":5,"successful":5,"failed":0},
"hits":{"total":3,"max_score":0.15372275,
"hits":[{"_index":"library","_type":"books","_id":"2","_score":0.15372275,
"_source":{ "title":"The Elasticsearch cookbook","price":15,"preview":"One of the main requirements of today's applications is search capability. In the market, we can find a lot of solutions that answer this need, both in commercial as well as the open source world. One of the most used libraries for searching is Apache Lucene. This library is the base of a large number of search solutions such as Apache Solr, Indextank, and ElasticSearch.", "publish_date":"2015-05-12" }},
{"_index":"library","_type":"books","_id":"1","_score":0.125,"_source":{ "title":"Elasticsearch: The Definitive Guide","price":5,"preview":"Elasticsearch is a distributed, scalable, real-time search and analytics engine. It ena‐bles you to search, analyze, and explore your data, often in ways that you did not anticipate at the start of a project. It exists because raw data sitting on a hard drive is just not useful." ,"publish_date":"2015-02-08"}},
{"_index":"library","_type":"books","_id":"3","_score":0.033562027,"_source":{ "title":"Elasticsearch Blueprints","price":9,"preview":"This book starts with the creation of a Google-like web search service, enabling you to generate your own search results. You will then learn how an e-commerce website can be built using Elasticsearch. We will discuss various approaches in getting relevant content up the results, such as relevancy based on how well a query matched the text, time-based recent documents, geographically nearer items, and other frequently used approaches." , "publish_date":"2015-06-01"}}]}}

原文地址:https://www.cnblogs.com/hzcya1995/p/13349686.html