我有这个查询:
GET /peopledb/person/_search
{
"from": 0,
"size": 2,
"query": {
"bool": {
"should": [
{
"match": {
"personId": "harp"
}
},
{
"match_phrase_prefix": {
"name": "43"
}
},
{
"match_phrase_prefix": {
"personAddress.city": "harp"
}
},
{
"match": {
"personAddress.street": "harp"
}
},
{
"match": {
"personAddress.streetNumber": "harp"
}
}
]
}
},
"filter": {
"bool": {
"should": [
{
"type": {
"name": "text"
}
},
{
"type": {
"personAddress.streetNumber": "long"
}
}
]
}
}
}
其中name
是文本,而personAddress.streetNumber
很长,如果我使用此查询并将“ 23”放到上面,它将起作用并找到streenNumber
在哪里,但是如果我在23把“约翰”放错了,可能是因为试图将“约翰”改成长号而失败了,所以我在这里怎么做?
我唯一想要的就是能够从客户端接收文本查询并在这些字段中进行搜索...
答案 0 :(得分:0)
您只能对“文本”类型的字段使用全文搜索。所有其他类型的字段(数字,日期等)都应放在filter子句中。
让我们假设您有一个索引peopledb
,类型为person
,具有默认映射。在peopledb
索引中,您有两个文档:
{
"personId": "wheeler",
"name": "Frank Wheeler",
"personAddress": {
"city": "New York",
"street": "Revolutionary road",
"streetNumber": 115
}
}
和
{
"personId": "harper",
"name": "John Harper",
"personAddress": {
"city": "Glasgow",
"street": "Craigiehall Street",
"streetNumber": 32
}
}
您可以轻松地在文本字段中进行全文搜索:
"query": {
"multi_match": {
"query": "frank harper",
"fields": ["name", "personAddress.city", "personAddress.street"]
}
}
此查询将从索引返回所有两个记录。但是,如果您要查找streetNumber
为115的文档,则不能仅编辑查询并将personAddress.streetNumber
包括在fields数组中,因为streetNumber
字段的类型是标量,没有太大意义。在标量字段中进行全文搜索。相反,您应该添加一个过滤子句:
"query": {
"bool": {
"must": {
"multi_match": {
"query": "frank harper",
"fields": ["name", "personAddress.city", "personAddress.street"]
}
},
"filter": {
"term": {
"personAddress.streetNumber": "115"
}
}
}
}