2012-10-22 148 views
0

我正在开发一个使用弹性搜索的应用程序,并且在某些情况下,我想根据术语和语言环境进行搜索。我在本地主机上elasticsearch通配符搜索和运算符

http://localhost:9200/index/type/_search 

测试这个和参数

query : { 
         wildcard : { "term" : "myterm*" } 
        }, 
        filter : { 
         and : [ 
          { 
           term : { "lang" : "en" } 
          }, 
          { 
           term : { "translations.lang" : "tr" } //this is subdocument search 
          }, 
         ] 
        } 

下面是一个例子文件:

{ 
    "_index": "twitter", 
    "_type": "tweet", 
    "_id": "5084151d2c6e5d5b11000008", 
    "_score": null, 
    "_source": { 
     "lang": "en", 
     "term": "photograph", 
     "translations": [ 
     { 
      "_id": "5084151d2c6e5d5b11000009", 
      "lang": "tr", 
      "translation": "fotoğraf", 
      "score": "0", 
      "createDate": "2012-10-21T15:30:37.994Z", 
      "author": "anonymous" 
     }, 
     { 
      "_id": "50850346532b865c2000000a", 
      "lang": "tr", 
      "translation": "resim", 
      "score": "0", 
      "createDate": "2012-10-22T08:26:46.670Z", 
      "author": "anonymous" 
     } 
     ], 
     "author": "anonymous", 
     "createDate": "2012-10-21T15:30:37.994Z" 
    } 
    } 

我试图获得与输入语言使用通配符条款(自动完成) “en”,输出语言“tr”。它正在获得具有“myterm”的条款,但不适用于此操作。任何建议将事先

+0

你能发表你的文件是什么样子的例子吗? – javanna

+0

我添加了一个文档,谢谢 –

回答

1

我已经设法解决我的问题,以下查询;

query : { 
    wildcard : { "term" : "myterm*" } 
}, 
filter : { 
    and : [ 
     { 
     term : { "lang" : "en" } 
     }, 
     { 
     term : { "translations.lang" : "tr" } //this is subdocument search 
     } 
    ] 
}, 
sort : { 
    {"term" : "desc"} 
} 

这里重要的一点是,您需要将排序字段设置为not_analyzed。因为,你不能排序分析的字段。

2

我猜想,translations元素具有nested型理解

感谢。如果是这样,你应该使用nested查询:

curl -XPOST "http://localhost:9200/twitter/tweet/_search" -d '{ 
    query: { 
     wildcard: { 
      "term": "term*" 
     } 
    }, 
    filter: { 
     and: [{ 
      term: { 
       "lang": "en" 
      } 
     }, { 
      "nested": { 
       "path": "translations", 
       "query": { 
        "term" : { "translations.lang" : "tr" } 
       } 
      } 
     }] 
    } 
}' 
+0

感谢您的回复,我的文档不是嵌套的。加入时,您需要将您的排序字段设置为not_analyzed。 –