标签: elasticsearch-dsl

如何在elasticsearch-dsl python中选择特定字段

我正在使用elasticsearch dsl搜索elasticsearch:https : //elasticsearch-dsl.readthedocs.org/en/latest/

进行搜索查询时如何过滤特定字段:

我知道它在Elasticsearch中受支持:https//www.elastic.co/guide/en/elasticsearch/reference/1.4/search-request-fields.html

只是不知道如何在Elasticsearch-dsl中做同样的事情

python elasticsearch elasticsearch-dsl

4
推荐指数
1
解决办法
3999
查看次数

使用elasticsearch-dsl DocType的映射配置

我正在开发一个简单的nlp工具,并使用elasticsearch-dsl作为django的es工具。

我将有两个“ DocType”,实体和意图。我创建了自己的分析器,它是:

turkish_stop = token_filter('turkish_stop', type='stop', stopwords="_turkish_")
turkish_lowercase = token_filter('turkish_lowercase', type='lowercase', language="turkish")
turkish_stemmer = token_filter('turkish_stemmer', type='stemmer', language='turkish')

turkish_analyzer = analyzer('turkish_analyzer', tokenizer='whitespace', filter=['apostrophe', 'asciifolding',
                                                                                turkish_lowercase, turkish_stop,
                                                                                turkish_stemmer])
Run Code Online (Sandbox Code Playgroud)

例如,在每个文档中,我都有一个自定义映射。

class Entity(DocType):
    entity_synonyms = String(analyzer=es.turkish_analyzer, include_in_all=True)
    entity_key = String(index='not_analyzed', include_in_all=False)

    class Meta:
        index = es.ELASTICSEARCH_INDEX
        doc_type = es.ELASTICSEARCH_ENTITY_DOCTYPE
Run Code Online (Sandbox Code Playgroud)

根据文档http://elasticsearch-dsl.readthedocs.org/en/latest/persistence.html#persistence。Entity.init()将为此文档创建映射。它确实在我的es上创建了映射(仅用于实体doc!:()。但是,在Entity.init()之后,我无法使用Intent做同样的事情。它给出以下错误:

IllegalOperation: You cannot update analysis configuration on an open index, you need to close index nlp first.
Run Code Online (Sandbox Code Playgroud)

有解决的办法吗?如果可能的话,我真的想使用Entity.init()和Intent.init()。

django elasticsearch django-rest-framework elasticsearch-dsl

4
推荐指数
1
解决办法
1828
查看次数

Elasticsearch延迟存储并立即搜索

我在用 用python。并dsl在python中使用驱动程序。

我的脚本如下。

import time
from elasticsearch_dsl import DocType, String
from elasticsearch import exceptions as es_exceptions
from elasticsearch_dsl.connections import connections

ELASTICSEARCH_INDEX = 'test'

class StudentDoc(DocType):
    student_id = String(required=True)
    tags = String(null_value=[])

    class Meta:
        index = ELASTICSEARCH_INDEX

    def save(self, **kwargs):
        '''
        Override to set metadata id
        '''
        self.meta.id = self.student_id
        return super(StudentDoc, self).save(**kwargs)

# Define a default Elasticsearch client
connections.create_connection(hosts=['localhost:9200'])

# create the mappings in elasticsearch
StudentDoc.init()

student_doc_obj = \
    StudentDoc(
        student_id=str(1),
        tags=['test'])

try:
    student_doc_obj.save()
except es_exceptions.SerializationError as …
Run Code Online (Sandbox Code Playgroud)

python elasticsearch elasticsearch-dsl elasticsearch-py

4
推荐指数
1
解决办法
632
查看次数

Elasticsearch 日期查询。某月出生的人

我有一个具有以下映射的字段:

birthdate: { type: :date, format: :dateOptionalTime }
Run Code Online (Sandbox Code Playgroud)
  1. 我需要找到所有五月出生的人(包括所有年份)
  2. 另一个查询是查找所有出生于“8 月 25 日”的人(包括所有年份)

对此的查询是什么?

elasticsearch elasticsearch-mapping elasticsearch-dsl elasticsearch-query

3
推荐指数
1
解决办法
3940
查看次数

elasticsearch-dsl:否定过滤器

我有一个这样的查询

   s = Search(using=es, index=indices)
   s = s.filter('range', 
                utctimestamp={'gte': begindateUTC, 'lte': enddateUTC})
         .filter("term",tags="foo").
         .sort("-utctimestamp")
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何取消过滤器?即不是foo。以上是使用过滤器的“必须”,例如

  "filter": {
    "bool": {
      "must": [
        {
          "range": {
            "utctimestamp": {
              "from": 1460407864660,
              "to": 1460409664660
            }
          }
        },
        {
          "fquery": {
            "query": {
              "query_string": {
                "query": "_type:(\"foo\")"
              }
            },
            "_cache": true
          }
        }
      ]
Run Code Online (Sandbox Code Playgroud)

我该如何将“foo”位设置为“不得”。

提前致谢

elasticsearch elasticsearch-dsl

3
推荐指数
1
解决办法
2458
查看次数

elasticsearch-dsl-py查询形成

有人可以告诉我如何使用这个dsl​​ python模块构建这个示例查询?

我到目前为止只有一部分查询字符串.

from elasticsearch_dsl import Search, Q, A, query, aggs
s = Search()
s.aggs.bucket('2', 'terms', field = 'Subscriber Type', size=5)
Run Code Online (Sandbox Code Playgroud)

我不确定查询的其余部分的语法是什么.任何帮助深表感谢.

所需的查询结构如下.

{
   "size": 0,
   "query": {
   "filtered": {
   "query": {
    "query_string": {
      "query": "lincoln",
      "analyze_wildcard": true
    }
  },
  "filter": {
    "bool": {
      "must": [
        {
          "range": {
            "Start date": {
              "gte": 936157359664,
              "lte": 1472701359665,
              "format": "epoch_millis"
            }
          }
        }
      ],
      "must_not": []
    }
  }
}
},
 "aggs": {
 "2": {
    "terms": {
    "field": "Subscriber Type",
    "size": 5,
    "order": …
Run Code Online (Sandbox Code Playgroud)

python elasticsearch elasticsearch-dsl

3
推荐指数
1
解决办法
2821
查看次数

Elasticsearch 2.4,对嵌套对象不起作用存在过滤器

我的映射是:

"properties": {
  "user": {
    "type": "nested",
    "properties": {
      "id": {
        "type": "integer"
      },
      "is_active": {
        "type": "boolean",
        "null_value": false
      },
      "username": {
        "type": "string"
      }
    }
  },
Run Code Online (Sandbox Code Playgroud)

我想获取所有没有user字段的文档。

我试过了:

GET /index/type/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "user"
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

返回所有文档。基于ElasticSearch 2.x,存在用于嵌套字段的过滤器不起作用的问题,我也尝试过:

GET /index/type/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must_not": [
            {
              "exists": {
                "field": "user"
              }
            }
          ]
        } …
Run Code Online (Sandbox Code Playgroud)

elasticsearch elasticsearch-dsl elasticsearch-2.0

3
推荐指数
1
解决办法
1034
查看次数

搜索嵌套文档

我在使用Python 中的elasticsearch_dslelasticsearch库搜索嵌套文档时遇到问题。

我可以成功地对文档的顶级(即非嵌套)部分执行搜索,但是我搜索嵌套部分的所有尝试都因某种原因而失败。

我已经在 StackOverflow 和网络上搜索了使用 Python 搜索嵌套文档的权威指南,但一直在简短说明。

这是我正在使用的示例文档:

{"username": "nancy",
"codeData": [
 {"code": "B1", "order": "2"}, 
 {"code": "L4", "order": "1"}
  ] 
}
Run Code Online (Sandbox Code Playgroud)

我在索引中有 7 个文档,我已将其映射如下:

request_body = {
    "settings" : {
        "number_of_shards": 5,
        "number_of_replicas": 1
    },

    'mappings': {
        'testNesting': {
            'properties': {
                'username': {'type': 'text'},
                'codeData': {'type': 'nested',
                                  'properties' :{
                                      "code" : {"type":"text"},
                                      "order" :{"type":"text"}
                                      }
                                    }
                                 }
            }
        }
    }
es.indices.create(index = "nest-test6", body = request_body)
Run Code Online (Sandbox Code Playgroud)

执行以下搜索工作正常:

s = Search(using = es).query("match", username = …
Run Code Online (Sandbox Code Playgroud)

python elasticsearch elasticsearch-dsl

3
推荐指数
1
解决办法
1405
查看次数

Django Elastic Search:AttributeError:类型对象“PostDocument”没有属性“Django”

我在 django 的弹性搜索方面很新......当我运行这个命令时,python3 manage.py search_index --rebuild 它会触发我这个错误:我没有得到什么问题

File "/home/pyking/.local/lib/python3.6/site-packages/django_elasticsearch_dsl/registries.py", line 39, in register_document
    django_meta = getattr(document, 'Django')
AttributeError: type object 'PostDocument' has no attribute 'Django'
Run Code Online (Sandbox Code Playgroud)

这是我的 documents.py

from django_elasticsearch_dsl import DocType, Index
from blog2.models import Article

posts = Index('articles')

@posts.doc_type
class PostDocument(DocType):
    class Meta:
        model = Article

        fields = [
            'alias',
            'author',
            'title',
            'body',
            'category',
        ]
Run Code Online (Sandbox Code Playgroud)

这是我的模型:

class Article(models.Model):
    alias = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='author')
    title = models.CharField(max_length=200)
    body = models.TextField()
    category = models.ForeignKey(Category, on_delete=models.CASCADE) …
Run Code Online (Sandbox Code Playgroud)

django elasticsearch django-rest-framework elasticsearch-dsl elasticsearch-dsl-py

3
推荐指数
1
解决办法
2504
查看次数

使用 nGram 分析器的 Django ElasticSearch DSL 部分匹配

django-elasticsearch-dsl 我对 ElasticSearch 主题还很陌生,我正在尝试使用 ElasticSearch 和库Github repo在我的 Django 应用程序中实现简单的电子商务搜索。

\n

我试图(极其简化)实现的目标是,考虑到这些 Django 模型实例:

\n
Red T-shirts\nBlue T-Shirts\nNice T-Shirts\n
Run Code Online (Sandbox Code Playgroud)\n

对于搜索词,T-Sh我将获得所有这三个结果:

\n
Red T-shirts\nBlue T-Shirts\nNice T-Shirts\n
Run Code Online (Sandbox Code Playgroud)\n

所以我在shop/models.py中有这个模型(同样非常简化)

\n
class Category(models.Model):\n   title = models.CharField(max_length=150, blank=False)\n   description = models.CharField(max_length=150, blank=False)\n   # In reality here I have more fields\n   def __str__(self):\n      return self.title\n
Run Code Online (Sandbox Code Playgroud)\n

shop/documents.py

\n
from elasticsearch_dsl import analyzer, tokenizer\n\nautocomplete_analyzer = analyzer(\'autocomplete_analyzer\',\n            tokenizer=tokenizer(\'trigram\', \'nGram\', min_gram=1, max_gram=20),\n            filter=[\'lowercase\']\n        )from elasticsearch_dsl import analyzer, tokenizer\n\n@registry.register_document\nclass CategoryDocument(Document):\n\n    title: fields.TextField(analyzer=autocomplete_analyzer, search_analyzer=\'standard\') # …
Run Code Online (Sandbox Code Playgroud)

django elasticsearch elasticsearch-dsl

3
推荐指数
1
解决办法
1197
查看次数