即使是不成熟的尝试,也胜于胎死腹中的策略。

match_phrase 查询顺序

Elasticsearch | 作者 chengyang | 发布于2019年02月14日 | 阅读数:2678

使用match_phrase查询语句如下,如何保证查出的结果中先出现张三后出现李四?并且“三张”“四李”的情况不要匹配,使用的分词器汉字是每个字都进行了分词。
 
curl -XGET  "http://127.0.0.1:9200/blog/_se ... ot%3B -H 'Content-Type: application/json' -d '
{
    "query": {
        "bool": {
            "must": [
                {
                    "match_phrase": {
                        "message": "张三"
                        "slop":5
                    }
                },
                {
                    "match_phrase": {
                        "message": "李四"
                        "slop":5
                    }
                }
            ]
        }
    }
}'
 
已邀请:

fanmo3yuan

赞同来自:

1. 结果中先出现张三后出现李四,可以去了解一下boost,可以在查询子句中设置不同的boost,使命中张三的得分更高
2. “三张”不要匹配,首先把slop去掉,另外也可以使用ngram去做分词

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

不要加slop了

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

PUT my_index3
{
"mappings": {
"_doc": {
"properties": {
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
}

POST my_index3/_doc/3
{
"title":"张三李四"
}

POST my_index3/_doc/4
{
"title":"四李三张"
}

POST my_index3/_doc/5
{
"title":"张三你好李四"
}

POST my_index3/_doc/6
{
"title":"李四xx你好张三"
}

# 方案一,最准确
POST my_index3/_search
{
"query": {
"wildcard": {
"title.keyword":"张三*李四"
}
}
}

# 方案二,张三、李四组合检索
POST my_index3/_search
{
"query": {
"match_phrase": {
"title": {
"query": "张三李四",
"slop":5
}
}
}
}

要回复问题请先登录注册