Java 喜欢在 Elasticsearch 中搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31176000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:45:20  来源:igfitidea点击:

Like search in Elasticsearch

javaphpelasticsearchelasticsearch-pluginelasticsearch-net

提问by Dixit Sourav

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.

我正在使用 elasticsearch 从 json 文件中过滤和搜索,我是这项技术的新手。所以我有点困惑如何在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.

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

采纳答案by Daniel Hoffmann-Mitscherling

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

如果可能的话,我强烈建议您更新您的 ElasticSearch 版本,自 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.

这个问题不够具体,因为 ElasticSearch 可以通过多种方式实现此功能,并且它们在您的总体目标上略有不同。如果您希望完全复制该 SQL 查询,则在这种情况下使用通配符查询或前缀查询。

Using a wildcardquery:

使用通配符查询:

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 NOTblindly 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!

此外,这个确切的查询将等同于LIKE '%a%',再次,通过正确使用映射和正常查询搜索可以更好地设置!

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 =)

如果您正在阅读这篇文章,想知道如何以类似方式查询 ES 以进行搜索,我建议您阅读edge-ngrams,这与正确使用映射有关,具体取决于您尝试执行的操作 =)

回答by Pranav Mishra

GET /indexName/table_name/_search
{
    "query": {
        "match_phrase": {
            "field_name": "your partial text"
        }
    }
}

You can use "type" : "phrase_prefix"to prefix or post fix you search Java code for the same:

您可以使用"type" : "phrase_prefix"前缀或后修复您搜索相同的 Java 代码:

AndFilterBuilder andFilterBuilder = FilterBuilders.andFilter();
 andFilterBuilder.add(FilterBuilders.queryFilter(QueryBuilders.matchPhraseQuery("field_name",
          "your partial text")));

Gave 'and filter' example so that you can append extra filters if you want to. Check this for more detail:

给出“和过滤器”示例,以便您可以根据需要附加额外的过滤器。检查这个以获取更多详细信息:

https://www.elastic.co/guide/en/elasticsearch/guide/current/slop.html

https://www.elastic.co/guide/en/elasticsearch/guide/current/slop.html