程序员人生 网站导航

_souce禁用,如何搜索? 指定_souce存储的字段?控制从_source返回部分字段?

栏目:互联网时间:2017-01-13 11:06:20

事实上,_source 字段就是1个贮存字段。
在 Elasticsearch 中,单独设置贮存字段不是1个好做法。完全的文档已被保存在 _source 字段中。
通常最好的办法会是使用 _source 参数来过滤你需要的字段。

_source禁用,如何搜索到字段值?

必须设置所有字段:store:true
如果mapping中,对某个type,设置了 “_source”: {“enabled”: false},那末查询结果中就没有_source字段只能查到文档id。

如果mapping中,field设置了store为true,例如 “age”:{“type”:”integer”,”store”:true},那末就能够在查询时通过指定fields获得到指定的field的数据。

store默许false。

fielddata_fields 和 fields区分?
fielddata_fields同时还返回_source, fields不返回_source。

  • field data fields(fielddata_fields) can work on fields that are not stored(from _source),will cause the terms for that field to be loaded to memory (cached),
  • fields parameter(fields) is about fields that are explicitly marked as stored in the mapping, which is off by default and generally not recommended.

https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-request-fielddata-fields.html
https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-request-fields.html

REST:
https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-request-fields.html

{
    "fields" : ["user", "postDate"],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

Java API

SearchRequestBuilder类
方法:addFields(String... fields)
或:addField(String field) 

建索引时,指定_souce存储的字段?

默许存储所有字段

PUT logs
{
  "mappings": {
    "event": {
      "_source": {
        "includes": [
          "*.count",
          "meta.*"
        ],
        "excludes": [
          "meta.description",
          "meta.other.*"
        ]
      }
    }
  }
}

控制从_source返回部份字段

GET /_search
{
    "query":   { "match_all": {}},
    "_source": [ "title", "created" ]
}
//支持通配符
{
    "_source": "obj.*",
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

{
    "_source": [ "obj1.*", "obj2.*" ],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

{
    "_source": {
        "include": [ "obj1.*", "obj2.*" ],
        "exclude": [ "*.description" ]
    },
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
------分隔线----------------------------
------分隔线----------------------------

最新技术推荐