栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

多字段,多单词,不带query_string的匹配

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多字段,多单词,不带query_string的匹配

您正在寻找的是多重匹配查询,但是它的执行效果并不理想。

比较器的输出验证了

multi_match
VS
query_string

multi_match
(与operator一起使用
and
)将确保所有术语都存在于至少一个字段中:

curl -XGET 'http://127.0.0.1:9200/_validate/query?pretty=1&explain=true'  -d '{   "multi_match" : {      "operator" : "and",      "fields" : [         "firstname",         "lastname"      ],      "query" : "john smith"   }}'# {#    "_shards" : {#       "failed" : 0,#       "successful" : 1,#       "total" : 1#    },#    "explanations" : [#       {#          "index" : "test",#          "explanation" : "((+lastname:john +lastname:smith) | (+firstname:john +firstname:smith))",#          "valid" : true#       }#    ],#    "valid" : true# }

虽然

query_string
(with default_operator
AND
)将检查EACH术语是否存在于至少一个字段中:

curl -XGET 'http://127.0.0.1:9200/_validate/query?pretty=1&explain=true'  -d '{   "query_string" : {      "fields" : [         "firstname",         "lastname"      ],      "query" : "john smith",      "default_operator" : "AND"   }}'# {#    "_shards" : {#       "failed" : 0,#       "successful" : 1,#       "total" : 1#    },#    "explanations" : [#       {#          "index" : "test",#          "explanation" : "+(firstname:john | lastname:john) +(firstname:smith | lastname:smith)",#          "valid" : true#       }#    ],#    "valid" : true# }

因此,您有几种选择可以实现自己的目标:

  1. 在使用搜索引擎之前,请准备好搜索字词,以删除通配符等内容。

    query_string

  2. 准备搜索词以提取每个单词,然后为每个单词生成

    multi_match
    查询

  3. 使用

    index_name
    您的映射名称字段来索引他们的数据到一个单一的领域,然后你就可以使用搜索。(例如您自己的自定义
    all
    字段):

如下:

curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1'  -d '{   "mappings" : {      "test" : {         "properties" : { "firstname" : {    "index_name" : "name",    "type" : "string" }, "lastname" : {    "index_name" : "name",    "type" : "string" }         }      }   }}'curl -XPOST 'http://127.0.0.1:9200/test/test?pretty=1'  -d '{   "firstname" : "john",   "lastname" : "smith"}'curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '{   "query" : {      "match" : {         "name" : { "operator" : "and", "query" : "john smith"         }      }   }}'# {#    "hits" : {#       "hits" : [#          {#  "_source" : {#     "firstname" : "john",#     "lastname" : "smith"#  },#  "_score" : 0.2712221,#  "_index" : "test",#  "_id" : "VJFU_RWbRNaeHF9wNM8fRA",#  "_type" : "test"#          }#       ],#       "max_score" : 0.2712221,#       "total" : 1#    },#    "timed_out" : false,#    "_shards" : {#       "failed" : 0,#       "successful" : 5,#       "total" : 5#    },#    "took" : 33# }

但是请注意,

firstname
并且
lastname
不再可以独立搜索。这两个字段的数据都已索引到中
name

您可以将multifields与

path
参数一起使用,以使它们既可以独立搜索也可以一起搜索,如下所示:

curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1'  -d '{   "mappings" : {      "test" : {         "properties" : { "firstname" : {    "fields" : {       "firstname" : {          "type" : "string"       },       "any_name" : {          "type" : "string"       }    },    "path" : "just_name",    "type" : "multi_field" }, "lastname" : {    "fields" : {       "any_name" : {          "type" : "string"       },       "lastname" : {          "type" : "string"       }    },    "path" : "just_name",    "type" : "multi_field" }         }      }   }}'curl -XPOST 'http://127.0.0.1:9200/test/test?pretty=1'  -d '{   "firstname" : "john",   "lastname" : "smith"}'

搜索

any_name
现场作品:

curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '{   "query" : {      "match" : {         "any_name" : { "operator" : "and", "query" : "john smith"         }      }   }}'# {#    "hits" : {#       "hits" : [#          {#  "_source" : {#     "firstname" : "john",#     "lastname" : "smith"#  },#  "_score" : 0.2712221,#  "_index" : "test",#  "_id" : "Xf9qqKt0TpCuyLWioNh-iQ",#  "_type" : "test"#          }#       ],#       "max_score" : 0.2712221,#       "total" : 1#    },#    "timed_out" : false,#    "_shards" : {#       "failed" : 0,#       "successful" : 5,#       "total" : 5#    },#    "took" : 11# }

搜索

firstname
john AND smith
不工作:

curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '{   "query" : {      "match" : {         "firstname" : { "operator" : "and", "query" : "john smith"         }      }   }}'# {#    "hits" : {#       "hits" : [],#       "max_score" : null,#       "total" : 0#    },#    "timed_out" : false,#    "_shards" : {#       "failed" : 0,#       "successful" : 5,#       "total" : 5#    },#    "took" : 2# }

但是搜索

firstname
仅能
john
正常工作:

curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '{   "query" : {      "match" : {         "firstname" : { "operator" : "and", "query" : "john"         }      }   }}'# {#    "hits" : {#       "hits" : [#          {#  "_source" : {#     "firstname" : "john",#     "lastname" : "smith"#  },#  "_score" : 0.30685282,#  "_index" : "test",#  "_id" : "Xf9qqKt0TpCuyLWioNh-iQ",#  "_type" : "test"#          }#       ],#       "max_score" : 0.30685282,#       "total" : 1#    },#    "timed_out" : false,#    "_shards" : {#       "failed" : 0,#       "successful" : 5,#       "total" : 5#    },#    "took" : 3# }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/409433.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号