查询elasticsearch返回所有文档 [英] Querying elasticsearch returns all documents

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

问题描述



这是索引,我如何设置它up
(使用弹性搜索头插件浏览器界面)

  {
settings {
number_of_replicas:1,
number_of_shards:1,
分析:{
过滤器:{
dutch_stemmer:{
type:dictionary_decompounder,
word_list:[
koud,
plaat,
staal,
fabriek
]
},
snowball_nl:{
type:snowball,
language:dutch
}
$ b分析器:{
dutch:{
tokenizer:standard,
filter:[
length ,
小写,
asciifolding,
dutch_stemmer,
snowball_nl
]
}
}
}
}
}

{
properties:{
test:{
type:string,
fields:{
dutch b $ btype:string,
analyzer:dutch
}
}
}
}
}

然后我添加了一些文档:

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

所以现在,当以plaat进行搜索时,某种程度上的搜索会返回包含plaatstaal的文档。 >

  {
match:{
test:plaat
}
}

不过,无论文本内容如何,​​弹性搜索都会重新记录所有文档。
有没有什么我在这里?
有趣的是,使用GET或POST时有区别。而使用后者没有返回任何命中,GET返回所有文档。



任何帮助都非常感激。

解决方案

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

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

如果您有更多字段使用此分析器,不想为每个分析器指定,您可以像该索引中的特定类型一样执行:

 映射:{
doc:{
analyzer:dutch
}
}

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

 mappings:{
_default_:{
analyzer:dutch
}
}

以简单的方式测试分析器:

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

这将是要执行的完整步骤:

  DELETE / some_index 

PUT / some_index
{
设置:{
number_of_replicas 1,
number_of_shards:1,
分析:{
过滤器:{
dutch_stemmer:{
type:dictionary_decompounder ,
word_list:[
koud,
plaat,
staal,
fabriek
]
},
snowball_nl:{
type:snowball,
language:dutch
}
},
分析器:{
dutch:{
tokenizer:standard,
filter:[
length,
smallcase,
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
}
}
}

搜索结果:

  {
take:1,
timed_out:false,
_shards:{
total:1,
success: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
}
}
]
}
}
/ pre>

i wonder why a search for a specific term returns all documents of an index and not the documents containing the requested term.

Here's the index and how i set it up: (using the elasticsearch head-plugin browser-interface)

{
  "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"
        }
      }
    }
  }
}

Then i added some docs:

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

So now when firing a search for "plaat" somehow one would expect the search would come back with the document containing "plaatstaal".

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

However saving me further searches elasticsearch retuns all documents regardless of its text content. Is there anything I am missing here? Funny enough: there is a difference when using GET or POST. While using the latter brings back no hits, GET returns all documents.

Any help is much appreciated.

解决方案

You need to configure your index to use your custom analyzer:

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

If you have more fields that use this analyzer and don't want to specify for each the analyzer, you can do it like this for a specific type in that index:

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

If you want ALL your types in that index to use your custom analyzer:

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

To test your analyzer in a simple way:

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

This would be the full list of steps to perform:

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"
    }
  }
}

And the result of the search:

{
   "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"
            }
         }
      ]
   }
}

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

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