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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM