使用Elasticsearch在多个字段上搜索 [英] Search on multiple fields with Elasticsearch

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

问题描述

我是Elasticsearch的新手,我想知道如何进行搜索以指定一个或多个字段.

I'm new to Elasticsearch and I'm wondering how I can make a search specifing one or more fields.

使用SQL,我将编写以下查询:

With SQL I would write this query:

"SELECT field1, field2, field3 FROM tablename WHERE field1 = 'X' AND field2 != 'Y' AND field3 = 'Z'"

在Elasticsearch中,我从这里开始:

In Elasticsearch I'm starting from this:

{
    "query": {
        "filtered": {
            "query": {
                "query_string": {
                    "query": "*"
                }
            },
            "filter": {
                "term" : {
                    "field1" : "286"
                }
            }
        }
    }
}

推荐答案

您需要为工作选择正确的查询,这在一开始可能很难.您绝对可以使用布尔查询将各种不同的查询组合在一起,就像已经建议的那样.还有一些查询允许在多个字段上执行,并在内部映射到布尔查询.另外,术语查询在生产系统中并不常见由于它们不支持任何文本分析,因此您通常希望以与对查询字段建立索引的方式相似的方式来分析查询.

You need to pick the right query for the job, which can be hard in the beginning. You can definitely use a bool query to combine all sorts of different queries together, like already suggested. There are also queries that allow to be executed on multiple fields, and map to boolean queries internally. Also, term queries are not so common in a production system since they don't support any text analysis, while you usually want to analyze the query in a way that's similar to the way you indexed the field you are querying.

elasticsearch中最常见的查询之一是match查询,它适用于单个字段.还有另一个具有相同选项的查询也可以在多个字段上使用,称为

One of the most common queries in elasticsearch is the match query, which works on a single field. And there's another query with the very same options that works also on multiple fields, called multi_match. These queries support text analysis and work really well. I would suggest to use them over query_string query for instance, which is a lot more powerful but error-prone as well due to the needed parsing process. I would say use the query_string only if you specifically need one of its features (e.g. specifying the field names or boolean operators within the query itself), otherwise go for match queries.

理解查询和过滤器之间的区别也很重要,请在此处了解更多

It's also important to understand the difference between queries and filters, have a look here to know more.

并查看查询DSL

And do have a look at all the queries available with the query DSL and play around with those, just to have a feeling of all the different things you can do.

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

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