当前位置: 首页 > 面试题库 >

使用match_phrase组合过滤器-Elastic 1.7

高朝明
2023-03-14
问题内容

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

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

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

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

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

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

例如: 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过滤器时出现错误:

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,因为您遇到了我在评论中提到的错误,因此看起来好像在映射中错过了。。

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

"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"
              }
            }
          }

之后收录一些样本文档,其中有不同的价值观addressnamePARAM,以验证我的搜索查询的一个正确的结果。

最后的搜索查询包括两个过滤器,请注意,我删除 了父文档中包含的地址字段中嵌套部分,因此您现在就可以直接对其进行查询。
该查询看起来非常简单,现在可以提供预期的结果。

{
  "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"
                }
            }
        ]


 类似资料:
  • 我想要一个组合框,它会在用户键入时过滤列表项。它应该如下工作: 键入时,文本字段应该显示一个可能的选择,但是用户尚未键入的单词部分应该突出显示。 当他打开列表时,下拉框应该只显示可能的选项? 使用箭头键,用户应该在缩小可能的项目后选择剩余的项目之一。 过滤并不重要,跳转到第一个匹配的选择也可以。 有类似的吗?

  • 有没有办法把这两条流合并成一条?我使用第一个流在嵌套列表中进行过滤和查找,并使用第二个流根据流的结果创建地图。我想知道是否有一种方法可以用一条流来实现这一点。 像这样的

  • 我从事elasticsearch,我尝试混合两个工作查询。第一个是“and filter”,第二个是“bool filter”,但我失败了。 我的查询是从用户交互界面动态生成的。 “和过滤器”: 我需要“和过滤器”来查询数据,例如,字段必须等于“非洲”或“亚洲”或为空。这是一个工作查询的示例: 此查询工作正常,结果如下: 现在我需要用字段“D\u TYPESTATUS”来限制结果数据,该字段必须与

  • 问题内容: 我有一个,我想在一行中过滤掉负值(创建一个没有新数组)而不添加循环。使用Java 8 Lambda表达式是否可能? 在python中,将使用生成器: 是否可以在Java 8中做类似的简洁操作? 问题答案: 是的,您可以通过在数组中创建一个,滤除负片,然后将流转换回数组来实现此目的。这是一个例子: 如果要过滤不是的引用数组,则需要使用采用的方法来获取原始类型的数组作为结果:

  • 本文向大家介绍dynamics-crm 使用过滤器过滤API查询,包括了dynamics-crm 使用过滤器过滤API查询的使用技巧和注意事项,需要的朋友参考一下 示例 您可以使用filter属性从CRM检索值的子集。在此示例中,仅返回公司名称等于CompanyName的帐户。            

  • 所有任务都实现了 Chain-of-responsibility 模式并且可以像ASP.NET MVC操作过滤器一样被拦截。 定义过滤器 public class LogEverythingAttribute : JobFilterAttribute, IClientFilter, IServerFilter, IElectStateFilter, IApplyStateFilter {