查询elasticsearch将返回所有文档

时间:2014-10-22 07:35:29

标签: elasticsearch analyzer

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

这是索引以及我如何设置它: (使用elasticsearch head-plugin浏览器界面)

{
  "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会返回所有文档。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

当您使用GET时,您不会传递请求正文,因此在没有任何过滤器的情况下执行搜索并返回所有文档。

当您使用POST时,您的搜索查询会被传递。它不会返回任何内容,可能是因为您的文档未按照您的意图进行分析。

答案 1 :(得分:0)

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

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"
            }
         }
      ]
   }
}
相关问题