Es 模糊查询的方式
Es 模糊查询的方式

Es 模糊查询的方式

Es 模糊查询的方式

要求:
Es查询:

查询工单信息, 输入 “测试”,查出 form_name 为字段中有查询出含有符合内容的数据

match:分词模糊查询:
比如“Everything will be OK, All is well”,会被分词一个一个单词(不是单个字母)

{
    "from": 0,
    "size": 20,
    "query": {
        "bool": {
            "should": [{
                    "term": {
                        "form_name": "will"
                    }
                }
            ]
        }
    }

}

match_phrase :短语模糊查询
match_phrase是短语搜索,即它会将给定的短语(phrase)当成一个完整的查询条件。

比如查询 “Everything will”,会当成一个完整的短语进行查询, 会查出含有该查询条件的内容。

GET /basic_index*/_search
{
    "from": 0,
    "size": 20,
    "query": {
        "bool": {
            "should": [{
                    "match": {
                        "form_name": "Everything will"
                    }
                }
            ]
        }
    }

}

如果是查询单个字母,match就不管用了,那该如何处理呢?

wildcard:通配符模糊查询:

匹配任意字符

匹配0个或多个字符

GET /basic_index*/_search
{
    "size": 20,
    "from": 0,
    "query": {
        "bool": {
            "should": [{
                "wildcard": {
                    "form_name": "*very*
                }
            }]
        }
    }
}

记录是存在的,但是没有查出来? 因为分词的影响,添加keyword 进行处理

 {
    "wildcard": {
        "form_name.keyword": "*very*"
    }
}

Wildcard 性能会比较慢。如果非必要,尽量避免在开头加通配符 ? 或者 *,这样会明显降低查询性能

如果查询的内容非空,怎么处理? 直接用*

{
    "wildcard": {
        "form_name": "*"
    }
}

总结:
Es 模糊查询, 分词的用match; 短语的用match_phrase;查询任意的,用wildcard通配符,注意查询的内容是否分词,分词的添加keyword,查询非空的情况,用*。


python:

body = {
            '_source': ['company_name', 'id'],
            'size': self.args.rows,
            "from": self.args.start_index,
            'query': {
                'wildcard': {
                    'company_name.keyword': {"value": "*"+self.company_name+"*"},
                },
            },
        }

        # 搜索所有数据
        es_result = self.es_obj.search(index="eoias_company_base_info", body=body, request_timeout=120)

发表回复

您的电子邮箱地址不会被公开。