Adding percolator settings and mapping with Python


(Kerem Can) #1

I added default percolator index with the python code:

class Document(DocType):
    title = Text()   
    query = Percolator()

    class Meta:
        index = 'my-index'
        doc_type = '_doc'

    def save(self, **kwargs):
        return super(Document, self).save(**kwargs)

I also want to add this some settings with Python. Here is my whole query, mapping and setting. I can put this via curl

PUT /my-index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "standard_lowercase_example": {
          "type": "custom",
          "tokenizer": "whitespace",
          "filter": ["lowercase"]
        },
        "turkish_lowercase_example": {
          "type": "custom",
          "tokenizer": "whitespace",
          "filter": ["turkish_lowercase"]
        }
      },
      "filter": {
        "turkish_lowercase": {
          "type": "lowercase",
          "language": "turkish"
        }
      }
    }
  },
  "mappings": {
        "_doc": {
            "properties": {
                "title": {
                    "type": "text",
                    "analyzer": "standard_lowercase_example"
                },
                "query": {
                    "type": "percolator"
                }
            }
        }
    }
}

In shortly, I'm trying to add above curl request with my Python code. Here is my whole Python code. I think I should use put_settings(**kwargs) from API but I couldn't figure that out how to combine.

import json

from elasticsearch_dsl import (
    connections,
    DocType,
    Mapping,
    Percolator,
    Text
)
from elasticsearch_dsl.query import (
    SpanNear,
    SpanTerm
)
from elasticsearch import Elasticsearch

# Read the json File
json_data = open('titles.json').read()
data = json.loads(json_data)

docs = data['response']['docs']

# creating a new default elasticsearch connection
connections.configure(
    default={'hosts': 'localhost:9200'},
)


class Document(DocType):
    title = Text()   
    query = Percolator()

    class Meta:
        index = 'my-index'
        doc_type = '_doc'

    def save(self, **kwargs):
        return super(Document, self).save(**kwargs)


# create the mappings in elasticsearch
Document.init()

# index the query
for doc in docs:
    terms = doc['title'].split(" ")

    clauses = []
    for abcd in terms:
        x = abcd.replace("İ", "i").replace(",", "").replace("î", "i").replace("I", "ı")
        term = x.lower()
        field = SpanTerm(title=term)
        clauses.append(field)
    query = SpanNear(clauses=clauses)
    item = Document(query=query)
    item.save()

Thanks in advance!

P.S: Also you can check this question.


(system) #2

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