How to get ampersand as value

hi Team,

below is my query for searching
http://localhost:9200/news//_search { "query": { "bool": { "must": [{ "multi_match": { "query": "T&L" , "fields": [ "stkno", "tag" ,"content" ,"htext" ] ,"operator": "and"} }] } }, "from": 0, "size": 100, "sort": [ { "CDate": { "order": "desc" } } ] }

result can't get 'T&L', it will show result not related.how to get & also as value?

thanks and best regards
Sharon

Can you show the mapping you get from running the command below?

curl -XGET http://localhost:9200/news

hi,

this is my mapping

"news": {
"aliases": { },
"mappings": {
"trkd": {
"properties": {
"CDate": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"RDate": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"category": {
"type": "string"
},
"content": {
"type": "string"
},
"exDate": {
"type": "string"
},
"exgcode": {
"type": "string"
},
"htext": {
"type": "string"
},
"paymentDate": {
"type": "string"
},
"query": {
"properties": {
"match_all": {
"type": "object"
}
}
},
"stat": {
"type": "string"
},
"stkno": {
"type": "string"
},
"tag": {
"type": "string"
},
"url": {
"type": "string"
}
}
}

thanks

That's what I feared, your fields are all string, i.e. they are analyzed and thus the ampersand character is discarded at indexing time.

You need to either change the field type of those fields to being not_analyzed strings, i.e.

"stkno": {
    "type": "string",
    "index": "not_analyzed"
},

or add a not_analyzed sub-field.

"stkno": {
    "type": "string",
    "fields": {
        "raw": {
            "type": "string",
            "index": "not_analyzed"
        }
    }
},

In both cases, you need to reindex your data.
Let us know

what is the query for add index?

is it like this

PUT /news/trkd/_mapping/
{
"properties" : {
"tag" : {
"type" : "string",
"index": "not_analyzed"
}
}
}

need to delete the mapping first just create this mapping?

Yes, delete your index, modify your mapping accordingly and recreate the index with the new mapping. Then reindex your data and try your query again.

we can't create new mapping with current index?
when delete the index, no mapping found and how to create the mapping?
i have doing at reindex side, when reindex back to news, the mapping will same as before without that not-analysed

//1. delete the index
DELETE news

//2 . Create the new index with the new mapping
PUT news
{
   "mappings": {
      "trkd": {
         "properties": {
            ...your fields go here...
         }
      }
   }
}

//3. Reindex your data
...

// 4. search again
POST /news/_search
{...}

after done, it can be find, and total hits also less than before
but some index without T&L, also show out. Why?

Can you show the query you're sending and a document that is returned from your query but shouldn't?

this is query
http://localhost:9200/news//_search { "query": { "bool": { "must": [{ "multi_match": { "query": "E&O" , "fields": [ "stkno", "tag" ,"content" ,"htext" ] ,"operator": "and"} }] } }, "from": 0, "size": 500, "sort": [ { "CDate": { "order": "desc" } } ] }
result

this is mapping
mapping

Oh I see, since you've taken the second approach, you need to change your query to use the .raw sub-fields instead, that should do the trick.

{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "E&O",
            "fields": [
              "stkno.raw",
              "tag.raw",
              "content.raw",
              "htext.raw"
            ],
            "operator": "and"
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 500,
  "sort": [
    {
      "CDate": {
        "order": "desc"
      }
    }
  ]
}

"stkno": {
"type": "string",
"index": "not_analyzed"
},

if i using this, mean i now need do stkno.raw right?
cause stkno.raw i need special handle during java coding.

That's correct. Your query simply needs to use the field that is not_analyzed. So if you go with option 1, then your query stays the same, if you go with option 2, then you need to use the .raw fields

i can get the result after i use first option.

if the value without this special character, it is whether can't find?
{ "query": { "bool": { "must": [{ "multi_match": { "query": "Malaysia" , "fields": [ "stkno", "tag" ,"content" ,"htext" ] ,"operator": "and"} }] } }, "from": 0, "size": 500, "sort": [ { "CDate": { "order": "desc" } } ] }

Actually Malaysia have this data in my index.
if i find like abc.N it can search.

is it by sentence it cant find if using "index": "not_analyzed"?

If you need both exact searches and full text search, then the first strategy I highlighted is the way to go. You can then do the exact matches on the *.raw fields and the full text search on the normal fields.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.