我在 elasticsearch 中创建了一个多字段,但它没有返回任何数据
I have created a multi field in elastisearch but it's not returning any data
我用这个
创建了一个多字段映射
PUT products/product/_mapping
{
"product": {
"properties": {
"brand": {
"type": "multi_field",
"fields": {
"brand_original": {"type": "string"},
"raw": {"type" : "string", "index" : "not_analyzed"}
}
}
}
}
}
然后我尝试查询但没有得到任何结果:
GET products/product/_search
{
"_source": [ "brand.raw"],
"query" : {
"match" : {
"brand.raw" : "clinique"
}
}
}
这是结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
我错过了什么吗?可以重新编辑映射吗?
试试这个:
我创建了一个这样的索引:
DELETE /test_index
PUT /test_index
PUT /test_index/product/_mapping
{
"product": {
"properties": {
"brand": {
"type": "string",
"index": "analyzed",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
并添加了几个文档:
POST /test_index/product/_bulk
{"index":{"_id":1}}
{"brand":"first/brand"}
{"index":{"_id":2}}
{"brand":"second/brand"}
现在,如果我像这样使用 "match"
进行查询,我将取回两个文档
POST /test_index/_search
{
"query": {
"match": {
"brand": "first/brand"
}
}
}
...
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2712221,
"hits": [
{
"_index": "test_index",
"_type": "product",
"_id": "1",
"_score": 0.2712221,
"_source": {
"brand": "first/brand"
}
},
{
"_index": "test_index",
"_type": "product",
"_id": "2",
"_score": 0.028130025,
"_source": {
"brand": "second/brand"
}
}
]
}
}
但是如果我针对 "brand.raw"
进行查询,我只会取回完全匹配的文档:
POST /test_index/_search
{
"query": {
"match": {
"brand.raw": "first/brand"
}
}
}
...
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
"_index": "test_index",
"_type": "product",
"_id": "1",
"_score": 0.30685282,
"_source": {
"brand": "first/brand"
}
}
]
}
}
这是我用来测试它的代码:
http://sense.qbox.io/gist/d2cee47ccff751955235e1efae7fce1b224dd35c
我用这个
创建了一个多字段映射PUT products/product/_mapping
{
"product": {
"properties": {
"brand": {
"type": "multi_field",
"fields": {
"brand_original": {"type": "string"},
"raw": {"type" : "string", "index" : "not_analyzed"}
}
}
}
}
}
然后我尝试查询但没有得到任何结果:
GET products/product/_search
{
"_source": [ "brand.raw"],
"query" : {
"match" : {
"brand.raw" : "clinique"
}
}
}
这是结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
我错过了什么吗?可以重新编辑映射吗?
试试这个:
我创建了一个这样的索引:
DELETE /test_index
PUT /test_index
PUT /test_index/product/_mapping
{
"product": {
"properties": {
"brand": {
"type": "string",
"index": "analyzed",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
并添加了几个文档:
POST /test_index/product/_bulk
{"index":{"_id":1}}
{"brand":"first/brand"}
{"index":{"_id":2}}
{"brand":"second/brand"}
现在,如果我像这样使用 "match"
进行查询,我将取回两个文档
POST /test_index/_search
{
"query": {
"match": {
"brand": "first/brand"
}
}
}
...
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2712221,
"hits": [
{
"_index": "test_index",
"_type": "product",
"_id": "1",
"_score": 0.2712221,
"_source": {
"brand": "first/brand"
}
},
{
"_index": "test_index",
"_type": "product",
"_id": "2",
"_score": 0.028130025,
"_source": {
"brand": "second/brand"
}
}
]
}
}
但是如果我针对 "brand.raw"
进行查询,我只会取回完全匹配的文档:
POST /test_index/_search
{
"query": {
"match": {
"brand.raw": "first/brand"
}
}
}
...
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
"_index": "test_index",
"_type": "product",
"_id": "1",
"_score": 0.30685282,
"_source": {
"brand": "first/brand"
}
}
]
}
}
这是我用来测试它的代码:
http://sense.qbox.io/gist/d2cee47ccff751955235e1efae7fce1b224dd35c