【问题标题】:Elasticsearch how to use multi_match with wildcardElasticsearch如何使用带通配符的multi_match
【发布时间】:2013-05-31 18:59:48
【问题描述】:

我有一个带有名称和姓氏属性的用户对象。我想使用一个查询来搜索这些字段,我在文档中找到了multi_match,但我不知道如何正确使用通配符。可能吗?

我尝试了 multi_match 查询,但没有成功:

{
    "query": {
        "multi_match": {
            "query": "*mar*",
            "fields": [
                "user.name",
                "user.surname"
            ]
        }
    }
}

【问题讨论】:

    标签: elasticsearch wildcard


    【解决方案1】:

    我不会使用通配符,它​​不会很好地扩展。您在查询时询问了很多搜索引擎。您可以使用 nGram 过滤器在索引时间而不是搜索时间进行处理。

    See this discussion on the nGram filter.

    正确索引namesurname 后(更改您的映射,上面的链接中有示例)您可以使用多重匹配但不使用通配符并获得预期结果。

    【讨论】:

    • 缺点:索引大小会增长很多。
    【解决方案2】:

    或者,您可以使用带有通配符的 query_string 查询。

    "query": {
        "query_string": {
            "query": "*mar*",
            "fields": ["user.name", "user.surname"]
        }
    }
    

    这将比在索引时使用 nGram 过滤器慢(请参阅我的其他答案),但如果您正在寻找一个快速而肮脏的解决方案......

    我也不确定您的映射,但如果您使用user.name 而不是name,您的映射需要如下所示:

    "your_type_name_here": {
        "properties": {
            "user": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "surname": {
                        "type": "string"
                    }
                }
            }
        }
    }
    

    【讨论】:

    • query_string 不仅如此,它还公开了 lucene 查询语法。我宁愿看通配符查询。
    • 你能粘贴一个链接到query_string 的详细信息吗?我对这种行为有点困惑......
    • 查看本页底部查询字符串和匹配查询的比较:elasticsearch.org/guide/reference/query-dsl/match-query
    • 答案不是回答如何使用 multi_match 和通配符的问题。有人有想法吗?
    • 关于多匹配和正则表达式的弹性文档真是令人头疼。这很好用,谢谢。
    【解决方案3】:

    我现在才这样做:

    GET _search {
        "query": {
            "bool": {
                "must": [
                    {
                        "range": {
                            "theDate": {
                                "gte": "2014-01-01",
                                "lte": "2014-12-31"
                            }
                        }
                    },
                    {
                        "match" : {
                            "Country": "USA"
                        }
                    }
                ],
                "should": [
                    {
                        "wildcard" : { "Id_A" : "0*" }
                    },
                    {
                        "wildcard" : { "Id_B" : "0*" }
                    }
                ],"minimum_number_should_match": 1
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      这样的查询对我有用:

      {
        "query": {
          "filtered": {
            "query": {
              "match_all": {}
            },
            "filter": {
              "bool": {
                "should": [
                  {"query": {"wildcard": {"user.name": {"value": "*mar*"}}}},
                  {"query": {"wildcard": {"user.surname": {"value": "*mar*"}}}}
                ]
              }
            }
          }
        }
      }
      

      与您正在做的事情类似,只是在我的情况下,不同的字段可能有不同的掩码。

      【讨论】:

      • 谢谢凯瑟琳 Tsokur。我也在尝试相同的方式,但有没有办法可以保留同一列的多个通配符条件。示例:user.name 可以是 5 种不同的通配符模式。请建议我如何使用该条件。
      【解决方案5】:

      类似于上面的建议,但这很简单并且对我有用:

      {
      "query": {
          "bool": {
              "must":
              [
                  {
                      "wildcard" : { "processname.keyword" : "*system*" }
                  },
                  {
                      "wildcard" : { "username" : "*admin*" }
                  },
                  {
                      "wildcard" : { "device_name" : "*10*" }
                  }
              ]
          }
      }
      }
      

      【讨论】:

      • 如果一个坏了就会失败。所以这些属性中的每一个都需要字符串的相同部分..
      【解决方案6】:
      description: {
        type: 'keyword',
        normalizer: 'useLowercase',
      },
      product: {
            type: 'object',
            properties: {
              name: {
                type: 'keyword',
                normalizer: 'useLowercase',
              },
            },
          },
      activity: {
            type: 'object',
            properties: {
              name: {
                type: 'keyword',
                normalizer: 'useLowercase',
              },
            },
          },
      

      查询:

                query: {
                  bool: {
                    must: [
                      {
                        bool: {
                          should: [
                            {
                              wildcard: {
                                description: {
                                  value: `*${value ? value : ''}*`,
                                  boost: 1.0,
                                  rewrite: 'constant_score',
                                },
                              },
                            },
                            {
                              wildcard: {
                                'product.name': {
                                  value: `*${value ? value : ''}*`,
                                  boost: 1.0,
                                  rewrite: 'constant_score',
                                },
                              },
                            },
                            {
                              wildcard: {
                                'activity.name': {
                                  value: `*${value ? value : ''}*`,
                                  boost: 1.0,
                                  rewrite: 'constant_score',
                                },
                              },
                            },
                          ],
                        },
                      },
                      {
                        match: {
                          recordStatus: RecordStatus.Active,
                        },
                      },
                      {
                        bool: {
                          must_not: [
                            {
                              term: {
                                'user.id': req.currentUser?.id,
                              },
                            },
                          ],
                        },
                      },
                      {
                        bool: {
                          should: tags
                            ? tags.map((name: string) => {
                                return {
                                  nested: {
                                    path: 'tags',
                                    query: {
                                      match: {
                                        'tags.name': name,
                                      },
                                    },
                                  },
                                };
                              })
                            : [],
                        },
                      },
                    ],
                    filter: {
                      bool: {
                        must_not: {
                          terms: {
                            id: existingIds ? existingIds : [],
                          },
                        },
                      },
                    },
                  },
                },
                sort: [
                  {
                    updatedAt: {
                      order: 'desc',
                    },
                  },
                ],
      

      【讨论】:

        猜你喜欢
        • 2019-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多