实现您的用例的一种方法是使用 bool 查询或 query_string
添加一个包含索引数据、映射、搜索查询和搜索结果的工作示例
索引映射:
{
"mappings": {
"properties": {
"department": {
"type": "text"
},
"fname": {
"type": "text"
},
"lname": {
"type": "text"
},
"title": {
"type": "text"
}
}
}
}
索引数据:
{
"fname": "john",
"title": "faculty",
"department": "navy"
}
{
"fname": "smith",
"title": "faculty",
"department": "navy"
}
{
"fname": "Will",
"lname": "Smith",
"title": "Student",
"department": "engineering"
}
使用bool query进行搜索查询:
{
"query": {
"bool": {
"must": [
{
"match": {
"fname": "Smith"
}
},
{
"match": {
"title": "faculty"
}
},
{
"match": {
"department": "navy"
}
}
]
}
}
}
搜索结果将是
"hits": [
{
"_index": "67550433",
"_type": "_doc",
"_id": "2",
"_score": 1.9208363,
"_source": {
"fname": "Smith",
"title": "faculty",
"department": "navy"
}
}
]
或者你甚至可以使用query_string
{
"query":{
"query_string":{
"query":"fname:smith AND title:faculty AND department:navy"
}
}
}