让我们看看查看我的所有步骤是否有助于您找出问题所在。
如果存在则先删除索引,重新开始:
curl -XDELETE "http://localhost:9200/test_index"
然后创建它:
curl -XPUT "http://localhost:9200/test_index/"
然后为“用户”创建映射:
curl -XPUT "http://localhost:9200/test_index/users/_mapping" -d'
{
"users":
{
"_all" : {"enabled" : false},
"properties":
{
"user":
{
"type":"multi_field",
"fields":
{
"user":{"type":"string"},
"original":{"type":"string","index":"not_analyzed"}
}
},
"school":{"type":"string","index": "not_analyzed"},
"college":{"type":"string","index":"not_analyzed"},
"uni":{"type":"string","index":"not_analyzed"}
}
}
}'
并检查以确保它有效:
curl -XGET "http://localhost:9200/test_index/users/_mapping"
...
{
"users": {
"_all": {
"enabled": false
},
"properties": {
"college": {
"type": "string",
"index": "not_analyzed",
"omit_norms": true,
"index_options": "docs"
},
"school": {
"type": "string",
"index": "not_analyzed",
"omit_norms": true,
"index_options": "docs"
},
"uni": {
"type": "string",
"index": "not_analyzed",
"omit_norms": true,
"index_options": "docs"
},
"user": {
"type": "multi_field",
"fields": {
"user": {
"type": "string"
},
"original": {
"type": "string",
"index": "not_analyzed",
"omit_norms": true,
"index_options": "docs",
"include_in_all": false
}
}
}
}
}
}
然后添加文档:
curl -XPUT "http://localhost:9200/test_index/users/1" -d'
{
"user": "Abdul Jabbar",
"school": "Aisha Bawany Academy",
"college": "Pakistan Air Force",
"uni": "Aptech Computer Education"
}'
curl -XPUT "http://localhost:9200/test_index/users/2" -d'
{
"user": "Abdul Jabbar WebBestow",
"school": "Sadaya Academy",
"college": "Pakistan Air Force",
"uni": "WebBestow"
}'
curl -XPUT "http://localhost:9200/test_index/users/3" -d'
{
"user":"Abdul Jabbar Leopard",
"school":"Innocent Public School",
"college":"Lucene College of Science",
"uni":"IBM"
}'
curl -XPUT "http://localhost:9200/test_index/users/4" -d'
{
"user":"Abdul Jabbar Loharwada",
"school":"Aisha Bawany Academy",
"college":"Behria College",
"uni":"Karachi University"
}'
curl -XPUT "http://localhost:9200/test_index/users/5" -d'
{
"user":"Abdul Raheem Loharwada",
"school":"Aisha Bawany Academy",
"college":"Indus College",
"uni":"Preston University"
}'
运行查询:
curl -XPOST "http://localhost:9200/test_index/_search" -d'
{
"query": {
"bool": {
"must": [
{
"prefix": {
"user": "jabb"
}
}
],
"should": [
{
"multi_match": {
"query": "Aisha Bawany Academy",
"fields": ["school", "college", "uni"]
}
},
{
"multi_match": {
"query": "Pakistan Air Force",
"fields": ["school", "college", "uni"]
}
},
{
"multi_match": {
"query": "Aptech Computer Education",
"fields": ["school", "college", "uni"]
}
}
]
}
}
}'
结果是:
{
"took": 27,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1.5784466,
"hits": [
{
"_index": "test_index",
"_type": "users",
"_id": "1",
"_score": 1.5784466,
"_source": {
"user": "Abdul Jabbar",
"school": "Aisha Bawany Academy",
"college": "Pakistan Air Force",
"uni": "Aptech Computer Education"
}
},
{
"_index": "test_index",
"_type": "users",
"_id": "2",
"_score": 0.32274455,
"_source": {
"user": "Abdul Jabbar WebBestow",
"school": "Sadaya Academy",
"college": "Pakistan Air Force",
"uni": "WebBestow"
}
},
{
"_index": "test_index",
"_type": "users",
"_id": "4",
"_score": 0.32274455,
"_source": {
"user": "Abdul Jabbar Loharwada",
"school": "Aisha Bawany Academy",
"college": "Behria College",
"uni": "Karachi University"
}
},
{
"_index": "test_index",
"_type": "users",
"_id": "3",
"_score": 0.06631388,
"_source": {
"user": "Abdul Jabbar Leopard",
"school": "Innocent Public School",
"college": "Lucene College of Science",
"uni": "IBM"
}
}
]
}
}