json 使用 Elasticsearch 搜索多个字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18695310/
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-09-03 19:47:45  来源:igfitidea点击:

Search on multiple fields with Elasticsearch

jsonelasticsearch

提问by Pennywise83

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

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

With SQL I would write this query:

使用 SQL 我会写这个查询:

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

In Elasticsearch I'm starting from this:

在 Elasticsearch 中,我从这个开始:

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

采纳答案by javanna

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 queriesare 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.

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

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.

elasticsearch 中最常见的查询之一是匹配查询,它适用于单个字段。还有另一个具有相同选项的查询,它也适用于多个字段,称为multi_match。这些查询支持文本分析并且工作得非常好。例如,我建议在 query_string 查询上使用它们,由于需要的解析过程,它功能更强大但也容易出错。我会说仅当您特别需要它的功能之一时才使用 query_string(例如,在查询本身中指定字段名称或布尔运算符),否则使用匹配查询。

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

了解查询和过滤器之间的区别也很重要,请查看此处了解更多信息。

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

并且一定要查看查询 DSL 中可用的所有查询并尝试使用这些查询,只是为了感受一下您可以做的所有不同的事情。

回答by moliware

The sql query is equivalent to:

sql查询等价于:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "field1": "X"
          }
        },
        {
          "term": {
            "field3": "Z"
          }
        }
      ],
      "must_not": {
        "term": {
          "field2": "Y"
        }
      }
    }
  }
}

In any case I recommend you to read a bit the doc before starting with elasticsearch if you are new.

无论如何,如果您是新手,我建议您在开始使用 elasticsearch 之前阅读一些文档。

There are lots of types of queries and some of them depends on how you index your data, for example for strings, you can analyze strings (lowercase, stem words, remove stopwords, ...) at index time. The query I posted will never match a doc whose field1 is "X" if you analyze that field at index time and convert it to lower case.

有很多类型的查询,其中一些取决于您如何索引数据,例如对于字符串,您可以在索引时分析字符串(小写、词干、删除停用词等)。如果您在索引时分析该字段并将其转换为小写,我发布的查询将永远不会匹配 field1 为“X”的文档。

Once you know a little bit better elasticsearch you can use filters for improving your queries.

一旦您了解了一些更好的 elasticsearch,您就可以使用过滤器来改进您的查询。

回答by stinger

I would suggest to start with Elastic's Simple query. It's more SQL-like and easier to understand. Link: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html

我建议从 Elastic 的简单查询开始。它更像 SQL 并且更容易理解。链接:https: //www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html

Query syntax will look like, e.g. if you're trying to find guest with name John AND lastname Doe:

查询语法如下所示,例如,如果您尝试查找名为 John 和姓氏 Doe 的来宾:

GET /_search
{
  "query": {
    "simple_query_string" : {
        "query": "John + Doe",
        "fields": ["guest"],
        "default_operator": "and"
    }
  }
}

If any of this criterias will not match, query will return no hits. Also, here you can search over multiple fields, but it will be slower rather than searching on one field. Also, per docs, simple query support special symbols as logical\search operators:

如果此条件中的任何一个不匹配,则查询将不返回任何匹配项。此外,在这里您可以搜索多个字段,但比搜索一个字段要慢。此外,根据文档,简单查询支持特殊符号作为逻辑\搜索运算符:

 '+' signifies AND operation | signifies OR operation
 '-' negates a single token " wraps a number of tokens to signify a phrase for searching
 '*' at the end of a term signifies a prefix query

回答by Akhilraj N S

If you want to search same value on multiple fields then you can try

如果您想在多个字段上搜索相同的值,那么您可以尝试

{"query": {"multi_match": {"query": "querystring", "fields": ["name", "description"]}}}

Replace querystringwith your search keyword

替换querystring为您的搜索关键字