简体   繁体   English

如何使用Java在Elasticsearch中搜索JSON字段?

[英]How to search for a JSON field in elasticsearch using java?

This is JSON that I want to use for a search: 这是我要用于搜索的JSON

{   
    "_index" : "test",   "_type" : "insert",   "_id" : "3",  
    "_version" : 2,   "found" : true,   
    "_source" : {
         "ACCOUNT_ID" : "123",
         "CONTACT_ID" : "ABC"   
    } 
}

How do I search for all the JSON which have ACCOUNT_ID starting from 1? 如何搜索所有ACCOUNT_ID从1开始的所有JSON

You can use Wildcard in elasticsearch to search for an ACCOUNT_ID which starts from 1 您可以在elasticsearch中使用通配符来搜索从1开始的ACCOUNT_ID

GET index/_search
 {
      "query": {
        "wildcard": {
          "ACCOUNT_ID ": {
            "value": "1*"
          }
        }
      }
  }

In Java, you can try something like this: 在Java中,您可以尝试如下操作:

QueryBuilders.wildcardQuery("ACCOUNT_ID ", "1*");

From what i see in your comments you are trying to find id's starting with 1 for example. 根据我在您的评论中看到的内容,您正在尝试查找以1开头的id。 Well if your analyzer is the standard one the id "123" is tokenized like "123". 好吧,如果您的分析仪是标准分析仪,则ID“ 123”将像“ 123”一样被标记。 You can use wildcard and search like '1*'. 您可以使用通配符并像“ 1 *”一样进行搜索。 Be careful using wildcards cause it takes some memory. 使用通配符时要小心,因为它会占用一些内存。

See here: QueryString - Wildcard 参见此处: QueryString-通配符

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

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