简体   繁体   English

如何在Elasticsearch中使用query_string语法搜索这组字段

[英]How to search this set of fields using query_string syntax in elasticsearch

It could be that I've created my index wrong, but I have a lead index with variable field names that I need to search through. 可能是我创建的索引错误,但是我有一个潜在顾客索引,其中包含需要搜索的可变字段名称。 I created a sub object called fields that contains name and value. 我创建了一个名为字段的子对象,其中包含名称和值。 Sample: 样品:

[
  {
    "name": "first_name",
    "value": "XXX"
  },
  {
    "name": "last_name",
    "value": "XXX"
  },
  {
    "name": "email",
    "value": "X0@yahoo.com"
  },
  {
    "name": "address",
    "value": "X Thomas RD Apt 1023"
  },
  {
    "name": "city",
    "value": "phoenix"
  },
  {
    "name": "state",
    "value": "AZ"
  },
  {
    "name": "zip",
    "value": "12345"
  },
  {
    "name": "phone",
    "value": "5554448888"
  },
  {
    "name": "message",
    "value": "recently had XXXX"
  }
]

name field is not_analyzed , and value field is analyzed and not, as .exact and .search name字段not_analyzed ,和value字段analyzed ,而不是像.exact和.search

I thought I could get the results I want from a query string query doing something like 我以为我可以从查询字符串查询中获得想要的结果,例如

+fields.name: first_name +fields.value.exact: XXX

But it doesn't quite work the way I thought. 但这并不完全符合我的想法。 I figure its because I'm trying to use this as mysql instead of as nosql, and there is a fundamental brain shift I must have. 我想出它的原因是因为我试图将其用作mysql而不是nosql,并且我必须要有一个基本的头脑转变。

While the approach you are taking probably should work with enough effort, you are much better off having explicit field names for everything, eg: { "name.first_name" : "XXX", "name.last_name" : "XXX", etc... } 尽管您所采用的方法应该足够努力,但最好为所有内容都使用显式字段名称,例如:{“ name.first_name”:“ XXX”,“ name.last_name”:“ XXX”等。 ..}

Then your query_string looks like this: name.first_name:XXX 然后您的query_string看起来像这样:name.first_name:XXX

If you are a new to elasticsearch, play around with things before you add your mappings. 如果您是Elasticsearch的新手,请在添加映射之前尝试一下。 The dynamic defaults should kick in and things will work. 动态默认值应该生效,一切都会正常进行。 You then add mappings to get fine grained control over the field behavior. 然后,您可以添加映射以获得对字段行为的精细控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM