作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 flex 搜索索引与此映射。
{
"hotels": {
"mappings": {
"properties": {
"name": {
"type": "text"
},
"location": {
"type": "text"
},
"star": {
"type": "float"
}
}
}
}
}
在索引里面,我放了一些文件。其中一份文件是这样的。
{
"name": "Queens Hotel",
"location": "West Australia",
"star": 3.5
}
然后,我在名称和/或位置字段中搜索匹配的文档。结果应在仅匹配特定单词的单词的顶部显示匹配短语。因此,我创建了此查询。
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "queens loremipsum",
"type": "phrase",
"fields": [
"name",
"location"
],
"boost": 10
}
},
{
"multi_match": {
"query": "queens loremipsum",
"type": "most_fields",
"fields": [
"name",
"location"
],
"fuzziness": "AUTO"
}
}
]
}
}
}
查询结果显示了我上面提供的示例数据。我期望的是
Queens Hotel
将不会显示在结果中,因为在查询中存在与文档不匹配的
loremipsum
。如何实现呢?
最佳答案
The query result shown the example data that I provided above.
should
子句中包含两个多重匹配查询。即使满足多重匹配查询中给出的任何条件,文档也将返回。
{
"query": {
"bool": {
"should": {
"multi_match": {
"query": "queens loremipsum",
"type": "phrase",
"fields": [
"name",
"location"
],
"boost": 10
}
}
}
}
}
没有搜索结果
{
"query": {
"bool": {
"should": {
"multi_match": {
"query": "queens loremipsum",
"type": "most_fields",
"fields": [
"name",
"location"
],
"fuzziness": "AUTO"
}
}
}
}
}
搜索结果:
"hits": [
{
"_index": "stof_64339433",
"_type": "_doc",
"_id": "1",
"_score": 0.10536051,
"_source": {
"name": "Queens Hotel",
"location": "West Australia",
"star": 3.5
}
}
]
因此,由于第二个多重匹配查询而出现了结果,您在其中添加了type: most_fields
甚至"fuzziness": "AUTO"
queens loremipsum
{
"name": "queens loremipsum",
"location": "West Australia",
"star": 3.5
}
搜索查询:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "queens loremipsum",
"type": "phrase",
"fields": [
"name",
"location"
],
"boost": 10
}
},
{
"multi_match": {
"query": "queens loremipsum",
"type": "most_fields",
"fields": [
"name",
"location"
],
"fuzziness": "AUTO"
}
}
]
}
}
}
搜索结果:
should
子句),但是由于在第一个类型为multi_match
的phrase
查询中,因此应用了boost
,因此该文档的得分高于其他文档。
"hits": [
{
"_index": "stof_64339433",
"_type": "_doc",
"_id": "2",
"_score": 9.630155, <-- note this
"_source": {
"name": "queens loremipsum",
"location": "West Australia",
"star": 3.5
}
},
{
"_index": "stof_64339433",
"_type": "_doc",
"_id": "1",
"_score": 0.18232156, <-- note this
"_source": {
"name": "Queens Hotel",
"location": "West Australia",
"star": 3.5
}
}
]
注意:如果只希望匹配确切的文档,则将搜索查询修改为:
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "queens loremipsum",
"type": "phrase",
"fields": [
"name",
"location"
],
"boost": 10
}
}
]
}
}
}
更新1:
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": {
"multi_match": {
"query": "queens australia",
"type": "cross_fields", <-- note this
"operator": "and",
"fields": [
"name",
"location"
],
"boost": 10
}
}
}
},
{
"bool": {
"must": [
{
"multi_match": {
"query": "queens",
"fields": [
"name",
"location"
],
"fuzziness": "AUTO"
}
},
{
"multi_match": {
"query": "australia",
"fields": [
"name",
"location"
],
"fuzziness": "AUTO"
}
}
]
}
}
]
}
}
}
关于elasticsearch - Elasticsearch显示不应显示的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64339433/
我是一名优秀的程序员,十分优秀!