一尘不染

查询elasticsearch返回所有文档

elasticsearch

我想知道为什么搜索特定术语会返回索引的所有文档,而不返回包含所请求术语的文档。

这是索引以及我的设置方法:(使用elasticsearch头插件浏览器界面)

{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 1,
    "analysis": {
      "filter": {
        "dutch_stemmer": {
          "type": "dictionary_decompounder",
          "word_list": [
            "koud",
            "plaat",
            "staal",
            "fabriek"
          ]
        },
        "snowball_nl": {
          "type": "snowball",
          "language": "dutch"
        }
      },
      "analyzer": {
        "dutch": {
          "tokenizer": "standard",
          "filter": [
            "length",
            "lowercase",
            "asciifolding",
            "dutch_stemmer",
            "snowball_nl"
          ]
        }
      }
    }
  }
}

{
  "properties": {
    "test": {
      "type": "string",
      "fields": {
        "dutch": {
          "type": "string",
          "analyzer": "dutch"
        }
      }
    }
  }
}

然后我添加了一些文档:

{"test": "ijskoud"}
{"test": "plaatstaal"}
{"test": "kristalfabriek"}

因此,现在触发“ plaat”搜索时,人们会希望搜索会返回包含“ plaatstaal”的文档。

{
  "match": {
    "test": "plaat"
  }
}

但是为我节省了更多的搜索,elasticsearch会恢复所有文档的大小,无论其文本内容如何。我在这里想念什么吗?有趣的是:使用GET或POST时有所不同。尽管使用后者不会带来任何成功,但GET会返回所有文档。

任何帮助深表感谢。


阅读 1385

收藏
2020-06-22

共1个答案

一尘不染

您需要配置索引以使用自定义分析器:

PUT /some_index
{
  "settings": {
     ...
  },
  "mappings": {
    "doc": {
      "properties": {
        "test": {
          "type": "string",
          "analyzer": "dutch"
        }
      }
    }
  }
}

如果您有更多使用此分析器的字段,并且不想为每个分析器指定,则可以针对该索引中的特定类型执行以下操作:

  "mappings": {
    "doc": {
      "analyzer": "dutch"
    }
  }

如果希望该索引中的所有类型都使用自定义分析器:

  "mappings": {
    "_default_": {
      "analyzer": "dutch"
    }
  }

要以简单的方式测试分析仪:

GET /some_index/_analyze?text=plaatstaal&analyzer=dutch

这将是执行步骤的完整列表:

DELETE /some_index

PUT /some_index
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 1,
    "analysis": {
      "filter": {
        "dutch_stemmer": {
          "type": "dictionary_decompounder",
          "word_list": [
            "koud",
            "plaat",
            "staal",
            "fabriek"
          ]
        },
        "snowball_nl": {
          "type": "snowball",
          "language": "dutch"
        }
      },
      "analyzer": {
        "dutch": {
          "tokenizer": "standard",
          "filter": [
            "length",
            "lowercase",
            "asciifolding",
            "dutch_stemmer",
            "snowball_nl"
          ]
        }
      }
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "test": {
          "type": "string",
          "analyzer": "dutch"
        }
      }
    }
  }
}

POST /some_index/doc/_bulk
{"index":{}}
{"test": "ijskoud"}
{"index":{}}
{"test": "plaatstaal"}
{"index":{}}
{"test": "kristalfabriek"}

GET /some_index/doc/_search
{
  "query": {
    "match": {
      "test": "plaat"
    }
  }
}

搜索结果:

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1.987628,
      "hits": [
         {
            "_index": "some_index",
            "_type": "doc",
            "_id": "jlGkoJWoQfiVGiuT_TUCpg",
            "_score": 1.987628,
            "_source": {
               "test": "plaatstaal"
            }
         }
      ]
   }
}
2020-06-22