inverted_index
inverted_index

Reputation: 2437

Elasticsearch exact matching doesn't work correctly

I'm using elasticsearch for logging. I have an index named "query_index" that has four interior fields. I use the following mapping to create this index:

POST
/query_index/queries

{
  "mappings": {
    "queries": {
      "properties": {
        "query": {
          "type": "string",
          "store": true 
        },
          "exact_match":{
            "type":"string",
              "index":"not_analyzed"
          },
          "search_type": {
            "type" : "string",
              "store": true
          },
        "search_engine": {
            "type" : "string",
              "store": true
          }
      }
    }
  }
}

As you see the 2nd field, I put "exact_match" as not_analyzed field in order to retrieve exact match queries.

Let's say that I hold two documents in this index:

{
    "query": "Barack Obama",
    "exact_match": "Barack Obama",
    "search_engine": "google",
    "search_type": "api"
}


{
    "query": "Barack",
    "exact_match": "Barack",
    "search_engine": "google",
    "search_type": "api"
}

Now I want to retrieve only the second document. So I use the following query:

Post
/query_index/queries/_search

{"query":
            {"bool":
                {"must":
                    [
                      { "match": { "exact_match": "Barack" }},
                      { "match": { "search_engine": "google" }},
                      { "match": { "search_type": "api" }}
                    ]
                }
            }

}

But after running the above query, both the two documents are retrieved while I expect the second document to be retrieved. I almost searched entire Stack, but I got no result of what I'm supposed to do.

UPDATE:

GET
/query_index/_mapping
{
"query_index": {
"mappings": {
"queries": {
"properties": {
"exact_match": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"mappings": {
"properties": {
"queries": {
"properties": {
"properties": {
"properties": {
"exact_match": {
"properties": {
"index": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"query": {
"properties": {
"store": {
"type": "boolean"
},
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"search_engine": {
"properties": {
"store": {
"type": "boolean"
},
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"search_type": {
"properties": {
"store": {
"type": "boolean"
},
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
}
},
"query": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"search_engine": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"search_type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}

Upvotes: 0

Views: 210

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52366

The correct index creation command with that specific mapping is with PUT and not POST. With POST you just create a document in that index which automatically will create some fields structure inside the index.

The correct command to create the index with this specific mapping is:

PUT /query_index
{
  "mappings": {
    "queries": {
      "properties": {
        "query": {
          "type": "string",
          "store": true
        },
        "exact_match": {
          "type": "string",
          "index": "not_analyzed"
        },
        "search_type": {
          "type": "string",
          "store": true
        },
        "search_engine": {
          "type": "string",
          "store": true
        }
      }
    }
  }
}

Upvotes: 1

Related Questions