【问题标题】:Elasticsearch partial queryElasticsearch 部分查询
【发布时间】:2020-07-29 09:32:07
【问题描述】:

我正在使用 Elasticsearch v 7.3.1 并尝试实现部分搜索。所有搜索都很顺利,但是当我查询“John Oxford”时,“John”与文档匹配,但整个文档中没有“Oxford”。但仍然显示文档而不是显示空结果。

我该怎么做,以便在我们查询 John Oxford 时它不返回文档?

我的映射、设置、示例文档和学生数据查询如下。

映射

PUT student
{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20
        }
      },
      "analyzer": {
        "autocomplete": { 
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  }, "mappings" : {
      "properties" : {
        "DOB" : {
          "type" : "text"
        },
        "email" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "first_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "home_phone" : {
          "type" : "text"
        },
        "last_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "student_id" : {
          "type" : "text"
        }
      }
    }
}

示例文档

POST student/_doc
{
    "DOB": "1983-12-04",
    "email": "johndoe@gmail.fr",
    "first_name": "john",
    "home_phone": 1242432,
    "last_name": "doe",
    "student_id": 28

}

查询

GET student/_search
{
  "query": {
    "multi_match": {
      "query": "john oxford",
      "type": "bool_prefix",
      "analyzer": "standard",
      "fields": [
        "first_name",
        "last_name",
        "email",
        "DOB",
        "home_phone",
        "student_id"
      ]
    }
  }
}

以下是我想要的结果

  • 1242 - 部分匹配 home_phone
  • joh do - 部分匹配“John”和“Doe”
  • 1983-12-04 - 匹配出生日期
  • johndoe - 电子邮件部分匹配
  • doe - 匹配姓氏

【问题讨论】:

  • 抱歉,无法理解您的意思...如果您想进行部分搜索,“john”一词可能是“john oxford”的部分结果。您是否要搜索“john oxford”这一表达是否包含在您的文档中?或者您将检索所有同时包含 john 和 oxford 的文档?
  • @Lupanoide 请检查更新的问题。我已经更新了要求。我的要求应该可以满足。
  • @Lupanoide 请帮助我实现我的目标。谢谢

标签: elasticsearch elastic-stack elasticsearch-query elasticsearch-7


【解决方案1】:

要实现部分搜索,您应该将特定的autocomplete analyzer 添加到所需的文本字段并实现特定的search_analyzer,因为您使用的是edgengram 过滤器 - 请阅读here 和here 以获得解释。正如您所做的那样,这比在查询时指定分析器更舒服。试试:

PUT student
{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20
        }
      },
      "analyzer": {
        "autocomplete": { 
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  }, "mappings" : {
      "properties" : {
        "DOB" : {
          "type" : "text",
          "analyzer": "autocomplete",
          "search_analyzer": "standard"
        },
        "email" : {
          "type" : "text",
          "analyzer": "autocomplete",
          "search_analyzer": "standard",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "first_name" : {
          "type" : "text",
          "analyzer": "autocomplete",
          "search_analyzer": "standard",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "home_phone" : {
          "type" : "text",
          "analyzer": "autocomplete",
          "search_analyzer": "standard"
        },
        "last_name" : {
          "type" : "text",
          "analyzer": "autocomplete",
          "search_analyzer": "standard",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "student_id" : {
          "type" : "text"
        }
      }
    }
}

然后,当您查询两个术语的自动完成时,您应该使用and 运算符将它们连接起来。对于您的用例,cross-field 类型应该是最好的:

GET student/_search
{
  "query": {
    "multi_match" : {
      "query":      "John Oxford",
      "type":       "cross_fields",
      "fields": [
        "first_name",
        "last_name",
        "email",
        "DOB",
        "home_phone",
        "student_id"
      ],
      "operator":   "and" 
    }
  }
}

【讨论】:

  • 很高兴为您提供帮助。祝你有美好的一天:-)
猜你喜欢
  • 1970-01-01
  • 2017-05-22
  • 1970-01-01
  • 2017-04-16
  • 1970-01-01
  • 1970-01-01
  • 2018-11-01
  • 2017-02-11
  • 2015-03-06
相关资源
最近更新 更多