Elasticsearch searching issues on string type

时间:2015-05-04 19:52:17

标签: json elasticsearch

I'm having an issue with a search on an index. Here's my index creation:

curl -XPUT localhost:9200/my_supder_index -d '{
   "mappings":
   {
    "doctype_I_index":
      {
       "properties":
         {
          "field_I_index":
            {
             "type":"string",
             "term_vector":"yes"
            }
          }
        }
    }
}'

Here is a sample piece of content in that index:

{
   _index:"my_super_index",
   _type:"doctype_I_index",
   _id:"676078",
   _version:1,
   found:true,
   _source:{
      created:"2015-05-02T00:24:03",
      field_I_index:[
         "21E0",
         "19E0",
         "5E0",
         "6E0",
         "4E0"
      ],
      id:676078
   }
}

Now when I do a search like this:

 curl -XGET 'http://127.0.0.1:9200/my_super_index/_search' -d '{
   "sort":[
      {
         "created":{
            "order":"desc"
         }
      }
   ],
   "query":{
      "bool":{
         "must":[
            {
               "terms":{
                  "field_I_index":[
                     "21E0"
                  ],
                  "minimum_should_match":1
               }
            }
         ]
      }
   }
}'

I get zero results. It's not doing a match on the text. Can anyone point me in the right direction please?

1 个答案:

答案 0 :(得分:2)

On checking how analysis happens for this value , following are results -

curl -XPOST 'localhost:9200/news/_analyze?pretty' -d '21E0'
{
  "tokens" : [ {
    "token" : "21e0",
    "start_offset" : 0,
    "end_offset" : 4,
    "type" : "<ALPHANUM>",
    "position" : 1
  } ]
}

Here you can see that the text is lower cased. Also as term query does not apply analyzer for the search text , it looks for the exact match of 21E0 , but 21e0 is what is indexed.

Hence in this case , if you use match query , instead of term query , it should work.

But i would recommend to use not_analyzed to the field and then use term query on top of that. It might be a better approach.