使用match_phrase组合过滤器-Elastic 1.7 [英] combine filters using match_phrase - Elastic 1.7

查看:112
本文介绍了使用match_phrase组合过滤器-Elastic 1.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ES 1.7,尝试使用match_phrase查找文档以使用完全匹配的字符串进行搜索.单独使用时,过滤器可以正常工作,但是当我组合使用过滤器时,会出现错误.

Im using ES 1.7, trying to look for documents using match_phrase to search using exact match string. The filter works fine when used alone, however when I combine the filters, I get an error.

示例:人员文档 q=aaron&address=scarborough-通过姓名和地址搜索人员,效果很好.

example: people document q=aaron&address=scarborough - searching a person by name and address, works fine.

{
  "query": {
    "match_phrase": {
      "name": "aaron"
    }
  },
  "filter": {
    "bool": {
      "must": {
        "nested": {
          "path": "addresses",
          "query": {
            "match_phrase": {
              "address": "scarborough"
            }
          }
        }
      }
    }
  },

q=aaron&phone=813-689-6889-通过姓名和电话号码搜索人也可以正常工作.

q=aaron&phone=813-689-6889 - searching a person by name and phone number works fine as well.

{
  "query": {
    "match_phrase": {
      "name": "aaron"
    }
  },
  "filter": {
    "bool": {
      "must": {
        "query": {
          "match_phrase": {
            "phone": "813-689-6889"
          }
        }
      }
    }
  }

但是,当我尝试同时使用过滤器,地址和电话时,出现No filter registered for [match_phrase]错误

However, When I try to use both filters, address and phone I get a No filter registered for [match_phrase] error

例如:q=aaron&address=scarborough&phone=813-689-6889

{
  "query": {
    "match_phrase": {
      "name": "aaron"
    }
  },
  "filter": {
    "bool": {
      "must": {
        "nested": {
          "path": "addresses",
          "query": {
            "match_phrase": {
              "address": "scarborough"
            }
          }
        },
        "query": {
          "match_phrase": {
            "phone": "813-689-6889"
          }
        }
      }
    }
  }

同时使用addressphone过滤器时的错误:

the error, when using address and phone filters together:

nested: QueryParsingException[[pl_people] No filter registered for [match_phrase]]; }]","status":400}):

索引映射(人):

{
  "pl_people": {
    "mappings": {
      "person": {
        "properties": {
          "ac_name": {
            "type": "string",
            "analyzer": "autocomplete"
          },
          "addresses": {
            "type": "nested",
            "properties": {
              "address": {
                "type": "string"
              },
              "city": {
                "type": "string",
                "index": "not_analyzed"
              },
              "city_id": {
                "type": "long"
              },
              "country": {
                "type": "string",
                "index": "not_analyzed"
              },
              "county": {
                "type": "string",
                "index": "not_analyzed"
              },
              "county_id": {
                "type": "long"
              },
              "id": {
                "type": "long"
              },
              "location": {
                "type": "geo_point"
              },
              "parameterized": {
                "type": "string"
              },
              "state": {
                "type": "string",
                "index": "not_analyzed"
              },
              "state_id": {
                "type": "long"
              },
              "zip": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "author": {
            "type": "string",
            "index": "not_analyzed"
          },
          "body": {
            "type": "string",
            "analyzer": "remove_html",
            "fields": {
              "ns_body": {
                "type": "string",
                "analyzer": "remove_html_stopwords"
              }
            }
          },
          "charities": {
            "type": "nested",
            "properties": {
              "email": {
                "type": "string",
                "index": "not_analyzed"
              },
              "id": {
                "type": "long"
              }
            }
          },
          "community": {
            "properties": {
              "name": {
                "type": "string",
                "index": "not_analyzed"
              },
              "parameterized": {
                "type": "string",
                "index": "not_analyzed"
              },
              "slug": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "created_at": {
            "type": "date",
            "format": "dateOptionalTime"
          },
          "date_of_birth": {
            "type": "date",
            "format": "dateOptionalTime"
          },
          "delimiters": {
            "type": "nested",
            "properties": {
              "delimiter_type": {
                "type": "string",
                "index": "not_analyzed"
              },
              "id": {
                "type": "long"
              }
            }
          },
          "description": {
            "type": "string"
          },
          "employments": {
            "type": "nested",
            "properties": {
              "email": {
                "type": "string",
                "index": "not_analyzed"
              },
              "employment_status": {
                "type": "string",
                "index": "not_analyzed"
              },
              "foia_contact": {
                "type": "boolean"
              },
              "id": {
                "type": "long"
              },
              "phone": {
                "type": "string",
                "index": "not_analyzed"
              },
              "phone_extension": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "first_name": {
            "type": "string",
            "fields": {
              "na_first_name": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "last_name": {
            "type": "string",
            "fields": {
              "na_last_name": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "market": {
            "properties": {
              "name": {
                "type": "string",
                "index": "not_analyzed"
              },
              "parameterized": {
                "type": "string",
                "index": "not_analyzed"
              },
              "slug": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "middle_name": {
            "type": "string",
            "fields": {
              "na_middle_name": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "most_recent_organization": {
            "properties": {
              "description": {
                "type": "string"
              },
              "id": {
                "type": "long"
              },
              "name": {
                "type": "string"
              },
              "parameterized": {
                "type": "string"
              },
              "phone": {
                "type": "string"
              }
            }
          },
          "name": {
            "type": "string",
            "fields": {
              "na_name": {
                "type": "string",
                "index": "not_analyzed"
              },
              "ngram_name": {
                "type": "string",
                "analyzer": "my_start"
              },
              "ns_name": {
                "type": "string",
                "analyzer": "no_stopwords"
              }
            }
          },
          "organizations": {
            "properties": {
              "name": {
                "type": "string"
              },
              "parameterized": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "package": {
            "properties": {
              "name": {
                "type": "string",
                "index": "not_analyzed"
              },
              "parameterized": {
                "type": "string",
                "index": "not_analyzed"
              },
              "slug": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "parameterized": {
            "type": "string",
            "index": "not_analyzed"
          },
          "phone": {
            "type": "string"
          },
          "photo": {
            "properties": {
              "large": {
                "type": "string"
              },
              "medium": {
                "type": "string"
              },
              "teaser": {
                "type": "string"
              },
              "thumb": {
                "type": "string"
              },
              "url": {
                "type": "string"
              }
            }
          },
          "projects": {
            "properties": {
              "id": {
                "type": "long"
              },
              "name": {
                "type": "string",
                "index": "not_analyzed"
              },
              "parameterized": {
                "type": "string",
                "index": "not_analyzed"
              },
              "slug": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "public_offices": {
            "type": "nested",
            "properties": {
              "email": {
                "type": "string",
                "index": "not_analyzed"
              },
              "employment_status": {
                "type": "string",
                "index": "not_analyzed"
              },
              "id": {
                "type": "long"
              }
            }
          },
          "published": {
            "type": "string",
            "index": "not_analyzed"
          },
          "region": {
            "properties": {
              "name": {
                "type": "string",
                "index": "not_analyzed"
              },
              "parameterized": {
                "type": "string",
                "index": "not_analyzed"
              },
              "slug": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "resource": {
            "type": "string"
          },
          "short_description": {
            "type": "string"
          },
          "show_path": {
            "type": "string"
          },
          "time": {
            "type": "date",
            "format": "dateOptionalTime"
          },
          "updated_at": {
            "type": "date",
            "format": "dateOptionalTime"
          }
        }
      }
    }
  }
}

我要测试的文档

[
    {
        "_index": "pl_people",
        "_type": "person",
        "_id": "813106",
        "_score": null,
        "sort": [
            -9223372036854775808
        ],
        "resource": "Person",
        "parameterized": "813106-aaron-mcguire",
        "created_at": "2011-10-29T19:51:24.000-05:00",
        "updated_at": "2014-12-11T07:21:08.000-06:00",
        "name": "Aaron McGuire",
        "title": null,
        "photo": {
            "url": "/assets/140x140.gif"
        },
        "description": null,
        "short_description": null,
        "time": "2014-12-11",
        "show_path": "/people/813106-aaron-mcguire",
        "published": true,
        "aliases": [],
        "phone": "813-689-6889",
        "date_of_birth": "1991-03-01",
        "first_name": "Aaron",
        "middle_name": "",
        "last_name": "McGuire",
        "delimiters": [],
        "market": null,
        "region": null,
        "most_recent_organization": null,
        "households": [],
        "court_cases": [],
        "addresses": [
            {
                "id": 1,
                "parameterized": "1",
                "address": "123 Scarborough road",
                "zip": "L5A2A9",
                "city": "Ontario",
                "country": "USA",
                "state": "California",
                "location": null,
                "state_id": null,
                "county_id": null,
                "city_id": null
            }
        ],
        "projects": [],
        "voter_ids": [],
        "id": "813106"
    }
]

推荐答案

最后,我能够重现该问题,看起来像"include_in_parent": true,在您的映射中丢失了,由于我遇到了我的评论..

Finally, I was able to reproduce the issue, Looks like "include_in_parent": true, missed in your mapping, due to which I was getting the error mentioned In my comment..

为解决此问题,我使用了相同的映射为您提供,但在address嵌套字段的顶层添加了"include_in_parent": true,.为简洁起见,仅提供修改后映射的address部分.

To fix, the issue I used the same mapping provided you but add "include_in_parent": true, at the top level of address nested field. For brevity providing only the address part of modified mapping.

"addresses": {
            "type": "nested",
            "include_in_parent": true, --> added only this in your mapping.
            "properties": {
              "address": {
                "type": "string"
              },
              "city": {
                "type": "string",
                "index": "not_analyzed"
              },
              "city_id": {
                "type": "long"
              },
              "country": {
                "type": "string",
                "index": "not_analyzed"
              },
              "county": {
                "type": "string",
                "index": "not_analyzed"
              },
              "county_id": {
                "type": "long"
              },
              "id": {
                "type": "long"
              },
              "location": {
                "type": "geo_point"
              },
              "parameterized": {
                "type": "string"
              },
              "state": {
                "type": "string",
                "index": "not_analyzed"
              },
              "state_id": {
                "type": "long"
              },
              "zip": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          }

在此之后,对一些示例文档建立了索引,这些文档的addressname参数值不同,以验证我的搜索查询的正确结果.

After that indexed some sample docs, which have different values of address and name param, to verify the right outcome of my search query.

以及同时包含过滤器的最终搜索查询,请注意,由于父文档中包含了地址字段中的嵌套部分,因此您可以直接在其上进行查询.看起来很简单,现在给出了预期的结果.

And final search query which includes both the filter, please note, I removed the nested part on address field as it's included at the parent document, so you can directly query now on it. This query looks much simple and now gives the expected result.

{
  "query": {
    "match_phrase": {
      "name": "aaron"
    }
  },
  "filter": {
    "bool": {
      "must": [{
        "query": {
          "match_phrase": {
            "address": "scarborough"
          }
        }
      },
      {
       "query": {
          "match_phrase": {
            "phone": "813-689-6889"
          }
        }
      }]
    }
  }
}

示例文档的输出

 "hits": [
            {
                "_index": "so-match-phrase",
                "_type": "pl_people",
                "_id": "4",
                "_score": 0.19178301,
                "_source": {
                    "resource": "Person",
                    "parameterized": "813106-aaron-mcguire",
                    "created_at": "2011-10-29T19:51:24.000-05:00",
                    "updated_at": "2014-12-11T07:21:08.000-06:00",
                    "name": "aaron McGuire",
                    "title": null,
                    "photo": {
                        "url": "/assets/140x140.gif"
                    },
                    "description": null,
                    "short_description": null,
                    "time": "2014-12-11",
                    "show_path": "/people/813106-aaron-mcguire",
                    "published": true,
                    "aliases": [],
                    "phone": "813-689-6889",
                    "date_of_birth": "1991-03-01",
                    "first_name": "Aaron",
                    "middle_name": "",
                    "last_name": "McGuire",
                    "delimiters": [],
                    "market": null,
                    "region": null,
                    "most_recent_organization": null,
                    "households": [],
                    "court_cases": [],
                    "addresses": [
                        {
                            "id": 1,
                            "parameterized": "1",
                            "address": "Scarborough road",
                            "zip": "L5A2A9",
                            "city": "Ontario",
                            "country": "USA",
                            "state": "California",
                            "location": null,
                            "state_id": null,
                            "county_id": null,
                            "city_id": null
                        }
                    ],
                    "id": "813106"
                }
            },
            {
                "_index": "so-match-phrase",
                "_type": "pl_people",
                "_id": "1",
                "_score": 0.19178301,
                "_source": {
                    "resource": "Person",
                    "parameterized": "813106-aaron-mcguire",
                    "created_at": "2011-10-29T19:51:24.000-05:00",
                    "updated_at": "2014-12-11T07:21:08.000-06:00",
                    "name": "Aaron McGuire",
                    "title": null,
                    "photo": {
                        "url": "/assets/140x140.gif"
                    },
                    "description": null,
                    "short_description": null,
                    "time": "2014-12-11",
                    "show_path": "/people/813106-aaron-mcguire",
                    "published": true,
                    "aliases": [],
                    "phone": "813-689-6889",
                    "date_of_birth": "1991-03-01",
                    "first_name": "Aaron",
                    "middle_name": "",
                    "last_name": "McGuire",
                    "delimiters": [],
                    "market": null,
                    "region": null,
                    "most_recent_organization": null,
                    "households": [],
                    "court_cases": [],
                    "addresses": [
                        {
                            "id": 1,
                            "parameterized": "1",
                            "address": "123 Scarborough road",
                            "zip": "L5A2A9",
                            "city": "Ontario",
                            "country": "USA",
                            "state": "California",
                            "location": null,
                            "state_id": null,
                            "county_id": null,
                            "city_id": null
                        }
                    ],
                    "id": "813106"
                }
            }
        ]

这篇关于使用match_phrase组合过滤器-Elastic 1.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆