为什么此Elasticsearch查询不返回任何内容 [英] Why this elasticsearch query doesnt return anything

查看:73
本文介绍了为什么此Elasticsearch查询不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 elasticsearch 查询下面执行了

GET amasyn/_search
{
    "query": {
        "bool" : {
            "filter" : {
                "term": {"ordernumber": "112-9550919-9141020"}
            }
        }
    }
}

但是没有任何点击

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

但是我有一个文档在索引中包含此 ordernumber . ordernumber 是一个文本字段.

But I have a document having this ordernumber in the index. ordernumber is a text field.

当我通过用 match 替换 term 来更改上述查询时,得到的总点击数是给定查询的总点击数.请解释这里发生了什么以及如何解决这个问题.

When I change the above query by replacing term with match, I get total number of hits as the no of hits for the given query. Please explain what's happening here and how to solve this.

推荐答案

这是因为由于您将 ordernumber 字段用作文本类型,因此正在对其进行分析.请通过此答案引用文本和关键字之间的区别关键字和关键字之间的区别文本在ElasticSearch中.

This is because since you used ordernumber field with type as text, so it is getting analyzed. Please refer difference between text and keyword through this answer Difference between keyword and text in ElasticSearch.

通过这种方式,您可以为 ordernumber 字段定义文本和关键字.

In this way you can define both text and keyword for your ordernumber field.

{
    "mappings": {
        "properties": {
            "ordernumber": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }
        }
    }
}

,然后您可以使用以下术语查询:

and then you can use term query as below :

{
    "query": {
        "bool" : {
            "filter" : {
                "term": {"ordernumber.keyword": "112-9550919-9141020"}
            }
        }
    }
}

请查看如何为您的文本标记 text keyword 字段.

Please see, how text and keyword fields are tokenized for your text.

当您将字段定义为 text 时,将使用此分析器.

This analyzer is used when you were defining your field as text.

{
    "analyzer": "standard",
    "text" : "112-9550919-9141020"
}

结果:

 {
        "tokens": [
            {
                "token": "112",
                "start_offset": 0,
                "end_offset": 3,
                "type": "<NUM>",
                "position": 0
            },
            {
                "token": "9550919",
                "start_offset": 4,
                "end_offset": 11,
                "type": "<NUM>",
                "position": 1
            },
            {
                "token": "9141020",
                "start_offset": 12,
                "end_offset": 19,
                "type": "<NUM>",
                "position": 2
            }
        ]
    }

关键字分析器

当您将字段定义为关键字时,将使用此分析器.

{
    "analyzer": "keyword",
    "text" : "112-9550919-9141020"
}

结果

 {
        "tokens": [
            {
                "token": "112-9550919-9141020",
                "start_offset": 0,
                "end_offset": 19,
                "type": "word",
                "position": 0
            }
        ]
    }

这篇关于为什么此Elasticsearch查询不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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