喜欢在Elasticsearch搜索 [英] Like search in Elasticsearch

查看:161
本文介绍了喜欢在Elasticsearch搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用弹性搜索来从json文件进行过滤和搜索,而我在这项技术中是新手。所以我有点困惑如何在弹性搜索中写入查询。

  select * from table_name其中'field_name'像'a% '

这是mysql查询。如何在Elasticsearch中写这个查询?我正在使用弹性搜索版本0.90.7。

解决方案

如果可能,我强烈建议更新您的ElasticSearch版本,发生了重大变化因为0.9.x。



这个问题不够具体,因为ElasticSearch有很多方法可以实现这个功能,而且它们的总体目标略有不同。如果您正在寻找完全复制该SQL查询,那么在这种情况下,请使用通配符查询或前缀查询。



使用 wildcard 查询:



注意:仔细阅读通配符,他们很慢。避免在字符串开头使用通配符。

  GET / my_index / table_name / _search 
{
query:{
wildcard:{
field_name:a *
}
}
}

前缀查询

  GET / my_index / table_name / _search 
{
查询:{
前缀:{
field_name:a
}
}
}

部分匹配



注意:执行 NOT 盲目使用部分匹配,而有角位的情况下使用,正确使用分析器几乎总是更好。



此精确查询将相当于 LIKE'%a%'这又可能是tter设置正确使用映射和正常的查询搜索!

  GET / my_index / table_name / _search 
{
query:{
match_phrase:{
field_name:a
}
}
}

如果你正在阅读这个想知道如何查询ES类似的搜索类型我建议阅读edge-ngrams ,其中涉及正确使用映射,具体取决于您正在尝试做什么= )


I am using elasticsearch for filtering and searching from json file and I am newbie in this technology. So I am little bit confused how to write like query in elasticsearch.

select * from table_name where 'field_name' like 'a%'

This is mysql query. How do I write this query in Elasticsearch? I am using elasticsearch version 0.90.7.

解决方案

I would highly suggest updating your ElasticSearch version if possible, there have been significant changes since 0.9.x.

This question is not quite specific enough, as there are many ways ElasticSearch can fulfill this functionality, and they differ slightly on your overall goal. If you are looking to replicate that SQL query exactly then in this case use the wildcard query or prefix query.

Using a wildcard query:

Note: Be careful with wildcard searches, they are slow. Avoid using wildcards at the beginning of your strings.

GET /my_index/table_name/_search
{
    "query": {
        "wildcard": {
            "field_name": "a*"
        }
    }
}

Or Prefix query

GET /my_index/table_name/_search
{
    "query": {
        "prefix": {
            "field_name": "a"
        }
    }
}

Or partial matching:

Note: Do NOT blindly use partial matching, while there are corner cases for it's use, correct use of analyzers is almost always better.

Also this exact query will be equivalent to LIKE '%a%', which again, could be better setup with correct use of mapping and a normal query search!

GET /my_index/table_name/_search
{
    "query": {
        "match_phrase": {
            "field_name": "a"
        }
    }
}

If you are reading this wondering about querying ES similarly for search-as-you-type I would suggest reading up on edge-ngrams, which relate to proper use of mapping depending on what you are attempting to do =)

这篇关于喜欢在Elasticsearch搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆