Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get elasticsearch to perform an exact match query?

This is a two-part question.

My documents look like this:

{"url": "https://someurl.com", 
 "content": "searchable content here", 
 "hash": "c54cc9cdd4a79ca10a891b8d1b7783c295455040", 
 "headings": "more searchable content", 
 "title": "Page Title"}

My first question is how to retrieve all documents where 'title' is exactly "No Title". I don't want a document with the title of "This Document Has No Title" to appear.

My second question is how to retrieve all documents where 'url' appears exactly in a long list of urls.

I'm using pyelasticsearch, but a generic answer in curl would also work.

like image 621
GDorn Avatar asked Oct 12 '12 21:10

GDorn


2 Answers

You have to define a mapping for fields.

If you are looking for exact values (case sensitive), you can set index property to not_analyzed.

Something like :

"url" : {"type" : "string", "index" : "not_analyzed"}
like image 61
dadoonet Avatar answered Oct 17 '22 20:10

dadoonet


try this method. it's work.

import json
from elasticsearch import Elasticsearch
connection = Elasticsearch([{'host': host, 'port': port}])

elastic_query = json.dumps({
     "query": {
         "match_phrase": {
            "UserName": "name"
          }
      }
 })
result = connection.search(index="test_index", body=elastic_query)
like image 7
waruna k Avatar answered Oct 17 '22 20:10

waruna k