如何创建更复杂的Lucene查询字符串? [英] How to create more complex Lucene query strings?

查看:181
本文介绍了如何创建更复杂的Lucene查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是从中分离出来的题。
我的查询是双重的,但是因为两者都是相关的,所以我认为将它们放在一起是一个好主意。


  • 如何以编程方式创建查询。我知道我可以开始创建字符串并获得用查询解析器解析的字符串。但是,当我从其他资源收集信息时,有一种编程方式可以做到这一点。

  • Lucene查询的语法规则是什么?



- 编辑 -



我会给出一个我希望查询的要求示例make:

假设我有5个字段:


  1. 名字


  2. 年龄

  3. 地址



  4. 所有字段都是可选的,最后一个字段应该搜索所有其他字段。
    我遍历每一个领域,看看它是否是IsNullOrEmpty()。如果不是,我想附加一部分查询,以便添加相关的搜索部分。

    名字和姓氏应该完全匹配,并且与其他字段相比具有更多的权重。年龄是一个字符串,应该完全匹配。地址可以按顺序排列。一切都可以按顺序排列。



    我应该如何去解决这个问题?

    解决方案

    使用布尔查询

    a>类来组成查询对象。创建其中一个,并添加()其他Query对象来创建一个更大的,不连贯的查询:
    $ b

    • BooleanQuery q = new BooleanQuery ;

    • q.add(qFirstName,Occur.SHOULD);
    • q.add(qLastName,Occur.SHOULD);



    • $ b原子查询可以使用Term和TermQuery类来构建。



      (链接和示例适用于Lucene Java,但.NET应该类似。)


      This question is a spin-off from this question. My inquiry is two-fold, but because both are related I think it is a good idea to put them together.

      • How to programmatically create queries. I know I could start creating strings and get that string parsed with the query parser. But as I gather bits and pieces of information from other resources, there is a programattical way to do this.
      • What are the syntax rules for the Lucene queries?

      --EDIT--

      I'll give a requirement example for a query I would like to make:
      Say I have 5 fields:

      1. First Name
      2. Last Name
      3. Age
      4. Address
      5. Everything

      All fields are optional, the last field should search over all the other fields. I go over every field and see if it's IsNullOrEmpty(). If it's not, I would like to append a part of my query so it adds the relevant search part.
      First name and last name should be exact matches and have more weight then the other fields. Age is a string and should exact match. Address can varry in order. Everything can also varry in order.

      How should I go about this?

      解决方案

      Use the BooleanQuery class to compose query objects. Create one of these and add() other Query objects to it to create a larger, disjunctive query:

      • BooleanQuery q = new BooleanQuery();
      • q.add(qFirstName, Occur.SHOULD);
      • q.add(qLastName, Occur.SHOULD);
      • ...

      Atomic queries can be built with the Term and TermQuery classes.

      (Links and example are for Lucene Java, but .NET should be similar.)

      这篇关于如何创建更复杂的Lucene查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆