1
votes

Mapping

      "isValid" : {
        "type" : "boolean"
      },
      "lName" : {
        "type" : "text"
      },
      "mname" : {
        "type" : "keyword"
      }

Data

          "isValid" : true
          "lName" : "John scena",
          "mname" : "prod"

Query (Match)

{
    "query": {
        "match" : {
            "lName" : "John scena"
        }
    }
}

It gives me no result but,

   {
        "query": {
            "match" : {
                "mname" : "prod"
            }
        }
    }

this gives me proper result

Query (Term)

{
   "query":{
      "term":{"lName":"John scena"}
   }
}

This will also not give any response.

My Questions:

  1. Why is it happening? Please give a proper solution!
  2. What is different between "term", "match", "match phrase" query.
  3. What is different between "keyword","text","string"? It affect in query?
  4. What is date datatype format and in which format we can save data in type?

TIA.

2
@Val Please help me and give an answer if possible!Unknown_Coder
Where is your term query? We can see only match queries. Can you share term query also?avr
@avr i forgot to mention that over here, i have edit que.Unknown_Coder

2 Answers

2
votes

In elasticsearch documents are inversed indexed. For inverse indexing we need to split the strings properly. For that we have analyser in elasticsearch. So by default at the time of indexing it will split "john scena" into "john" and "scena" and they will be pointing to same document. So you are trying to index "john scena", but it gets indexed as "john","scena". You can change behaviour of your analysers by creating custom analysers.

Now every index also have search analyser, which says how the search term should be analysed at search time. By default it has the same value as index time analyser. When we do term query, it says that don't analyse my search term. While match says that analyse me before searching. As you have indexed "john scena", it gets indexed as "john","scena". Ans you are trying to do term query with "john scena", it will not match. Because term query stops ES from analysing "john scena", so it looks for exact match. But the values that were indexed are "john","scena". So you will not get any match. In case of match query, your search term is also split into "john" and "scena" and you get the hits. For more details, refer to docs, it has good examples.

From ES 5 we have keyword field by default to have exact value. You can use term query with that if you know exactly what you are searching. But be careful as exact means exact, even the case difference will result into no hits found.

1
votes

lName needs to be of type keyword for your term query to match. If it's text as you have it, it will be analyzed and, thus, split into multiple terms.

And term query will match exactly the text you've given to it.

The solution is to change the mapping to:

  "lName" : {
    "type" : "text"
  }

What is different between "term", "match", "match phrase" query.

The biggest difference between these is that term is not analyzing the text received as search text, whereas the other two will.

What is different between "keyword","text","string"? It affect in query?

text as well as string used with "index": "analyzed" (or the default) will analyze the text at indexing time and will potentially split the text in the document into multiple terms. keyword or string with "index":"not_analyzed" option will not touch the text at indexing time and there will be only one term indexed for that value for a single document.

The questions you have are basic ones and the documentation is really good at covering these. I strongly suggest going over the documentation on these: https://www.elastic.co/guide/en/elasticsearch/guide/master/term-vs-full-text.html https://www.elastic.co/guide/en/elasticsearch/reference/5.4/text.html https://www.elastic.co/guide/en/elasticsearch/reference/5.4/keyword.html https://www.elastic.co/guide/en/elasticsearch/guide/master/match-query.html