Elasticsearch部分查询

百合

我正在使用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": "[email protected]",
    "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-匹配DOB
  • johndoe-电子邮件中的部分匹配
  • 母鹿-匹配姓氏
羽扇豆类

要实施部分搜索,您应该autocomplete analyzer在所需的文本字段中添加特定内容,并search_analyzer由于使用edgengram过滤器而实施特定内容-请在此处此处阅读以获取解释。与您在查询期间指定分析器相比,这样做更舒服。尝试:

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运算符连接起来对于您的用例,跨字段类型应该是最好的:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章