elasticsearch不返回预期收益

时间:2020-07-07 05:39:26

标签: elasticsearch

我对Elasticsearch完全陌生。我尝试使用搜索API,但未返回预期的结果

我做了

POST /test/_doc/1
{
  "name": "Hello World"
}

GET /test/_doc/1
Response:
{
  "_index" : "test",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 5,
  "_seq_no" : 28,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Hello World"
  }
}

GET /test/_mapping
Response:
{
  "test" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "query" : {
          "properties" : {
            "term" : {
              "properties" : {
                "name" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

GET /test/_search
{
  "query": {
    "term": {
      "name": "Hello"
    }
  }
}:
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

GET /test/_search
{
  "query": {
    "term": {
      "name": "Hello World"
    }
  }
}
Response:
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

我的elasticsearch版本是7.3.2

最后两个搜索应该返回我文件1,对吗?为什么什么都没打?

1 个答案:

答案 0 :(得分:3)

问题是您有term queries。不分析词条查询。因此,Hello与索引中的术语hello不匹配。请注意大小写差异。

与全文查询不同,术语级查询不会分析搜索词。相反,术语级查询会匹配字段中存储的确切术语。

Reference

match查询也会分析搜索字词。

{
  "query": {
    "match": {
      "name": "Hello"
    }
  }
}

您可以使用_analyze检查如何为您的术语建立索引。

相关问题