elasticmodels帮助您使用elasticsearch索引和查询django模型
elasticmodels的Python项目详细描述
#elasticmodels
elasticmodels帮助您使用elasticsearch索引和查询django模型。
当您需要对索引创建进行更多控制时,它是django haystack的替代品,而且您总是要使用elastic search。
install
“index”:“您的索引的名称”,“http认证”:“用户名:密码”,
}
“StopWords”:“>
>
>
>
>
>
>
````
>在已安装的应用程序中添加弹性模型:
``python
```
django应用程序,创建一个search_index.py文件,就像这样:
`` python
from elasticmodels import indexable
from django.template.loader import render_to_string
from.models import file,filetag
class file index(indexable):
此模型类型的elasticsearch映射
“
返回{
“properties”:{
“pk”:{“type”:“integer”,“index”:“not_analyted”},
“content”:{“type”:“string”,“analyzer”:“snowball”},
“tags”:{“type”:“string”,“analyzer”:“keyword”},
“组织ID”:{“类型”:“整型”,“索引”:“未分析”},
“类型”:{“类型”:“整型”,“分析器”:“关键字”},
“按ID上传”{“类型”:“整型”,“分析器”:“关键字”},
}
}
def prepare(self,obj):
“
返回转换为dict的obj,该dict对应于您定义的映射
。这是将由elasticsearch编制索引的内容。
“
”返回{
“pk”:obj.pk,
“content”:将“u”呈现为“u”字符串(“files/search.txt”,{“object”:obj}),
“tags”:[ft.tag.name for ft in filetag.objects.filter(file=obj).select“u related”(“tag”),
“org-id”:obj.org-id,
“type”:obj.type,
“uploaded戋by戋id”:obj.uploaded戋by戋id,
}
````
所有
模型对象都将被重新索引。
type=1)
f.save()
make_searchable(f)
`````
```查询
elasticmodels的子类。indexable有一个名为“objects`
的类属性,该属性返回elasticutils的实例。然后您可以使用s上elasticutils中可用的任何方法实例。
请参见:
http://elasticutils.readthedocs.org/en/latest/search.html
http://elasticutils.readthedocs.org/en/latest/search.html过滤过滤器
http://elasticutils.readthedocs.org/en/latest/search.html查询查询
http://elasticutils.readthedocs.org/en/latest/search.html/>
`` python
from elasticutils import f
from.search_index import fileindex
results=fileindex.objects.filter(f(type=1)f(type=2)).query(content_u match=“foo“)
结果:
打印result.pk,result.content
````
elasticmodels帮助您使用elasticsearch索引和查询django模型。
当您需要对索引创建进行更多控制时,它是django haystack的替代品,而且您总是要使用elastic search。
install
“index”:“您的索引的名称”,“http认证”:“用户名:密码”,
}
“StopWords”:“>
>
>
>
>
>
>
````
>在已安装的应用程序中添加弹性模型:
``python
```
django应用程序,创建一个search_index.py文件,就像这样:
`` python
from elasticmodels import indexable
from django.template.loader import render_to_string
from.models import file,filetag
class file index(indexable):
此模型类型的elasticsearch映射
“
返回{
“properties”:{
“pk”:{“type”:“integer”,“index”:“not_analyted”},
“content”:{“type”:“string”,“analyzer”:“snowball”},
“tags”:{“type”:“string”,“analyzer”:“keyword”},
“组织ID”:{“类型”:“整型”,“索引”:“未分析”},
“类型”:{“类型”:“整型”,“分析器”:“关键字”},
“按ID上传”{“类型”:“整型”,“分析器”:“关键字”},
}
}
def prepare(self,obj):
“
返回转换为dict的obj,该dict对应于您定义的映射
。这是将由elasticsearch编制索引的内容。
“
”返回{
“pk”:obj.pk,
“content”:将“u”呈现为“u”字符串(“files/search.txt”,{“object”:obj}),
“tags”:[ft.tag.name for ft in filetag.objects.filter(file=obj).select“u related”(“tag”),
“org-id”:obj.org-id,
“type”:obj.type,
“uploaded戋by戋id”:obj.uploaded戋by戋id,
}
````
所有
模型对象都将被重新索引。
type=1)
f.save()
make_searchable(f)
`````
```查询
elasticmodels的子类。indexable有一个名为“objects`
的类属性,该属性返回elasticutils的实例。然后您可以使用s上elasticutils中可用的任何方法实例。
请参见:
http://elasticutils.readthedocs.org/en/latest/search.html
http://elasticutils.readthedocs.org/en/latest/search.html过滤过滤器
http://elasticutils.readthedocs.org/en/latest/search.html查询查询
http://elasticutils.readthedocs.org/en/latest/search.html/>
`` python
from elasticutils import f
from.search_index import fileindex
results=fileindex.objects.filter(f(type=1)f(type=2)).query(content_u match=“foo“)
结果:
打印result.pk,result.content
````