简体   繁体   English

如何查询elasticsearch中的字段?

[英]How to query fields in elasticsearch?

I try to do some query with fields mapping but it not working.我尝试使用字段映射进行一些查询,但它不起作用。

define index mapping定义索引映射

english_mapping = {
 "mappings": {
    "properties": {
      "text": { 
        "type": "text",
        "fields": {
          "english": { 
            "type":     "text",
            "analyzer": "english"
          }
        }
      }
    }
  }
}

es.indices.create(index = 'english', body = english_mapping)

add some data添加一些数据

doc2 = { "text": "quick brown fox" }
doc3 = { "text": "quick brown foxes" }
es.index(index='english', document = doc2, id = 1)
es.index(index='english', document = doc3, id = 2)

query on text.english field but it not working (it return both document)查询 text.english 字段但它不起作用(它返回两个文档)

res = es.search(index = 'english',  body = {"query": {
    "match": {
        "text.english" : "foxes"
    }
}})

Please help me how to query on text.english field.请帮助我如何查询 text.english 字段。

The english analyzer you're using is composed of an english_stemmer token filter , and what this does is stemming words, like for instance foxes gets' stemmed to fox .您正在使用的english分析器由一个english_stemmer标记过滤器组成,它所做的是词干提取,例如foxes gets' stemmed to fox

Hence, both of your documents below get analyzed/stemmed the same way因此,您下面的两个文档都以相同的方式进行分析/提取

doc2 = { "text": "quick brown fox" }
doc3 = { "text": "quick brown foxes" }

ie the following tokens get indexed: quick , brown and fox .即以下标记被索引: quickbrownfox

Same thing at search time when searching for foxes , it's gets analyzed to fox and that's the reason why it finds both documents.在搜索foxes时的搜索时间也是如此,它被分析为fox ,这就是它找到两个文档的原因。

If you search on the text field instead of text.english , the standard analyzer will be used instead and no stemming occurs, so it will work as you expect.如果您在text字段而不是text.english上搜索,则会使用standard分析器,并且不会发生词干提取,因此它将按您预期的方式工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM