elasticsearch返回指定字段

1. postman 请求elasticsearch 返回指定字段

  1.直接在请求体当中,json 数据,对应的是一个列表

{

  "_source":['title','id','desc'],
  "from":10,
  "size":100,

}

   至于from和size是浅分页

    2. 或者这样

{
    "_source":{
        "includes":["title","url","id"],
        "excludes":["desc"]
    }
}

  其中includes代表需要返回的字段,excludes代表不要返回的字段

  3.直接在请求url带上需要查询参数

curl -XGET 'localhost:9200/_search?pretty&filter_path=took,hits.hits._id,hits.hits._score'
{
  "took" : 3,
  "hits" : {
    "hits" : [
      {
        "_id" : "3640",
        "_score" : 1.0
      },
      {
        "_id" : "3642",
        "_score" : 1.0
      }
    ]
  }
}

  对_source的字段进行过滤

curl -XGET 'localhost:9200/_search?pretty&filter_path=hits.hits._source&_source=title'
{
  "hits" : {
    "hits" : [ {
      "_source":{"title":"Book #2"}
    }, {
      "_source":{"title":"Book #1"}
    }, {
      "_source":{"title":"Book #3"}
    } ]
  }
}

   这样也可以

_search?_source=goodsId,uri

_search?fields=goodsId,uri

 

2.python 对接elassearch ,指定返回字段

  1.实例化的es客户端,然后调用search方法,传入参数,params


from elasticsearch import Elasticsearch
es=Elasticseach(xxxx) es.search(params={"_source":"title,id,desc,url"})

   注:这边调用的是包中的search方法,和postman不一样的是,_source的值是一个z字符串,不同字段用逗号隔开,而post满是一个列表

   2.也是调用Elasticsearch的search方法,传入参数不是param,而直接是_source字段

pprint(es.search(index='person', body={"query": {"match": {"age": "19"}}}, _source=['name']))

  结果:

{'_shards': {'failed': 0, 'skipped': 0, 'successful': 1, 'total': 1},
 'hits': {'hits': [{'_id': 'xFznIXIBMTX0DMkCyicV',
                    '_index': 'person',
                    '_score': 1.0,
                    '_source': {'name': 'lisi'},
                    '_type': 'male'}],
          'max_score': 1.0,
          'total': {'relation': 'eq', 'value': 1}},
 'timed_out': False,
 'took': 1}

 

 

3.不懂直接查询api接口

  https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html

 

4.返回文档版本字段

GET /_search
{
    "version": true,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

 

5.Script Field 用脚本来对命中的每个文档的字段进行运算后返回

GET /bank/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "test1": {
      "script": {
        "lang": "painless",
        "source": "doc['balance'].value * 2"
      }
    },
    "test2": {
      "script": {
        "lang": "painless",
        <!--  doc指文档-->
        "source": "doc['age'].value * params.factor",
        "params": {
          "factor": 2
        }
      }
    } }}

  搜索结果:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": 1,
    "hits": [
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "25",
        "_score": 1,
        "fields": {
          "test1": [
          ],
          "test2": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "44",
        "_score": 1,
        "fields": {
          "test1": [
          ],
          "test2": [
          ]
        }
      }
      }
    ]
  }
}

  示例2

 
GET /bank/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "ffx": {
      "script": {
        "lang": "painless",
        "source": "doc['age'].value * doc['balance'].value"
      }
    },
    "balance*2": {
      "script": {
        "lang": "painless",
        "source": "params['_source'].balance*2"
      }
    }
  }
}

  搜索结果:

{
  "took": 26,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": 1,
    "hits": [
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "25",
        "_score": 1,
        "fields": {
          "balance*2": [
          ],
          "ffx": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "44",
        "_score": 1,
        "fields": {
          "balance*2": [
          ],
          "ffx": [
          ]
        }
      },

      }
    ]
  }
}

  

  说明: params  _source 取 _source字段值

  官方推荐使用doc,理由是用doc效率比取_source 高

 

 

参考:https://zyc88.blog.csdn.net/article/details/91463409

posted @ 2020-05-15 23:46  阿布_alone  阅读(34209)  评论(0编辑  收藏  举报
TOP