elasticsearch词条查询不起作用 [英] elasticsearch term query doesn't work

查看:149
本文介绍了elasticsearch词条查询不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

elasticsearch中的查询一词有问题。我发送以下查询:

I have problem with the term query in elasticsearch. I send the following query:

{
    "query": {
       "term": {
            "title":"Test1"
        }
    }
}

我有一个空结果:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}

但是如果我发送以下内容:

But if I send the following:

{
    "query": {
       "term": {
            "root":true
        }
    }
}

我有:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "content_2018-05-30-092148",
                "_type": "topic",
                "_id": "6064f7ac-63d5-11e8-adc0-fa7ae01bbeb1",
                "_score": 0.2876821,
                "_source": {
                    "_meta": {
                        "model": "Secafi\\Content\\Topic"
                    },
                    "id": "6064f7ac-63d5-11e8-adc0-fa7ae01bbeb1",
                    "title": "Test2",
                    "root": true
                }
            },
            {
                "_index": "content_2018-05-30-092148",
                "_type": "topic",
                "_id": "6064f7ac-63d5-11e8-adc0-fa7ae01bbeb2",
                "_score": 0.2876821,
                "_source": {
                    "_meta": {
                        "model": "Secafi\\Content\\Topic"
                    },
                    "id": "6064f7ac-63d5-11e8-adc0-fa7ae01bbeb2",
                    "title": "Test3",
                    "root": true
                }
            },
            {
                "_index": "content_2018-05-30-092148",
                "_type": "topic",
                "_id": "6064f7ac-63d5-11e8-adc0-fa7ae01bbebc",
                "_score": 0.2876821,
                "_source": {
                    "_meta": {
                        "model": "Secafi\\Content\\Topic"
                    },
                    "id": "6064f7ac-63d5-11e8-adc0-fa7ae01bbebc",
                    "title": "Test1",
                    "root": true
                }
            }
        ]
    }
}

如果对Ť在标题字段中,它永远不会返回任何文档。

I have the same result if I do a match query on the title field, it never returns any document.

出了什么问题。为什么第一个查询不返回文档Test1?

What's wrong. Why the first query doesn't return the document Test1?

推荐答案

字词查询会查找确切的字词。您可能正在使用带有小写过滤器的标准分析仪。因此,当索引中的内容是 term1时,您正在搜索的正是 Term1。

The term query looks for the exact term. Is likely that you are using the standard analyzer which has the lowercase filter. So you are searching for exactly "Term1" when what is in the index is "term1".

Term非常适合精确匹配的数字或ID(例如9844- 9332-22333)。

Term is great for things like exact match numbers or ids (like 9844-9332-22333). Less so for a fields like titles of posts.

要确认这一点,您可以这样做:

To confirm this you could do:

{
    "query": {
       "term": {
            "title.keyword":"Test1"
        }
    }
}

如果您的记录以 Test1的确切标题建立索引,则应该工作。这将使用关键字分析器而不是标准分析器(请注意标题后的 .keyword)。在最新版本的elasticsearch中,默认情况下会添加关键字分析器,除非您覆盖此行为。关键字是完全匹配的 noop分析器,它将整个字符串作为单个令牌返回以进行匹配。

Which should work if your record is indexed with the exact title of "Test1". This uses the keyword analyzer instead of the standard analyzer (notice the ".keyword" after title). In the latest versions of elasticsearch the keyword analyzer is added by default unless you override this behavior. Keyword is an exact match "noop" analyzer that returns the entire string as a single token to match against.

对于标题,您可能想要的是:

For a title, what you probably want is:

{
    "query": {
       "match": {
            "title":"Test1"
        }
    }
}

Match查询通过标准分析器运行您的输入字符串,默认情况下,该分析器用于对文档建立索引(小写等),因此elasticsearch可以以便将您的查询文本与elasticsearch索引中的内容进行匹配。

Match query runs your input string through the standard analyzer that was used by default to index the document (lowercaseing etc) so elasticsearch is able to match your query text to what is in the elasticsearch index.

查看文档以获取有关匹配与条件的更多详细信息。
https:// www .elastic.co / guide / zh-CN / elasticsearch / reference / current / query-dsl-match-query.html
https://www.elastic.co/guide/zh/elasticsearch/reference/current/query-dsl-term-query .html

Checkout the docs for more details on match vs term. https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html

这篇关于elasticsearch词条查询不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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