关于ElasticSearch增删改查的方法有很多,使用curl操作命令总结如下,如有需要可以点击收藏。insert into users(name,age,email) values('ctt',18,'ctt@abc.com')curl -XPOST "http://127.0.0.1:9200/users/_doc" -H "Content-Type: application/json" -d '
{
"name": "ctt",
"age": 18,
"email": "ctt@abc.com"
}'
上面的命令使用HTTP POST方法向名为"users"的索引中添加一条文档,文档包含"name"、"age"和"email"三个字段。其中,-X选项指定HTTP请求的方法,-H选项指定HTTP请求的头部信息,-d选项指定HTTP请求的数据体。写了多条记录,便于后面进行测试,结果如下:{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":5,"max_score":1.0,"hits":[{"_index":"users","_type":"_doc","_id":"Uxz2jYYBNjbWHoXGCfXa","_score":1.0,"_source":
{
"name": "jgt",
"age": 29,
"email": "jgt@abc.com"
}},{"_index":"users","_type":"_doc","_id":"UBz0jYYBNjbWHoXGufWJ","_score":1.0,"_source":
{
"name": "ttc",
"age": 20,
"email": "ttc@abc.com"
}},{"_index":"users","_type":"_doc","_id":"URz1jYYBNjbWHoXGOvU0","_score":1.0,"_source":
{
"name": "tt",
"age": 25,
"email": "tt@abc.com"
}},{"_index":"users","_type":"_doc","_id":"Uhz1jYYBNjbWHoXGhPWI","_score":1.0,"_source":
{
"name": "att",
"age": 27,
"email": "att@abc.com"
}},{"_index":"users","_type":"_doc","_id":"TxzvjYYBNjbWHoXG-fUx","_score":1.0,"_source":
{
"name": "ctt",
"age": 18,
"email": "ctt@abc.com"
}}]}}
可以用pretty加以修饰,结果集显示如下: {
"name" : "ttc",
"age" : 20,
"email" : "ttc@abc.com"
}
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "URz1jYYBNjbWHoXGOvU0",
"_score" : 1.0,
"_source" : {
"name" : "tt",
"age" : 25,
"email" : "tt@abc.com"
}
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "Uhz1jYYBNjbWHoXGhPWI",
"_score" : 1.0,
"_source" : {
"name" : "att",
"age" : 27,
"email" : "att@abc.com"
}
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "TxzvjYYBNjbWHoXG-fUx",
"_score" : 1.0,
"_source" : {
"name" : "ctt",
"age" : 18,
"email" : "ctt@abc.com"
}
}
]
}
}curl -XPOST "http://127.0.0.1:9200/users/_create" -H "Content-Type: application/json" -d '
{
"name": "ctty",
"age": 30,
"email": "ctty@abc.com"
}'update users set age=30 where name='jgt'curl -XPOST "http://127.0.0.1:9200/users/_update_by_query" -H "Content-Type: application/json" -d '
{
"query": {
"term": {
"name": "jgt"
}
},
"script": {
"source": "ctx._source.age = params.new_age",
"params": {
"new_age": 30
}
}
}'
上面的命令使用HTTP POST方法向名为"users"的索引中name为“jgt”的所有文档的年龄为30。其中:-X选项指定HTTP请求的方法
-H选项指定HTTP请求的头部信息
-d选项指定HTTP请求的数据体
"query"字段表示查询条件:"term"表示匹配字段的值等于给定值,"name"字段表示要匹配的字段,"jgt"表示要匹配的值。
"script"字段表示更新脚本:"source"表示要执行的脚本,"ctx._source.age"表示要更新的字段,"params"表示要传入的参数,"new_age"表示要更新成的新值。curl -XPOST "http://127.0.0.1:9200/users/_bulk" -H "Content-Type: application/json" -d '
{ "update": {"_id": "1", "_index": "users"} }
{ "doc": {"age": 16} }
{ "update": {"_id": "2", "_index": "users"} }
{ "doc": {"age": 16} }
'curl -XPOST "http://127.0.0.1:9200/users/_doc/1/_update" -H "Content-Type: application/json" -d '
{
"doc": {
"age": 16
}
}'delete from users where name='jgtcurl -XPOST "http://127.0.01:9200/users/_delete_by_query" -H "Content-Type: application/json" -d '
{
"query": {
"term": {
"name": "att"
}
}
}'curl -XDELETE "http://127.0.0.1:9200/users/_doc/1"{ "query": { "match": { "my_field": "my_value" } } }curl -XGET "http://127.0.0.1:9200/users/_search?pretty" -H "Content-Type: application/json" -d '
{
"query": {
"match": {
"name": "tt"
}
}
}'
{ "query": { "term": { "my_field": "my_value" } } }curl -XGET "http://127.0.0.1:9200/users/_search?pretty" -H "Content-Type: application/json" -d '
{
"query": {
"term": {
"name": "ctt"
}
}
}'
{ "query": { "range": { "my_field": { "gte": 10, "lte": 20 } } } }curl -XGET "http://127.0.0.1:9200/users/_search?pretty" -H "Content-Type: application/json" -d '
{
"query": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}'
{ "query":
{ "bool":
{ "must": [ { "match": { "my_field1": "my_value1" } },
{ "match": { "my_field2": "my_value2" } }
]
} } }curl -XGET "http://127.0.0.1:9200/users/_search?pretty" -H "Content-Type: application/json" -d '
{
"query": {
"bool": {
"must": [
{ "match": { "name": "ctt" } },
{ "match": { "age": "18" } }
]
}
}
}'
must查询:所有的查询条件都必须匹配才能返回文档。可以使用多个must子句来构建复杂的查询。
should查询:至少有一个查询条件匹配时返回文档。可以使用多个should子句来构建复杂的查询。
must_not查询:所有的查询条件都不能匹配才能返回文档。{
"query": {
"bool": {
"must": [
{ "match": { "field1": "value1" } },
{ "match": { "field2": "value2" } }
],
"should": [
{ "match": { "field3": "value3" } },
{ "match": { "field4": "value4" } }
],
"must_not": [
{ "match": { "field5": "value5" } }
]
}
}
}必须匹配field1为value1的文档;
必须匹配field2为value2的文档;
至少匹配一个should子句,其中field3为value3或field4为value4;
不匹配field5为value5的文档。curl -XGET "http://127.0.0.1:9200/users/_search?pretty" -H "Content-Type: application/json" -d '
{
"query": {
"bool": {
"must": [
{ "match": { "name": "ctt" } },
{ "match": { "age": "18" } }
],
"should": [
{ "match": { "name": "tt" } },
{ "match": { "age": "18" } }
],
"must_not": [
{ "match": { "name": "jgt" } }
]
}
}
}'
select * from users where (name like '%ctt%' or name like '%tt%') and age >=10 and age<=2curl -XGET "http://127.0.0.1:9200/users/_search?pretty" -H "Content-Type: application/json" -d '
{
"query": {
"bool": {
"should": [
{ "match": { "name": "ctt" } },
{ "match": { "name": "tt" } }
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}
}
}'
需要注意的是,查询操作的结果可以通过Elasticsearch的排序、过滤、聚合等方式进行处理,以满足不同的业务需求。同时,查询操作也会占用Elasticsearch的系统资源,因此需要谨慎使用,建议根据具体的业务需求选择合适的查询方式。我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i