java 完全匹配_Elasticsearch与分析字段完全匹配

您可以为此目的使用multi-fields并在 analyzed 字段中有一个 not_analyzed 子字段(在本例中我们称之为 item ) . 您的映射必须如下所示:

{

"yourtype": {

"properties": {

"item": {

"type": "string",

"fields": {

"raw": {

"type": "string",

"index": "not_analyzed"

}

}

}

}

}

}

通过这种映射,您可以检查分析器相对于多字段 item 和 item.raw 的每个值 Hamburgers 和 Hamburger Buns 是如何"viewed"

对于 Hamburger :

curl -XGET 'localhost:9200/yourtypes/_analyze?field=item&pretty' -d 'Hamburger'

{

"tokens" : [ {

"token" : "hamburger",

"start_offset" : 0,

"end_offset" : 10,

"type" : "",

"position" : 1

} ]

}

curl -XGET 'localhost:9200/yourtypes/_analyze?field=item.raw&pretty' -d 'Hamburger'

{

"tokens" : [ {

"token" : "Hamburger",

"start_offset" : 0,

"end_offset" : 10,

"type" : "word",

"position" : 1

} ]

}

对于 Hamburger Buns :

curl -XGET 'localhost:9200/yourtypes/_analyze?field=item&pretty' -d 'Hamburger Buns'

{

"tokens" : [ {

"token" : "hamburger",

"start_offset" : 0,

"end_offset" : 10,

"type" : "",

"position" : 1

}, {

"token" : "buns",

"start_offset" : 11,

"end_offset" : 15,

"type" : "",

"position" : 2

} ]

}

curl -XGET 'localhost:9200/yourtypes/_analyze?field=item.raw&pretty' -d 'Hamburger Buns'

{

"tokens" : [ {

"token" : "Hamburger Buns",

"start_offset" : 0,

"end_offset" : 15,

"type" : "word",

"position" : 1

} ]

}

正如您所看到的, not_analyzed 字段将完全按照输入的方式进行索引 .

现在,让我们索引两个示例文档来说明这一点:

curl -XPOST localhost:9200/yourtypes/_bulk -d '

{"index": {"_type": "yourtype", "_id": 1}}

{"item": "Hamburger"}

{"index": {"_type": "yourtype", "_id": 2}}

{"item": "Hamburger Buns"}

'

最后,为了回答你的问题,如果你想在 Hamburger 上进行完全匹配,你可以在你的子字段 item.raw 中搜索(注意案例也必须匹配):

curl -XPOST localhost:9200/yourtypes/yourtype/_search -d '{

"query": {

"term": {

"item.raw": "Hamburger"

}

}

}'

你会得到:

{

...

"hits" : {

"total" : 1,

"max_score" : 0.30685282,

"hits" : [ {

"_index" : "yourtypes",

"_type" : "yourtype",

"_id" : "1",

"_score" : 0.30685282,

"_source":{"item": "Hamburger"}

} ]

}

}

UPDATE (see comments/discussion below and question re-edit)

从评论中获取您的示例并尝试使 HaMbUrGeR BuNs 匹配 Hamburger buns 您可以使用 match 这样的查询来实现它 .

curl -XPOST localhost:9200/yourtypes/yourtype/_search?pretty -d '{

"query": {

"match": {

"item": {

"query": "HaMbUrGeR BuNs",

"operator": "and"

}

}

}

}'

基于上述相同的两个索引文档将产生

{

...

"hits" : {

"total" : 1,

"max_score" : 0.2712221,

"hits" : [ {

"_index" : "yourtypes",

"_type" : "yourtype",

"_id" : "2",

"_score" : 0.2712221,

"_source":{"item": "Hamburger Buns"}

} ]

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值