简体   繁体   中英

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)

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

Please help me how to query on text.english field.

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 .

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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